Skip to content

Commit e1514fb

Browse files
committed
test(material/snack-bar): combine shared tests
Since we only have one module, we don't need separate shared tests anymore.
1 parent 5050b3e commit e1514fb

File tree

3 files changed

+114
-182
lines changed

3 files changed

+114
-182
lines changed

src/material/snack-bar/testing/BUILD.bazel

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ filegroup(
2020
)
2121

2222
ng_test_library(
23-
name = "harness_tests_lib",
24-
srcs = ["shared.spec.ts"],
23+
name = "unit_tests_lib",
24+
srcs = glob(["**/*.spec.ts"]),
2525
deps = [
2626
":testing",
2727
"//src/cdk/overlay",
@@ -33,22 +33,6 @@ ng_test_library(
3333
],
3434
)
3535

36-
ng_test_library(
37-
name = "unit_tests_lib",
38-
srcs = glob(
39-
["**/*.spec.ts"],
40-
exclude = ["shared.spec.ts"],
41-
),
42-
deps = [
43-
":harness_tests_lib",
44-
":testing",
45-
"//src/cdk/testing",
46-
"//src/cdk/testing/testbed",
47-
"//src/material/snack-bar",
48-
"@npm//@angular/platform-browser",
49-
],
50-
)
51-
5236
ng_web_test_suite(
5337
name = "unit_tests",
5438
deps = [

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

Lines changed: 0 additions & 155 deletions
This file was deleted.

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

Lines changed: 112 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
import {MatSnackBar, MatSnackBarConfig, MatSnackBarModule} from '@angular/material/snack-bar';
2-
import {runHarnessTests} from './shared.spec';
1+
import {Component, TemplateRef, ViewChild} from '@angular/core';
32
import {ComponentFixture, TestBed} from '@angular/core/testing';
43
import {HarnessLoader} from '@angular/cdk/testing';
5-
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
64
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
7-
import {Component, TemplateRef, ViewChild} from '@angular/core';
5+
import {MatSnackBar, MatSnackBarConfig, MatSnackBarModule} from '@angular/material/snack-bar';
6+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
87
import {MatSnackBarHarness} from './snack-bar-harness';
98

10-
describe('MDC-based MatSnackBarHarness', () => {
11-
runHarnessTests(MatSnackBarModule, MatSnackBar, MatSnackBarHarness);
12-
});
13-
14-
describe('MDC-based MatSnackBarHarness (MDC only behavior)', () => {
9+
describe('MatSnackBarHarness', () => {
1510
let fixture: ComponentFixture<SnackbarHarnessTest>;
1611
let loader: HarnessLoader;
1712

@@ -26,6 +21,110 @@ describe('MDC-based MatSnackBarHarness (MDC only behavior)', () => {
2621
loader = TestbedHarnessEnvironment.documentRootLoader(fixture);
2722
});
2823

24+
it('should load harness for simple snack-bar', async () => {
25+
const snackBarRef = fixture.componentInstance.openSimple('Hello!', '');
26+
let snackBars = await loader.getAllHarnesses(MatSnackBarHarness);
27+
28+
expect(snackBars.length).toBe(1);
29+
30+
snackBarRef.dismiss();
31+
snackBars = await loader.getAllHarnesses(MatSnackBarHarness);
32+
expect(snackBars.length).toBe(0);
33+
});
34+
35+
it('should load harness for custom snack-bar', async () => {
36+
const snackBarRef = fixture.componentInstance.openCustom();
37+
let snackBars = await loader.getAllHarnesses(MatSnackBarHarness);
38+
39+
expect(snackBars.length).toBe(1);
40+
41+
snackBarRef.dismiss();
42+
snackBars = await loader.getAllHarnesses(MatSnackBarHarness);
43+
expect(snackBars.length).toBe(0);
44+
});
45+
46+
it('should load snack-bar harness by selector', async () => {
47+
fixture.componentInstance.openSimple('Hello!', '', {panelClass: 'my-snack-bar'});
48+
const snackBars = await loader.getAllHarnesses(
49+
MatSnackBarHarness.with({
50+
selector: '.my-snack-bar',
51+
}),
52+
);
53+
expect(snackBars.length).toBe(1);
54+
});
55+
56+
it('should be able to get role of snack-bar', async () => {
57+
// Get role is now deprecated, so it should always return null.
58+
fixture.componentInstance.openCustom();
59+
let snackBar = await loader.getHarness(MatSnackBarHarness);
60+
expect(await snackBar.getRole()).toBe(null);
61+
62+
fixture.componentInstance.openCustom({politeness: 'polite'});
63+
snackBar = await loader.getHarness(MatSnackBarHarness);
64+
expect(await snackBar.getRole()).toBe(null);
65+
66+
fixture.componentInstance.openCustom({politeness: 'off'});
67+
snackBar = await loader.getHarness(MatSnackBarHarness);
68+
expect(await snackBar.getRole()).toBe(null);
69+
});
70+
71+
it('should be able to get aria-live of snack-bar', async () => {
72+
fixture.componentInstance.openCustom();
73+
let snackBar = await loader.getHarness(MatSnackBarHarness);
74+
expect(await snackBar.getAriaLive()).toBe('assertive');
75+
76+
fixture.componentInstance.openCustom({politeness: 'polite'});
77+
snackBar = await loader.getHarness(MatSnackBarHarness);
78+
expect(await snackBar.getAriaLive()).toBe('polite');
79+
80+
fixture.componentInstance.openCustom({politeness: 'off'});
81+
snackBar = await loader.getHarness(MatSnackBarHarness);
82+
expect(await snackBar.getAriaLive()).toBe('off');
83+
});
84+
85+
it('should be able to get message of simple snack-bar', async () => {
86+
fixture.componentInstance.openSimple('Subscribed to newsletter.');
87+
let snackBar = await loader.getHarness(MatSnackBarHarness);
88+
expect(await snackBar.getMessage()).toBe('Subscribed to newsletter.');
89+
});
90+
91+
it('should be able to get action description of simple snack-bar', async () => {
92+
fixture.componentInstance.openSimple('Hello', 'Unsubscribe');
93+
let snackBar = await loader.getHarness(MatSnackBarHarness);
94+
expect(await snackBar.getActionDescription()).toBe('Unsubscribe');
95+
});
96+
97+
it('should be able to check whether simple snack-bar has action', async () => {
98+
fixture.componentInstance.openSimple('With action', 'Unsubscribe');
99+
let snackBar = await loader.getHarness(MatSnackBarHarness);
100+
expect(await snackBar.hasAction()).toBe(true);
101+
102+
fixture.componentInstance.openSimple('No action');
103+
snackBar = await loader.getHarness(MatSnackBarHarness);
104+
expect(await snackBar.hasAction()).toBe(false);
105+
});
106+
107+
it('should be able to dismiss simple snack-bar with action', async () => {
108+
const snackBarRef = fixture.componentInstance.openSimple('With action', 'Unsubscribe');
109+
let snackBar = await loader.getHarness(MatSnackBarHarness);
110+
let actionCount = 0;
111+
snackBarRef.onAction().subscribe(() => actionCount++);
112+
113+
expect(await snackBar.isDismissed())
114+
.withContext('The snackbar should be present in the DOM before dismiss')
115+
.toBe(false);
116+
117+
await snackBar.dismissWithAction();
118+
expect(actionCount).toBe(1);
119+
expect(await snackBar.isDismissed())
120+
.withContext('The snackbar should be absent from the DOM after dismiss')
121+
.toBe(true);
122+
123+
fixture.componentInstance.openSimple('No action');
124+
snackBar = await loader.getHarness(MatSnackBarHarness);
125+
await expectAsync(snackBar.dismissWithAction()).toBeRejectedWithError(/without an action/);
126+
});
127+
29128
it('should be able to get message of a snack-bar with custom content', async () => {
30129
fixture.componentInstance.openCustom();
31130
let snackBar = await loader.getHarness(MatSnackBarHarness);
@@ -74,6 +173,10 @@ class SnackbarHarnessTest {
74173

75174
constructor(public snackBar: MatSnackBar) {}
76175

176+
openSimple(message: string, action = '', config?: MatSnackBarConfig) {
177+
return this.snackBar.open(message, action, config);
178+
}
179+
77180
openCustom(config?: MatSnackBarConfig) {
78181
return this.snackBar.openFromTemplate(this.customTmpl, config);
79182
}

0 commit comments

Comments
 (0)