diff --git a/src/lib/snack-bar/simple-snack-bar.html b/src/lib/snack-bar/simple-snack-bar.html index cff85a370ce5..28bf81598e0b 100644 --- a/src/lib/snack-bar/simple-snack-bar.html +++ b/src/lib/snack-bar/simple-snack-bar.html @@ -3,4 +3,4 @@ + (click)="action()">{{data.action}} diff --git a/src/lib/snack-bar/simple-snack-bar.ts b/src/lib/snack-bar/simple-snack-bar.ts index 4b247562611d..d2db0ef2e206 100644 --- a/src/lib/snack-bar/simple-snack-bar.ts +++ b/src/lib/snack-bar/simple-snack-bar.ts @@ -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. */ diff --git a/src/lib/snack-bar/snack-bar-ref.ts b/src/lib/snack-bar/snack-bar-ref.ts index a56bdc64b381..32349ff2c3ed 100644 --- a/src/lib/snack-bar/snack-bar-ref.ts +++ b/src/lib/snack-bar/snack-bar-ref.ts @@ -25,13 +25,13 @@ export class MdSnackBarRef { containerInstance: MdSnackBarContainer; /** Subject for notifying the user that the snack bar has closed. */ - private _afterClosed: Subject = new Subject(); + private _afterClosed = new Subject(); /** Subject for notifying the user that the snack bar has opened and appeared. */ - private _afterOpened: Subject; + private _afterOpened = new Subject(); /** Subject for notifying the user that the snack bar action was called. */ - private _onAction: Subject = new Subject(); + private _onAction = new Subject(); /** * Timeout ID for the duration setTimeout call. Used to clear the timeout if the snackbar is @@ -55,19 +55,19 @@ export class MdSnackBarRef { 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) { diff --git a/src/lib/snack-bar/snack-bar.spec.ts b/src/lib/snack-bar/snack-bar.spec.ts index 730e55cf66f4..74a770d16adb 100644 --- a/src/lib/snack-bar/snack-bar.spec.ts +++ b/src/lib/snack-bar/snack-bar.spec.ts @@ -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); + })); + }); });