Skip to content

Commit 2d79ab7

Browse files
authored
docs(material/button): add harness example (#20722)
1 parent 06294d1 commit 2d79ab7

File tree

6 files changed

+75
-0
lines changed

6 files changed

+75
-0
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/button",
1313
deps = [
14+
"//src/cdk/testing",
15+
"//src/cdk/testing/testbed",
1416
"//src/material/button",
17+
"//src/material/button/testing",
1518
"//src/material/divider",
1619
"//src/material/icon",
20+
"@npm//@angular/platform-browser",
21+
"@npm//@angular/platform-browser-dynamic",
22+
"@npm//@types/jasmine",
1723
],
1824
)
1925

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<button id="basic" type="button" mat-button (click)="clicked = true">
2+
Basic button
3+
</button>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatButtonHarness} from '@angular/material/button/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {MatButtonModule} from '@angular/material/button';
8+
import {ButtonHarnessExample} from './button-harness-example';
9+
10+
describe('ButtonHarnessExample', () => {
11+
let fixture: ComponentFixture<ButtonHarnessExample>;
12+
let loader: HarnessLoader;
13+
let buttonHarness = MatButtonHarness;
14+
15+
beforeAll(() => {
16+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
17+
});
18+
19+
beforeEach(
20+
waitForAsync(() => {
21+
TestBed.configureTestingModule({
22+
imports: [MatButtonModule],
23+
declarations: [ButtonHarnessExample]
24+
}).compileComponents();
25+
fixture = TestBed.createComponent(ButtonHarnessExample);
26+
fixture.detectChanges();
27+
loader = TestbedHarnessEnvironment.loader(fixture);
28+
})
29+
);
30+
31+
it('should load all button harnesses', async () => {
32+
const buttons = await loader.getAllHarnesses(MatButtonHarness);
33+
expect(buttons.length).toBe(1);
34+
}
35+
);
36+
37+
it('should load button with exact text', async () => {
38+
const buttons = await loader.getAllHarnesses(buttonHarness.with({text: 'Basic button'}));
39+
expect(buttons.length).toBe(1);
40+
expect(await buttons[0].getText()).toBe('Basic button');
41+
});
42+
43+
it('should click a button', async () => {
44+
const button = await loader.getHarness(buttonHarness.with({text: 'Basic button'}));
45+
await button.click();
46+
expect(fixture.componentInstance.clicked).toBe(true);
47+
});
48+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Testing with MatButtonHarness
5+
*/
6+
@Component({
7+
selector: 'button-harness-example',
8+
templateUrl: 'button-harness-example.html',
9+
})
10+
export class ButtonHarnessExample {
11+
clicked = false;
12+
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ import {MatDividerModule} from '@angular/material/divider';
44
import {MatIconModule} from '@angular/material/icon';
55
import {ButtonOverviewExample} from './button-overview/button-overview-example';
66
import {ButtonTypesExample} from './button-types/button-types-example';
7+
import {ButtonHarnessExample} from './button-harness/button-harness-example';
78

89
export {
910
ButtonOverviewExample,
1011
ButtonTypesExample,
12+
ButtonHarnessExample,
1113
};
1214

1315
const EXAMPLES = [
1416
ButtonOverviewExample,
1517
ButtonTypesExample,
18+
ButtonHarnessExample,
1619
];
1720

1821
@NgModule({

tools/example-module/generate-example-module.ts

+3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ function analyzeExamples(sourceFiles: string[], baseDir: string): AnalyzedExampl
131131
if (primaryComponent.styleUrls) {
132132
example.files.push(...primaryComponent.styleUrls);
133133
}
134+
if (primaryComponent.componentName.includes('Harness')) {
135+
example.files.push(primaryComponent.selector + '.spec.ts');
136+
}
134137

135138
if (secondaryComponents.length) {
136139
for (const meta of secondaryComponents) {

0 commit comments

Comments
 (0)