Skip to content

Commit 3c5b632

Browse files
authored
fix(snackbar): always clear ref when dismissing (#1773)
1 parent 2b913de commit 3c5b632

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/lib/snack-bar/snack-bar.spec.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
import {
2-
inject,
3-
async,
4-
ComponentFixture,
5-
TestBed,
6-
} from '@angular/core/testing';
7-
import {
8-
NgModule,
9-
Component,
10-
Directive,
11-
ViewChild,
12-
ViewContainerRef
13-
} from '@angular/core';
1+
import {inject, async, ComponentFixture, TestBed} from '@angular/core/testing';
2+
import {NgModule, Component, Directive, ViewChild, ViewContainerRef} from '@angular/core';
143
import {MdSnackBar, MdSnackBarModule} from './snack-bar';
154
import {OverlayContainer, MdLiveAnnouncer} from '../core';
165
import {MdSnackBarConfig} from './snack-bar-config';
@@ -194,6 +183,26 @@ describe('MdSnackBar', () => {
194183
.toBe('visible', `Expected the animation state would be 'visible'.`);
195184
});
196185
}));
186+
187+
it('should open a new snackbar after dismissing a previous snackbar', async(() => {
188+
let config = new MdSnackBarConfig(testViewContainerRef);
189+
let snackBarRef = snackBar.open(simpleMessage, 'DISMISS', config);
190+
viewContainerFixture.detectChanges();
191+
192+
snackBarRef.dismiss();
193+
viewContainerFixture.detectChanges();
194+
195+
// Wait for the snackbar dismiss animation to finish.
196+
viewContainerFixture.whenStable().then(() => {
197+
snackBarRef = snackBar.open('Second snackbar', 'DISMISS', config);
198+
viewContainerFixture.detectChanges();
199+
200+
// Wait for the snackbar open animation to finish.
201+
viewContainerFixture.whenStable().then(() => {
202+
expect(snackBarRef.containerInstance.animationState).toBe('visible');
203+
});
204+
});
205+
}));
197206
});
198207

199208
@Directive({selector: 'dir-with-view-container'})

src/lib/snack-bar/snack-bar.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ export class MdSnackBar {
4545
let snackBarContainer = this._attachSnackBarContainer(overlayRef, config);
4646
let mdSnackBarRef = this._attachSnackbarContent(component, snackBarContainer, overlayRef);
4747

48+
// When the snackbar is dismissed, clear the reference to it.
49+
mdSnackBarRef.afterDismissed().subscribe(() => {
50+
this._snackBarRef = null;
51+
});
52+
4853
// If a snack bar is already in view, dismiss it and enter the new snack bar after exit
4954
// animation is complete.
5055
if (this._snackBarRef) {

0 commit comments

Comments
 (0)