Skip to content

Commit c41eb30

Browse files
authored
docs(material/radio): add harness example for radio (#21463)
1 parent b04af89 commit c41eb30

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/radio",
1313
deps = [
14+
"//src/cdk/testing",
15+
"//src/cdk/testing/testbed",
1416
"//src/material/radio",
17+
"//src/material/radio/testing",
1518
"@npm//@angular/forms",
19+
"@npm//@angular/platform-browser",
20+
"@npm//@angular/platform-browser-dynamic",
21+
"@npm//@types/jasmine",
1622
],
1723
)
1824

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import {CommonModule} from '@angular/common';
22
import {NgModule} from '@angular/core';
3-
import {FormsModule} from '@angular/forms';
3+
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
44
import {MatRadioModule} from '@angular/material/radio';
55
import {RadioNgModelExample} from './radio-ng-model/radio-ng-model-example';
66
import {RadioOverviewExample} from './radio-overview/radio-overview-example';
7+
import {RadioHarnessExample} from './radio-harness/radio-harness-example';
78

89
export {
10+
RadioHarnessExample,
911
RadioNgModelExample,
1012
RadioOverviewExample,
1113
};
1214

1315
const EXAMPLES = [
16+
RadioHarnessExample,
1417
RadioNgModelExample,
1518
RadioOverviewExample,
1619
];
1720

1821
@NgModule({
1922
imports: [
23+
ReactiveFormsModule,
2024
CommonModule,
2125
MatRadioModule,
2226
FormsModule,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<mat-radio-group name="flavors">
2+
<mat-radio-button value="chocolate" checked="true">Chocolate</mat-radio-button>
3+
<mat-radio-button value="vanilla">Vanilla</mat-radio-button>
4+
<mat-radio-button value="strawberry">Strawberry</mat-radio-button>
5+
</mat-radio-group>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatRadioButtonHarness, MatRadioGroupHarness} from '@angular/material/radio/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {MatRadioModule} from '@angular/material/radio';
8+
import {RadioHarnessExample} from './radio-harness-example';
9+
import {ReactiveFormsModule} from '@angular/forms';
10+
11+
describe('RadioHarnessExample', () => {
12+
let fixture: ComponentFixture<RadioHarnessExample>;
13+
let loader: HarnessLoader;
14+
15+
beforeAll(() => {
16+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
17+
});
18+
19+
beforeEach(
20+
waitForAsync(() => {
21+
TestBed.configureTestingModule({
22+
imports: [MatRadioModule, ReactiveFormsModule],
23+
declarations: [RadioHarnessExample]
24+
}).compileComponents();
25+
fixture = TestBed.createComponent(RadioHarnessExample);
26+
fixture.detectChanges();
27+
loader = TestbedHarnessEnvironment.loader(fixture);
28+
})
29+
);
30+
31+
it('should load all radio-group harnesses', async () => {
32+
const groups = await loader.getAllHarnesses(MatRadioGroupHarness);
33+
expect(groups.length).toBe(1);
34+
});
35+
36+
it('should get name of radio-group', async () => {
37+
const group = await loader.getHarness(MatRadioGroupHarness);
38+
const name = await group.getName();
39+
expect(name).toBe('flavors');
40+
});
41+
42+
it('should check radio button', async () => {
43+
const buttons = await loader.getAllHarnesses(MatRadioButtonHarness);
44+
expect(await buttons[0].isChecked()).toBeTrue();
45+
46+
await buttons[1].check();
47+
expect(await buttons[1].isChecked()).toBeTrue();
48+
expect(await buttons[0].isChecked()).toBeFalse();
49+
});
50+
51+
it('should get label text of buttons', async () => {
52+
const [firstRadio, secondRadio, thirdRadio] =
53+
await loader.getAllHarnesses(MatRadioButtonHarness);
54+
expect(await firstRadio.getLabelText()).toBe('Chocolate');
55+
expect(await secondRadio.getLabelText()).toBe('Vanilla');
56+
expect(await thirdRadio.getLabelText()).toBe('Strawberry');
57+
});
58+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Testing with MatRadioHarness
5+
*/
6+
@Component({
7+
selector: 'radio-harness-example',
8+
templateUrl: 'radio-harness-example.html',
9+
})
10+
export class RadioHarnessExample {}

0 commit comments

Comments
 (0)