Skip to content

docs(material/checkbox): add checkbox harness example #20839

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 28, 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/checkbox/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ ng_module(
]),
module_name = "@angular/components-examples/material/checkbox",
deps = [
"//src/cdk/testing",
"//src/cdk/testing/testbed",
"//src/material/card",
"//src/material/checkbox",
"//src/material/checkbox/testing",
"//src/material/radio",
"@npm//@angular/forms",
"@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,11 @@
<mat-checkbox
required
[checked]="true"
name="first-name"
value="first-value"
aria-label="First checkbox">
First
</mat-checkbox>
<mat-checkbox indeterminate="true" [disabled]="disabled" aria-label="Second checkbox">
Second
</mat-checkbox>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {MatCheckboxHarness} from '@angular/material/checkbox/testing';
import {HarnessLoader} from '@angular/cdk/testing';
import {ReactiveFormsModule} from '@angular/forms';
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
from '@angular/platform-browser-dynamic/testing';
import {MatCheckboxModule} from '@angular/material/checkbox';
import {CheckboxHarnessExample} from './checkbox-harness-example';

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

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

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatCheckboxModule, ReactiveFormsModule],
declarations: [CheckboxHarnessExample]
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(CheckboxHarnessExample);
fixture.detectChanges();
loader = TestbedHarnessEnvironment.loader(fixture);
});

it('should load checkbox with name', async () => {
const checkboxes = await loader.getAllHarnesses(MatCheckboxHarness.with({name: 'first-name'}));
expect(checkboxes.length).toBe(1);
expect(await checkboxes[0].getLabelText()).toBe('First');
});

it('should get checked state', async () => {
const [checkedCheckbox, uncheckedCheckbox] = await loader.getAllHarnesses(MatCheckboxHarness);
expect(await checkedCheckbox.isChecked()).toBe(true);
expect(await uncheckedCheckbox.isChecked()).toBe(false);
});

it('should get name', async () => {
const checkbox = await loader.getHarness(MatCheckboxHarness.with({label: 'First'}));
expect(await checkbox.getName()).toBe('first-name');
});

it('should get label text', async () => {
const [firstCheckbox, secondCheckbox] = await loader.getAllHarnesses(MatCheckboxHarness);
expect(await firstCheckbox.getLabelText()).toBe('First');
expect(await secondCheckbox.getLabelText()).toBe('Second');
});

it('should toggle checkbox', async () => {
fixture.componentInstance.disabled = false;
const [checkedCheckbox, uncheckedCheckbox] = await loader.getAllHarnesses(MatCheckboxHarness);
await checkedCheckbox.toggle();
await uncheckedCheckbox.toggle();
expect(await checkedCheckbox.isChecked()).toBe(false);
expect(await uncheckedCheckbox.isChecked()).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Component} from '@angular/core';

/**
* @title Testing with MatCheckboxHarness
*/
@Component({
selector: 'checkbox-harness-example',
templateUrl: 'checkbox-harness-example.html',
})
export class CheckboxHarnessExample {
disabled = true;
}
3 changes: 3 additions & 0 deletions src/components-examples/material/checkbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import {MatCheckboxModule} from '@angular/material/checkbox';
import {MatRadioModule} from '@angular/material/radio';
import {CheckboxConfigurableExample} from './checkbox-configurable/checkbox-configurable-example';
import {CheckboxOverviewExample} from './checkbox-overview/checkbox-overview-example';
import {CheckboxHarnessExample} from './checkbox-harness/checkbox-harness-example';

export {
CheckboxConfigurableExample,
CheckboxOverviewExample,
CheckboxHarnessExample,
};

const EXAMPLES = [
CheckboxConfigurableExample,
CheckboxOverviewExample,
CheckboxHarnessExample,
];

@NgModule({
Expand Down