Skip to content

Commit 6d805ab

Browse files
annieywwagnermaciel
authored andcommitted
docs(material/checkbox): add checkbox harness example (#20839)
(cherry picked from commit 65e9ad1)
1 parent ebca237 commit 6d805ab

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/checkbox",
1313
deps = [
14+
"//src/cdk/testing",
15+
"//src/cdk/testing/testbed",
1416
"//src/material/card",
1517
"//src/material/checkbox",
18+
"//src/material/checkbox/testing",
1619
"//src/material/radio",
1720
"@npm//@angular/forms",
21+
"@npm//@angular/platform-browser",
22+
"@npm//@angular/platform-browser-dynamic",
23+
"@npm//@types/jasmine",
1824
],
1925
)
2026

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<mat-checkbox
2+
required
3+
[checked]="true"
4+
name="first-name"
5+
value="first-value"
6+
aria-label="First checkbox">
7+
First
8+
</mat-checkbox>
9+
<mat-checkbox indeterminate="true" [disabled]="disabled" aria-label="Second checkbox">
10+
Second
11+
</mat-checkbox>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatCheckboxHarness} from '@angular/material/checkbox/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {ReactiveFormsModule} from '@angular/forms';
6+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
7+
from '@angular/platform-browser-dynamic/testing';
8+
import {MatCheckboxModule} from '@angular/material/checkbox';
9+
import {CheckboxHarnessExample} from './checkbox-harness-example';
10+
11+
describe('CheckboxHarnessExample', () => {
12+
let fixture: ComponentFixture<CheckboxHarnessExample>;
13+
let loader: HarnessLoader;
14+
15+
beforeAll(() => {
16+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
17+
});
18+
19+
beforeEach(
20+
waitForAsync(() => {
21+
TestBed.configureTestingModule({
22+
imports: [MatCheckboxModule, ReactiveFormsModule],
23+
declarations: [CheckboxHarnessExample]
24+
}).compileComponents();
25+
}));
26+
27+
beforeEach(() => {
28+
fixture = TestBed.createComponent(CheckboxHarnessExample);
29+
fixture.detectChanges();
30+
loader = TestbedHarnessEnvironment.loader(fixture);
31+
});
32+
33+
it('should load checkbox with name', async () => {
34+
const checkboxes = await loader.getAllHarnesses(MatCheckboxHarness.with({name: 'first-name'}));
35+
expect(checkboxes.length).toBe(1);
36+
expect(await checkboxes[0].getLabelText()).toBe('First');
37+
});
38+
39+
it('should get checked state', async () => {
40+
const [checkedCheckbox, uncheckedCheckbox] = await loader.getAllHarnesses(MatCheckboxHarness);
41+
expect(await checkedCheckbox.isChecked()).toBe(true);
42+
expect(await uncheckedCheckbox.isChecked()).toBe(false);
43+
});
44+
45+
it('should get name', async () => {
46+
const checkbox = await loader.getHarness(MatCheckboxHarness.with({label: 'First'}));
47+
expect(await checkbox.getName()).toBe('first-name');
48+
});
49+
50+
it('should get label text', async () => {
51+
const [firstCheckbox, secondCheckbox] = await loader.getAllHarnesses(MatCheckboxHarness);
52+
expect(await firstCheckbox.getLabelText()).toBe('First');
53+
expect(await secondCheckbox.getLabelText()).toBe('Second');
54+
});
55+
56+
it('should toggle checkbox', async () => {
57+
fixture.componentInstance.disabled = false;
58+
const [checkedCheckbox, uncheckedCheckbox] = await loader.getAllHarnesses(MatCheckboxHarness);
59+
await checkedCheckbox.toggle();
60+
await uncheckedCheckbox.toggle();
61+
expect(await checkedCheckbox.isChecked()).toBe(false);
62+
expect(await uncheckedCheckbox.isChecked()).toBe(true);
63+
});
64+
});
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 Testing with MatCheckboxHarness
5+
*/
6+
@Component({
7+
selector: 'checkbox-harness-example',
8+
templateUrl: 'checkbox-harness-example.html',
9+
})
10+
export class CheckboxHarnessExample {
11+
disabled = true;
12+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ import {MatCheckboxModule} from '@angular/material/checkbox';
66
import {MatRadioModule} from '@angular/material/radio';
77
import {CheckboxConfigurableExample} from './checkbox-configurable/checkbox-configurable-example';
88
import {CheckboxOverviewExample} from './checkbox-overview/checkbox-overview-example';
9+
import {CheckboxHarnessExample} from './checkbox-harness/checkbox-harness-example';
910

1011
export {
1112
CheckboxConfigurableExample,
1213
CheckboxOverviewExample,
14+
CheckboxHarnessExample,
1315
};
1416

1517
const EXAMPLES = [
1618
CheckboxConfigurableExample,
1719
CheckboxOverviewExample,
20+
CheckboxHarnessExample,
1821
];
1922

2023
@NgModule({

0 commit comments

Comments
 (0)