From 523a48e10a98491c8e965249b4abb49731e2a38a Mon Sep 17 00:00:00 2001 From: Jeremy Elbourn Date: Wed, 9 Nov 2016 17:34:19 -0800 Subject: [PATCH] fix(snackbar): remove even if still animating open (#1797) --- src/lib/snack-bar/snack-bar-container.ts | 3 +-- src/lib/snack-bar/snack-bar.spec.ts | 25 +++++++++++++++++++++++- src/lib/snack-bar/snack-bar.ts | 3 +-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/lib/snack-bar/snack-bar-container.ts b/src/lib/snack-bar/snack-bar-container.ts index 189c7f8bc2da..f310223ce863 100644 --- a/src/lib/snack-bar/snack-bar-container.ts +++ b/src/lib/snack-bar/snack-bar-container.ts @@ -92,8 +92,7 @@ export class MdSnackBarContainer extends BasePortalHost { /** Mark snack bar as exited from the view. */ markAsExited(event: AnimationTransitionEvent) { - if (event.fromState === 'visible' && - (event.toState === 'void' || event.toState === 'complete')) { + if (event.toState === 'void' || event.toState === 'complete') { this._ngZone.run(() => { this._onExit.next(); this._onExit.complete(); diff --git a/src/lib/snack-bar/snack-bar.spec.ts b/src/lib/snack-bar/snack-bar.spec.ts index 778440b0cdc3..11b50d8f7ffd 100644 --- a/src/lib/snack-bar/snack-bar.spec.ts +++ b/src/lib/snack-bar/snack-bar.spec.ts @@ -1,4 +1,12 @@ -import {inject, async, ComponentFixture, TestBed} from '@angular/core/testing'; +import { + inject, + async, + ComponentFixture, + TestBed, + fakeAsync, + flushMicrotasks, + tick, +} from '@angular/core/testing'; import {NgModule, Component, Directive, ViewChild, ViewContainerRef} from '@angular/core'; import {MdSnackBar, MdSnackBarModule} from './snack-bar'; import {OverlayContainer, MdLiveAnnouncer} from '../core'; @@ -236,6 +244,21 @@ describe('MdSnackBar', () => { }); }); })); + + it('should remove snackbar if another is shown while its still animating open', fakeAsync(() => { + snackBar.open('First snackbar'); + viewContainerFixture.detectChanges(); + + snackBar.open('Second snackbar'); + viewContainerFixture.detectChanges(); + + // Flush microtasks to make observables run, but don't tick such that any animations would run. + flushMicrotasks(); + expect(overlayContainerElement.textContent.trim()).toBe('Second snackbar'); + + // Let remaining animations run. + tick(500); + })); }); @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 23b63158c37b..a32ee078da33 100644 --- a/src/lib/snack-bar/snack-bar.ts +++ b/src/lib/snack-bar/snack-bar.ts @@ -33,8 +33,7 @@ export class MdSnackBar { /** A reference to the current snack bar in the view. */ private _snackBarRef: MdSnackBarRef; - constructor(private _overlay: Overlay, - private _live: MdLiveAnnouncer) {} + constructor(private _overlay: Overlay, private _live: MdLiveAnnouncer) {} /** * Creates and dispatches a snack bar with a custom component for the content, removing any