From 1e749efdea9c861a4b9519c9a37dc1682910b34b Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Thu, 15 Apr 2021 12:13:09 +0900 Subject: [PATCH] fix(material/snack-bar): clear timeout upon dismiss with action This is very similar to #4860 where a programmatic dismiss cancelled the timeout. This patch is for a manual dismiss via clicking on the snackbar action. It is especially useful in e2e tests with Protractor, because Protractor always wait for Angular to become stable. With this patch Protractor does not need to wait if the action is clicked, saving potentially a lot of time in repetitive e2e tests. --- .../mdc-snack-bar/snack-bar.spec.ts | 14 ++++++++++++++ src/material/snack-bar/snack-bar-ref.ts | 1 + src/material/snack-bar/snack-bar.spec.ts | 14 ++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/src/material-experimental/mdc-snack-bar/snack-bar.spec.ts b/src/material-experimental/mdc-snack-bar/snack-bar.spec.ts index 16a7e708a467..dddd031cedc1 100644 --- a/src/material-experimental/mdc-snack-bar/snack-bar.spec.ts +++ b/src/material-experimental/mdc-snack-bar/snack-bar.spec.ts @@ -466,6 +466,20 @@ describe('MatSnackBar', () => { expect(viewContainerFixture.isStable()).toBe(true); })); + it('should clear the dismiss timeout when dismissed with action', fakeAsync(() => { + let config = new MatSnackBarConfig(); + config.duration = 1000; + const snackBarRef = snackBar.open('content', 'test', config); + + setTimeout(() => snackBarRef.dismissWithAction(), 500); + + tick(600); + viewContainerFixture.detectChanges(); + tick(); + + expect(viewContainerFixture.isStable()).toBe(true); + })); + it('should add extra classes to the container', () => { snackBar.open(simpleMessage, simpleActionLabel, { panelClass: ['one', 'two'] }); viewContainerFixture.detectChanges(); diff --git a/src/material/snack-bar/snack-bar-ref.ts b/src/material/snack-bar/snack-bar-ref.ts index a25805494915..ecb5e76018ec 100644 --- a/src/material/snack-bar/snack-bar-ref.ts +++ b/src/material/snack-bar/snack-bar-ref.ts @@ -74,6 +74,7 @@ export class MatSnackBarRef { this._onAction.next(); this._onAction.complete(); } + clearTimeout(this._durationTimeoutId); } diff --git a/src/material/snack-bar/snack-bar.spec.ts b/src/material/snack-bar/snack-bar.spec.ts index a44876348116..dd893e1f97e0 100644 --- a/src/material/snack-bar/snack-bar.spec.ts +++ b/src/material/snack-bar/snack-bar.spec.ts @@ -525,6 +525,20 @@ describe('MatSnackBar', () => { expect(viewContainerFixture.isStable()).toBe(true); })); + it('should clear the dismiss timeout when dismissed with action', fakeAsync(() => { + let config = new MatSnackBarConfig(); + config.duration = 1000; + const snackBarRef = snackBar.open('content', 'test', config); + + setTimeout(() => snackBarRef.dismissWithAction(), 500); + + tick(600); + viewContainerFixture.detectChanges(); + tick(); + + expect(viewContainerFixture.isStable()).toBe(true); + })); + it('should add extra classes to the container', () => { snackBar.open(simpleMessage, simpleActionLabel, { panelClass: ['one', 'two'] }); viewContainerFixture.detectChanges();