Skip to content

Commit 37d7471

Browse files
committed
docs(material/button): add harness example
1 parent 50d3f29 commit 37d7471

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

src/components-examples/material/button/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/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

Lines changed: 3 additions & 0 deletions
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>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
beforeAll(() => {
15+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
16+
});
17+
beforeEach(
18+
waitForAsync(() => {
19+
TestBed.configureTestingModule({
20+
imports: [MatButtonModule],
21+
declarations: [ButtonHarnessExample]
22+
}).compileComponents();
23+
fixture = TestBed.createComponent(ButtonHarnessExample);
24+
fixture.detectChanges();
25+
loader = TestbedHarnessEnvironment.loader(fixture);
26+
})
27+
);
28+
it('should load all button harnesses', async () => {
29+
const buttons = await loader.getAllHarnesses(MatButtonHarness);
30+
expect(buttons.length).toBe(1);
31+
}
32+
);
33+
it('should load button with exact text', async () => {
34+
const buttons = await loader.getAllHarnesses(buttonHarness.with({text: 'Basic button'}));
35+
expect(buttons.length).toBe(1);
36+
expect(await buttons[0].getText()).toBe('Basic button');
37+
});
38+
39+
it('should click a button', async () => {
40+
const button = await loader.getHarness(buttonHarness.with({text: 'Basic button'}));
41+
await button.click();
42+
expect(fixture.componentInstance.clicked).toBe(true);
43+
});
44+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Button harness
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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)