Skip to content

Commit 77b67c6

Browse files
authored
docs(material/toolbar): add harness example for toolbar (#21084)
1 parent 2a9919a commit 77b67c6

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

src/components-examples/material/toolbar/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/toolbar",
1313
deps = [
14+
"//src/cdk/testing",
15+
"//src/cdk/testing/testbed",
1416
"//src/material/button",
1517
"//src/material/icon",
1618
"//src/material/toolbar",
19+
"//src/material/toolbar/testing",
20+
"@npm//@angular/platform-browser",
21+
"@npm//@angular/platform-browser-dynamic",
22+
"@npm//@types/jasmine",
1723
],
1824
)
1925

src/components-examples/material/toolbar/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import {MatToolbarModule} from '@angular/material/toolbar';
55
import {ToolbarBasicExample} from './toolbar-basic/toolbar-basic-example';
66
import {ToolbarMultirowExample} from './toolbar-multirow/toolbar-multirow-example';
77
import {ToolbarOverviewExample} from './toolbar-overview/toolbar-overview-example';
8+
import {ToolbarHarnessExample} from './toolbar-harness/toolbar-harness-example';
89

910
export {
1011
ToolbarBasicExample,
12+
ToolbarHarnessExample,
1113
ToolbarMultirowExample,
1214
ToolbarOverviewExample,
1315
};
1416

1517
const EXAMPLES = [
1618
ToolbarBasicExample,
19+
ToolbarHarnessExample,
1720
ToolbarMultirowExample,
1821
ToolbarOverviewExample,
1922
];
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<mat-toolbar><span>My App</span></mat-toolbar>
2+
<mat-toolbar>
3+
<mat-toolbar-row><span>Row 1</span></mat-toolbar-row>
4+
<mat-toolbar-row><span>Row 2</span>
5+
<button mat-button>
6+
Button 1
7+
</button>
8+
<button mat-button>
9+
Button 2
10+
</button>
11+
</mat-toolbar-row>
12+
</mat-toolbar>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatToolbarHarness} from '@angular/material/toolbar/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {MatToolbarModule} from '@angular/material/toolbar';
8+
import {ToolbarHarnessExample} from './toolbar-harness-example';
9+
import {MatIconModule} from '@angular/material/icon';
10+
11+
describe('ToolbarHarnessExample', () => {
12+
let fixture: ComponentFixture<ToolbarHarnessExample>;
13+
let loader: HarnessLoader;
14+
15+
beforeAll(() => {
16+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
17+
});
18+
19+
beforeEach(
20+
waitForAsync(() => {
21+
TestBed.configureTestingModule({
22+
imports: [MatToolbarModule, MatIconModule],
23+
declarations: [ToolbarHarnessExample]
24+
}).compileComponents();
25+
fixture = TestBed.createComponent(ToolbarHarnessExample);
26+
fixture.detectChanges();
27+
loader = TestbedHarnessEnvironment.loader(fixture);
28+
})
29+
);
30+
31+
it('should find all toolbars', async () => {
32+
const toolbars = await loader.getAllHarnesses(MatToolbarHarness);
33+
34+
expect(toolbars.length).toBe(2);
35+
});
36+
37+
it('should find toolbar with text', async () => {
38+
const toolbars = await loader.getAllHarnesses(MatToolbarHarness.with({text: 'My App'}));
39+
40+
expect(toolbars.length).toBe(1);
41+
expect(await toolbars[0].hasMultipleRows()).toBeFalse();
42+
});
43+
44+
it('should find toolbar with regex', async () => {
45+
const toolbars = await loader.getAllHarnesses(MatToolbarHarness.with({text: /Row/}));
46+
47+
expect(toolbars.length).toBe(1);
48+
expect(await toolbars[0].hasMultipleRows()).toBeTrue();
49+
});
50+
51+
it('should get toolbar text', async () => {
52+
const toolbars = await loader.getAllHarnesses(MatToolbarHarness);
53+
54+
expect(await toolbars[0].getRowsAsText()).toEqual(['My App']);
55+
expect(await toolbars[1].getRowsAsText()).toEqual([
56+
'Row 1',
57+
'Row 2 Button 1 Button 2'
58+
]);
59+
});
60+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Testing with MatToolbarHarness
5+
*/
6+
@Component({
7+
selector: 'toolbar-harness-example',
8+
templateUrl: 'toolbar-harness-example.html',
9+
})
10+
export class ToolbarHarnessExample {}

0 commit comments

Comments
 (0)