Skip to content

Commit 1a1528d

Browse files
PowerKiKiwagnermaciel
authored andcommitted
fix(material/snack-bar): clear timeout upon dismiss with action (#22485)
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. (cherry picked from commit c08e2c9)
1 parent 1399e3d commit 1a1528d

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/material-experimental/mdc-snack-bar/snack-bar.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,20 @@ describe('MatSnackBar', () => {
466466
expect(viewContainerFixture.isStable()).toBe(true);
467467
}));
468468

469+
it('should clear the dismiss timeout when dismissed with action', fakeAsync(() => {
470+
let config = new MatSnackBarConfig();
471+
config.duration = 1000;
472+
const snackBarRef = snackBar.open('content', 'test', config);
473+
474+
setTimeout(() => snackBarRef.dismissWithAction(), 500);
475+
476+
tick(600);
477+
viewContainerFixture.detectChanges();
478+
tick();
479+
480+
expect(viewContainerFixture.isStable()).toBe(true);
481+
}));
482+
469483
it('should add extra classes to the container', () => {
470484
snackBar.open(simpleMessage, simpleActionLabel, { panelClass: ['one', 'two'] });
471485
viewContainerFixture.detectChanges();

src/material/snack-bar/snack-bar-ref.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class MatSnackBarRef<T> {
7474
this._onAction.next();
7575
this._onAction.complete();
7676
}
77+
clearTimeout(this._durationTimeoutId);
7778
}
7879

7980

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,20 @@ describe('MatSnackBar', () => {
525525
expect(viewContainerFixture.isStable()).toBe(true);
526526
}));
527527

528+
it('should clear the dismiss timeout when dismissed with action', fakeAsync(() => {
529+
let config = new MatSnackBarConfig();
530+
config.duration = 1000;
531+
const snackBarRef = snackBar.open('content', 'test', config);
532+
533+
setTimeout(() => snackBarRef.dismissWithAction(), 500);
534+
535+
tick(600);
536+
viewContainerFixture.detectChanges();
537+
tick();
538+
539+
expect(viewContainerFixture.isStable()).toBe(true);
540+
}));
541+
528542
it('should add extra classes to the container', () => {
529543
snackBar.open(simpleMessage, simpleActionLabel, { panelClass: ['one', 'two'] });
530544
viewContainerFixture.detectChanges();

0 commit comments

Comments
 (0)