Skip to content

docs(material/button): add button harness example #20722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components-examples/material/button/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ ng_module(
]),
module_name = "@angular/components-examples/material/button",
deps = [
"//src/cdk/testing",
"//src/cdk/testing/testbed",
"//src/material/button",
"//src/material/button/testing",
"//src/material/divider",
"//src/material/icon",
"@npm//@angular/platform-browser",
"@npm//@angular/platform-browser-dynamic",
"@npm//@types/jasmine",
],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<button id="basic" type="button" mat-button (click)="clicked = true">
Basic button
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {MatButtonHarness} from '@angular/material/button/testing';
import {HarnessLoader} from '@angular/cdk/testing';
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
from '@angular/platform-browser-dynamic/testing';
import {MatButtonModule} from '@angular/material/button';
import {ButtonHarnessExample} from './button-harness-example';

describe('ButtonHarnessExample', () => {
let fixture: ComponentFixture<ButtonHarnessExample>;
let loader: HarnessLoader;
let buttonHarness = MatButtonHarness;

beforeAll(() => {
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
});

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatButtonModule],
declarations: [ButtonHarnessExample]
}).compileComponents();
fixture = TestBed.createComponent(ButtonHarnessExample);
fixture.detectChanges();
loader = TestbedHarnessEnvironment.loader(fixture);
})
);

it('should load all button harnesses', async () => {
const buttons = await loader.getAllHarnesses(MatButtonHarness);
expect(buttons.length).toBe(1);
}
);

it('should load button with exact text', async () => {
const buttons = await loader.getAllHarnesses(buttonHarness.with({text: 'Basic button'}));
expect(buttons.length).toBe(1);
expect(await buttons[0].getText()).toBe('Basic button');
});

it('should click a button', async () => {
const button = await loader.getHarness(buttonHarness.with({text: 'Basic button'}));
await button.click();
expect(fixture.componentInstance.clicked).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Component} from '@angular/core';

/**
* @title Testing with MatButtonHarness
*/
@Component({
selector: 'button-harness-example',
templateUrl: 'button-harness-example.html',
})
export class ButtonHarnessExample {
clicked = false;
}
3 changes: 3 additions & 0 deletions src/components-examples/material/button/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import {MatDividerModule} from '@angular/material/divider';
import {MatIconModule} from '@angular/material/icon';
import {ButtonOverviewExample} from './button-overview/button-overview-example';
import {ButtonTypesExample} from './button-types/button-types-example';
import {ButtonHarnessExample} from './button-harness/button-harness-example';

export {
ButtonOverviewExample,
ButtonTypesExample,
ButtonHarnessExample,
};

const EXAMPLES = [
ButtonOverviewExample,
ButtonTypesExample,
ButtonHarnessExample,
];

@NgModule({
Expand Down
3 changes: 3 additions & 0 deletions tools/example-module/generate-example-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ function analyzeExamples(sourceFiles: string[], baseDir: string): AnalyzedExampl
if (primaryComponent.styleUrls) {
example.files.push(...primaryComponent.styleUrls);
}
if (primaryComponent.componentName.includes('Harness')) {
example.files.push(primaryComponent.selector + '.spec.ts');
}

if (secondaryComponents.length) {
for (const meta of secondaryComponents) {
Expand Down