Skip to content

feat(material/snackbar): add isDismissed harness method #18766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/material/snack-bar/snack-bar-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ export class MatSnackBarContainer extends BasePortalOutlet implements OnDestroy
// where multiple snack bars are opened in quick succession (e.g. two consecutive calls to
// `MatSnackBar.open`).
this._animationState = 'hidden';

// Mark this element with an 'exit' attribute to indicate that the snackbar has
// been dismissed and will soon be removed from the DOM. This is used by the snackbar
// test harness.
this._elementRef.nativeElement.setAttribute('mat-exit', '');

return this._onExit;
}

Expand Down
4 changes: 4 additions & 0 deletions src/material/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ describe('MatSnackBar', () => {
snackBarRef.afterDismissed().subscribe({complete: dismissCompleteSpy});

snackBarRef.dismiss();
const messageElement = overlayContainerElement.querySelector('snack-bar-container')!;
expect (messageElement.hasAttribute('mat-exit'))
.toBe(true, 'Expected the snackbar container to have the "exit" attribute upon dismiss');

viewContainerFixture.detectChanges(); // Run through animations for dismissal
flush();

Expand Down
5 changes: 5 additions & 0 deletions src/material/snack-bar/testing/shared.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,13 @@ export function runHarnessTests(
let actionCount = 0;
snackBarRef.onAction().subscribe(() => actionCount++);

expect(await snackBar.isDismissed())
.toBe(false, 'The snackbar should be present in the DOM before dismiss');

await snackBar.dismissWithAction();
expect(actionCount).toBe(1);
expect(await snackBar.isDismissed())
.toBe(true, 'The snackbar should be absent from the DOM after dismiss');

fixture.componentInstance.openSimple('No action');
snackBar = await loader.getHarness(snackBarHarness);
Expand Down
20 changes: 18 additions & 2 deletions src/material/snack-bar/testing/snack-bar-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,29 @@ export class MatSnackBarHarness extends ComponentHarness {
return (await this._simpleSnackBarMessage()).text();
}

/** Gets whether the snack-bar has been dismissed. */
async isDismissed(): Promise<boolean> {
// We consider the snackbar dismissed if it's not in the DOM. We can assert that the
// element isn't in the DOM by seeing that its width and height are zero.

const host = await this.host();
const [exit, dimensions] = await Promise.all([
// The snackbar container is marked with the "exit" attribute after it has been dismissed
// but before the animation has finished (after which it's removed from the DOM).
host.getAttribute('mat-exit'),
host.getDimensions(),
]);

return exit != null || (!!dimensions && dimensions.height === 0 && dimensions.width === 0);
}

/**
* Asserts that the current snack-bar does not use custom content. Promise rejects if
* custom content is used.
*/
private async _assertSimpleSnackBar(): Promise<void> {
if (!await this._isSimpleSnackBar()) {
throw new Error('Method cannot be used for snack-bar with custom content.');
throw Error('Method cannot be used for snack-bar with custom content.');
}
}

Expand All @@ -91,7 +107,7 @@ export class MatSnackBarHarness extends ComponentHarness {
private async _assertSimpleSnackBarWithAction(): Promise<void> {
await this._assertSimpleSnackBar();
if (!await this.hasAction()) {
throw new Error('Method cannot be used for standard snack-bar without action.');
throw Error('Method cannot be used for standard snack-bar without action.');
}
}

Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/snack-bar/testing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export declare class MatSnackBarHarness extends ComponentHarness {
getMessage(): Promise<string>;
getRole(): Promise<'alert' | 'status' | null>;
hasAction(): Promise<boolean>;
isDismissed(): Promise<boolean>;
static hostSelector: string;
static with(options?: SnackBarHarnessFilters): HarnessPredicate<MatSnackBarHarness>;
}
Expand Down