Skip to content

Commit 94ad10d

Browse files
authored
docs(material/snack-bar): add harness example for snack bar (#21790)
1 parent 47ea05d commit 94ad10d

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed

src/components-examples/material/snack-bar/BUILD.bazel

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
load("//tools:defaults.bzl", "ng_module")
1+
load("//tools:defaults.bzl", "ng_module", "ng_test_library", "ng_web_test_suite")
22

33
package(default_visibility = ["//visibility:public"])
44

55
ng_module(
66
name = "snack-bar",
7-
srcs = glob(["**/*.ts"]),
7+
srcs = glob(
8+
["**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
811
assets = glob([
912
"**/*.html",
1013
"**/*.css",
1114
]),
1215
module_name = "@angular/components-examples/material/snack-bar",
1316
deps = [
17+
"//src/cdk/testing",
18+
"//src/cdk/testing/testbed",
1419
"//src/material/button",
1520
"//src/material/input",
1621
"//src/material/select",
1722
"//src/material/snack-bar",
23+
"//src/material/snack-bar/testing",
1824
"@npm//@angular/forms",
25+
"@npm//@angular/platform-browser",
26+
"@npm//@angular/platform-browser-dynamic",
27+
"@npm//@types/jasmine",
1928
],
2029
)
2130

@@ -27,3 +36,23 @@ filegroup(
2736
"**/*.ts",
2837
]),
2938
)
39+
40+
ng_test_library(
41+
name = "unit_tests_lib",
42+
srcs = glob(["**/*.spec.ts"]),
43+
deps = [
44+
":snack-bar",
45+
"//src/cdk/testing",
46+
"//src/cdk/testing/testbed",
47+
"//src/material/snack-bar",
48+
"//src/material/snack-bar/testing",
49+
"@npm//@angular/platform-browser",
50+
"@npm//@angular/platform-browser-dynamic",
51+
],
52+
)
53+
54+
ng_web_test_suite(
55+
name = "unit_tests",
56+
exclude_init_script = True,
57+
deps = [":unit_tests_lib"],
58+
)

src/components-examples/material/snack-bar/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ import {
1010
} from './snack-bar-component/snack-bar-component-example';
1111
import {SnackBarOverviewExample} from './snack-bar-overview/snack-bar-overview-example';
1212
import {SnackBarPositionExample} from './snack-bar-position/snack-bar-position-example';
13+
import {SnackBarHarnessExample} from './snack-bar-harness/snack-bar-harness-example';
1314

1415
export {
1516
SnackBarComponentExample,
17+
SnackBarHarnessExample,
1618
SnackBarOverviewExample,
1719
SnackBarPositionExample,
1820
PizzaPartyComponent,
1921
};
2022

2123
const EXAMPLES = [
2224
SnackBarComponentExample,
25+
SnackBarHarnessExample,
2326
SnackBarOverviewExample,
2427
SnackBarPositionExample,
2528
];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<ng-template>Hello from the snackbar</ng-template>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {TestBed, ComponentFixture} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatSnackBarHarness} from '@angular/material/snack-bar/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {
6+
BrowserDynamicTestingModule,
7+
platformBrowserDynamicTesting,
8+
} from '@angular/platform-browser-dynamic/testing';
9+
import {MatSnackBarModule} from '@angular/material/snack-bar';
10+
import {SnackBarHarnessExample} from './snack-bar-harness-example';
11+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
12+
13+
14+
describe('SnackBarHarnessExample', () => {
15+
let fixture: ComponentFixture<SnackBarHarnessExample>;
16+
let loader: HarnessLoader;
17+
18+
beforeAll(() => {
19+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
20+
});
21+
22+
beforeEach(async () => {
23+
await TestBed.configureTestingModule({
24+
imports: [MatSnackBarModule, NoopAnimationsModule],
25+
declarations: [SnackBarHarnessExample]
26+
}).compileComponents();
27+
fixture = TestBed.createComponent(SnackBarHarnessExample);
28+
fixture.detectChanges();
29+
loader = TestbedHarnessEnvironment.documentRootLoader(fixture);
30+
});
31+
32+
it('should load harness for simple snack-bar', async () => {
33+
const snackBarRef = fixture.componentInstance.open('Hi!', '');
34+
let snackBars = await loader.getAllHarnesses(MatSnackBarHarness);
35+
36+
expect(snackBars.length).toBe(1);
37+
38+
snackBarRef.dismiss();
39+
snackBars = await loader.getAllHarnesses(MatSnackBarHarness);
40+
expect(snackBars.length).toBe(0);
41+
});
42+
43+
it('should be able to get message of simple snack-bar', async () => {
44+
fixture.componentInstance.open('Subscribed to newsletter.');
45+
let snackBar = await loader.getHarness(MatSnackBarHarness);
46+
expect(await snackBar.getMessage()).toBe('Subscribed to newsletter.');
47+
});
48+
49+
it('should be able to get action description of simple snack-bar', async () => {
50+
fixture.componentInstance.open('Hello', 'Unsubscribe');
51+
let snackBar = await loader.getHarness(MatSnackBarHarness);
52+
expect(await snackBar.getActionDescription()).toBe('Unsubscribe');
53+
});
54+
55+
it('should be able to check whether simple snack-bar has action', async () => {
56+
fixture.componentInstance.open('With action', 'Unsubscribe');
57+
let snackBar = await loader.getHarness(MatSnackBarHarness);
58+
expect(await snackBar.hasAction()).toBe(true);
59+
60+
fixture.componentInstance.open('No action');
61+
snackBar = await loader.getHarness(MatSnackBarHarness);
62+
expect(await snackBar.hasAction()).toBe(false);
63+
});
64+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {Component} from '@angular/core';
2+
import {MatSnackBar, MatSnackBarConfig} from '@angular/material/snack-bar';
3+
4+
/**
5+
* @title Testing with MatSnackBarHarness
6+
*/
7+
@Component({
8+
selector: 'snack-bar-harness-example',
9+
templateUrl: 'snack-bar-harness-example.html',
10+
})
11+
export class SnackBarHarnessExample {
12+
constructor(readonly snackBar: MatSnackBar) {}
13+
14+
open(message: string, action = '', config?: MatSnackBarConfig) {
15+
return this.snackBar.open(message, action, config);
16+
}
17+
}

0 commit comments

Comments
 (0)