From fcd29c8a671089174ed8993cef9b456d78bdc7a6 Mon Sep 17 00:00:00 2001 From: Jeremy Elbourn Date: Wed, 9 Nov 2016 16:29:51 -0800 Subject: [PATCH] fix(snackbar): snacksbars sometimes don't get removed (#1795) --- src/lib/snack-bar/snack-bar.spec.ts | 17 +++++++++++++++++ src/lib/snack-bar/snack-bar.ts | 5 ++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lib/snack-bar/snack-bar.spec.ts b/src/lib/snack-bar/snack-bar.spec.ts index 90fced2e3a1f..778440b0cdc3 100644 --- a/src/lib/snack-bar/snack-bar.spec.ts +++ b/src/lib/snack-bar/snack-bar.spec.ts @@ -219,6 +219,23 @@ describe('MdSnackBar', () => { }); }); })); + + it('should remove past snackbars when opening new snackbars', async(() => { + snackBar.open('First snackbar'); + viewContainerFixture.detectChanges(); + + snackBar.open('Second snackbar'); + viewContainerFixture.detectChanges(); + + viewContainerFixture.whenStable().then(() => { + snackBar.open('Third snackbar'); + viewContainerFixture.detectChanges(); + + viewContainerFixture.whenStable().then(() => { + expect(overlayContainerElement.textContent.trim()).toBe('Third snackbar'); + }); + }); + })); }); @Directive({selector: 'dir-with-view-container'}) diff --git a/src/lib/snack-bar/snack-bar.ts b/src/lib/snack-bar/snack-bar.ts index 8ba710d8665c..23b63158c37b 100644 --- a/src/lib/snack-bar/snack-bar.ts +++ b/src/lib/snack-bar/snack-bar.ts @@ -48,7 +48,10 @@ export class MdSnackBar { // When the snackbar is dismissed, clear the reference to it. snackBarRef.afterDismissed().subscribe(() => { - this._snackBarRef = null; + // Clear the snackbar ref if it hasn't already been replaced by a newer snackbar. + if (this._snackBarRef == snackBarRef) { + this._snackBarRef = null; + } }); // If a snack bar is already in view, dismiss it and enter the new snack bar after exit