Skip to content

Commit

Permalink
fix(material/snack-bar): clear timeout upon dismiss with action (#22485)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
PowerKiKi authored and wagnermaciel committed Apr 19, 2021
1 parent c9b9d8a commit f3dcfa9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/material-experimental/mdc-snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/material/snack-bar/snack-bar-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class MatSnackBarRef<T> {
this._onAction.next();
this._onAction.complete();
}
clearTimeout(this._durationTimeoutId);
}


Expand Down
14 changes: 14 additions & 0 deletions src/material/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f3dcfa9

Please sign in to comment.