mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(snackbar): make closeWithAction public method (#5686)
* fix(snackbar): make closeWithAction public method * Add unit tests
This commit is contained in:
committed by
Andrew Seguin
parent
940f1a2dbe
commit
f4f64ac59e
@@ -3,4 +3,4 @@
|
||||
<button
|
||||
class="mat-simple-snackbar-action"
|
||||
*ngIf="hasAction"
|
||||
(click)="dismiss()">{{data.action}}</button>
|
||||
(click)="action()">{{data.action}}</button>
|
||||
|
||||
@@ -36,9 +36,9 @@ export class SimpleSnackBar {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/** Dismisses the snack bar. */
|
||||
dismiss(): void {
|
||||
this.snackBarRef._action();
|
||||
/** Performs the action on the snack bar. */
|
||||
action(): void {
|
||||
this.snackBarRef.closeWithAction();
|
||||
}
|
||||
|
||||
/** If the action button should be shown. */
|
||||
|
||||
@@ -25,13 +25,13 @@ export class MdSnackBarRef<T> {
|
||||
containerInstance: MdSnackBarContainer;
|
||||
|
||||
/** Subject for notifying the user that the snack bar has closed. */
|
||||
private _afterClosed: Subject<any> = new Subject();
|
||||
private _afterClosed = new Subject<void>();
|
||||
|
||||
/** Subject for notifying the user that the snack bar has opened and appeared. */
|
||||
private _afterOpened: Subject<any>;
|
||||
private _afterOpened = new Subject<void>();
|
||||
|
||||
/** Subject for notifying the user that the snack bar action was called. */
|
||||
private _onAction: Subject<any> = new Subject();
|
||||
private _onAction = new Subject<void>();
|
||||
|
||||
/**
|
||||
* Timeout ID for the duration setTimeout call. Used to clear the timeout if the snackbar is
|
||||
@@ -55,19 +55,19 @@ export class MdSnackBarRef<T> {
|
||||
clearTimeout(this._durationTimeoutId);
|
||||
}
|
||||
|
||||
/** Dismisses the snack bar after some duration */
|
||||
_dismissAfter(duration: number): void {
|
||||
this._durationTimeoutId = setTimeout(() => this.dismiss(), duration);
|
||||
}
|
||||
|
||||
/** Marks the snackbar action clicked. */
|
||||
_action(): void {
|
||||
closeWithAction(): void {
|
||||
if (!this._onAction.closed) {
|
||||
this._onAction.next();
|
||||
this._onAction.complete();
|
||||
}
|
||||
}
|
||||
|
||||
/** Dismisses the snack bar after some duration */
|
||||
_dismissAfter(duration: number): void {
|
||||
this._durationTimeoutId = setTimeout(() => this.dismiss(), duration);
|
||||
}
|
||||
|
||||
/** Marks the snackbar as opened */
|
||||
_open(): void {
|
||||
if (!this._afterOpened.closed) {
|
||||
|
||||
@@ -305,6 +305,29 @@ describe('MdSnackBar', () => {
|
||||
tick(500);
|
||||
}));
|
||||
|
||||
it('should allow manually closing with an action', fakeAsync(() => {
|
||||
let dismissObservableCompleted = false;
|
||||
let actionObservableCompleted = false;
|
||||
let snackBarRef = snackBar.open('Some content');
|
||||
viewContainerFixture.detectChanges();
|
||||
|
||||
snackBarRef.afterDismissed().subscribe(undefined, undefined, () => {
|
||||
dismissObservableCompleted = true;
|
||||
});
|
||||
snackBarRef.onAction().subscribe(undefined, undefined, () => {
|
||||
actionObservableCompleted = true;
|
||||
});
|
||||
|
||||
snackBarRef.closeWithAction();
|
||||
viewContainerFixture.detectChanges();
|
||||
flushMicrotasks();
|
||||
|
||||
expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
|
||||
expect(actionObservableCompleted).toBeTruthy('Expected the snack bar to notify of action');
|
||||
|
||||
tick(500);
|
||||
}));
|
||||
|
||||
it('should dismiss automatically after a specified timeout', fakeAsync(() => {
|
||||
let dismissObservableCompleted = false;
|
||||
let config = new MdSnackBarConfig();
|
||||
@@ -386,6 +409,29 @@ describe('MdSnackBar', () => {
|
||||
.toBe('Chimichanga', 'Expected the injected data object to be the one the user provided.');
|
||||
});
|
||||
|
||||
it('should allow manually closing with an action', fakeAsync(() => {
|
||||
let dismissObservableCompleted = false;
|
||||
let actionObservableCompleted = false;
|
||||
const snackBarRef = snackBar.openFromComponent(BurritosNotification);
|
||||
viewContainerFixture.detectChanges();
|
||||
|
||||
snackBarRef.afterDismissed().subscribe(undefined, undefined, () => {
|
||||
dismissObservableCompleted = true;
|
||||
});
|
||||
snackBarRef.onAction().subscribe(undefined, undefined, () => {
|
||||
actionObservableCompleted = true;
|
||||
});
|
||||
|
||||
snackBarRef.closeWithAction();
|
||||
viewContainerFixture.detectChanges();
|
||||
flushMicrotasks();
|
||||
|
||||
expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
|
||||
expect(actionObservableCompleted).toBeTruthy('Expected the snack bar to notify of action');
|
||||
|
||||
tick(500);
|
||||
}));
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user