-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathradio-harness.spec.ts
344 lines (285 loc) · 13.5 KB
/
radio-harness.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import {HarnessLoader} from '@angular/cdk/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {Component} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {ReactiveFormsModule} from '@angular/forms';
import {MatRadioModule} from '@angular/material/radio';
import {MatRadioButtonHarness, MatRadioGroupHarness} from './radio-harness';
describe('radio harness', () => {
let fixture: ComponentFixture<MultipleRadioButtonsHarnessTest>;
let loader: HarnessLoader;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [MatRadioModule, ReactiveFormsModule, MultipleRadioButtonsHarnessTest],
});
fixture = TestBed.createComponent(MultipleRadioButtonsHarnessTest);
fixture.detectChanges();
loader = TestbedHarnessEnvironment.loader(fixture);
});
describe('MatRadioGroupHarness', () => {
it('should load all radio-group harnesses', async () => {
const groups = await loader.getAllHarnesses(MatRadioGroupHarness);
expect(groups.length).toBe(3);
});
it('should load radio-group with exact id', async () => {
const groups = await loader.getAllHarnesses(
MatRadioGroupHarness.with({selector: '#my-group-2'}),
);
expect(groups.length).toBe(1);
});
it('should load radio-group by name', async () => {
let groups = await loader.getAllHarnesses(
MatRadioGroupHarness.with({name: 'my-group-2-name'}),
);
expect(groups.length).toBe(1);
expect(await groups[0].getId()).toBe('my-group-2');
groups = await loader.getAllHarnesses(MatRadioGroupHarness.with({name: 'my-group-1-name'}));
expect(groups.length).toBe(1);
expect(await groups[0].getId()).toBe('my-group-1');
});
it('should throw when finding radio-group with specific name that has mismatched radio-button names', async () => {
fixture.componentInstance.thirdGroupButtonName = 'other-name';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
await expectAsync(
loader.getAllHarnesses(MatRadioGroupHarness.with({name: 'third-group-name'})),
).toBeRejectedWithError(
/locator found a radio-group with name "third-group-name".*have mismatching names/,
);
});
it('should get name of radio-group', async () => {
const groups = await loader.getAllHarnesses(MatRadioGroupHarness);
expect(groups.length).toBe(3);
expect(await groups[0].getName()).toBe('my-group-1-name');
expect(await groups[1].getName()).toBe('my-group-2-name');
expect(await groups[2].getName()).toBe('third-group-name');
fixture.componentInstance.secondGroupId = 'new-group';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(await groups[1].getName()).toBe('new-group-name');
fixture.componentInstance.thirdGroupButtonName = 'other-button-name';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
await expectAsync(groups[2].getName()).toBeRejectedWithError(
/Radio buttons in radio-group have mismatching names./,
);
});
it('should get id of radio-group', async () => {
const groups = await loader.getAllHarnesses(MatRadioGroupHarness);
expect(groups.length).toBe(3);
expect(await groups[0].getId()).toBe('my-group-1');
expect(await groups[1].getId()).toBe('my-group-2');
expect(await groups[2].getId()).toBe('');
fixture.componentInstance.secondGroupId = 'new-group-name';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(await groups[1].getId()).toBe('new-group-name');
});
it('should get checked value of radio-group', async () => {
const [firstGroup, secondGroup] = await loader.getAllHarnesses(MatRadioGroupHarness);
expect(await firstGroup.getCheckedValue()).toBe('opt2');
expect(await secondGroup.getCheckedValue()).toBe(null);
});
it('should get radio-button harnesses of radio-group', async () => {
const groups = await loader.getAllHarnesses(MatRadioGroupHarness);
expect(groups.length).toBe(3);
expect((await groups[0].getRadioButtons()).length).toBe(3);
expect((await groups[1].getRadioButtons()).length).toBe(1);
expect((await groups[2].getRadioButtons()).length).toBe(2);
});
it('should get radio buttons from group with filter', async () => {
const group = await loader.getHarness(MatRadioGroupHarness.with({name: 'my-group-1-name'}));
expect((await group.getRadioButtons({label: 'opt2'})).length).toBe(1);
});
it('should get checked radio-button harnesses of radio-group', async () => {
const groups = await loader.getAllHarnesses(MatRadioGroupHarness);
expect(groups.length).toBe(3);
const groupOneChecked = await groups[0].getCheckedRadioButton();
const groupTwoChecked = await groups[1].getCheckedRadioButton();
const groupThreeChecked = await groups[2].getCheckedRadioButton();
expect(groupOneChecked).not.toBeNull();
expect(groupTwoChecked).toBeNull();
expect(groupThreeChecked).toBeNull();
expect(await groupOneChecked!.getId()).toBe('opt2-group-one');
});
it('should check radio button in group', async () => {
const group = await loader.getHarness(MatRadioGroupHarness.with({name: 'my-group-1-name'}));
expect(await group.getCheckedValue()).toBe('opt2');
await group.checkRadioButton({label: 'opt3'});
expect(await group.getCheckedValue()).toBe('opt3');
});
it('should throw error when checking invalid radio button', async () => {
const group = await loader.getHarness(MatRadioGroupHarness.with({name: 'my-group-1-name'}));
await expectAsync(group.checkRadioButton({label: 'opt4'})).toBeRejectedWithError(
/Could not find radio button matching {"label":"opt4"}/,
);
});
});
describe('MatRadioButtonHarness', () => {
it('should load all radio-button harnesses', async () => {
const radios = await loader.getAllHarnesses(MatRadioButtonHarness);
expect(radios.length).toBe(10);
});
it('should load radio-button with exact label', async () => {
const radios = await loader.getAllHarnesses(MatRadioButtonHarness.with({label: 'Option #2'}));
expect(radios.length).toBe(1);
expect(await radios[0].getId()).toBe('opt2');
expect(await radios[0].getLabelText()).toBe('Option #2');
});
it('should load radio-button with regex label match', async () => {
const radios = await loader.getAllHarnesses(MatRadioButtonHarness.with({label: /#3$/i}));
expect(radios.length).toBe(1);
expect(await radios[0].getId()).toBe('opt3');
expect(await radios[0].getLabelText()).toBe('Option #3');
});
it('should load radio-button with id', async () => {
const radios = await loader.getAllHarnesses(MatRadioButtonHarness.with({selector: '#opt3'}));
expect(radios.length).toBe(1);
expect(await radios[0].getId()).toBe('opt3');
expect(await radios[0].getLabelText()).toBe('Option #3');
});
it('should load radio-buttons with same name', async () => {
const radios = await loader.getAllHarnesses(MatRadioButtonHarness.with({name: 'group1'}));
expect(radios.length).toBe(2);
expect(await radios[0].getId()).toBe('opt1');
expect(await radios[1].getId()).toBe('opt2');
});
it('should get checked state', async () => {
const [uncheckedRadio, checkedRadio] = await loader.getAllHarnesses(MatRadioButtonHarness);
expect(await uncheckedRadio.isChecked()).toBe(false);
expect(await checkedRadio.isChecked()).toBe(true);
});
it('should get label text', async () => {
const [firstRadio, secondRadio, thirdRadio] =
await loader.getAllHarnesses(MatRadioButtonHarness);
expect(await firstRadio.getLabelText()).toBe('Option #1');
expect(await secondRadio.getLabelText()).toBe('Option #2');
expect(await thirdRadio.getLabelText()).toBe('Option #3');
});
it('should get value', async () => {
const [firstRadio, secondRadio, thirdRadio] =
await loader.getAllHarnesses(MatRadioButtonHarness);
expect(await firstRadio.getValue()).toBe('opt1');
expect(await secondRadio.getValue()).toBe('opt2');
expect(await thirdRadio.getValue()).toBe('opt3');
});
it('should get disabled state', async () => {
const [firstRadio] = await loader.getAllHarnesses(MatRadioButtonHarness);
expect(await firstRadio.isDisabled()).toBe(false);
fixture.componentInstance.disableAll = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(await firstRadio.isDisabled()).toBe(true);
});
it('should get the disabled state with disabledInteractive is enabled', async () => {
fixture.componentInstance.disabledInteractive = true;
fixture.changeDetectorRef.markForCheck();
const [firstRadio] = await loader.getAllHarnesses(MatRadioButtonHarness);
expect(await firstRadio.isDisabled()).toBe(false);
fixture.componentInstance.disableAll = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(await firstRadio.isDisabled()).toBe(true);
});
it('should focus radio-button', async () => {
const radioButton = await loader.getHarness(MatRadioButtonHarness.with({selector: '#opt2'}));
expect(await radioButton.isFocused()).toBe(false);
await radioButton.focus();
expect(await radioButton.isFocused()).toBe(true);
});
it('should blur radio-button', async () => {
const radioButton = await loader.getHarness(MatRadioButtonHarness.with({selector: '#opt2'}));
await radioButton.focus();
expect(await radioButton.isFocused()).toBe(true);
await radioButton.blur();
expect(await radioButton.isFocused()).toBe(false);
});
it('should check radio-button', async () => {
const [uncheckedRadio, checkedRadio] = await loader.getAllHarnesses(MatRadioButtonHarness);
await uncheckedRadio.check();
expect(await uncheckedRadio.isChecked()).toBe(true);
// Checked radio state should change since the two radio's
// have the same name and only one can be checked.
expect(await checkedRadio.isChecked()).toBe(false);
});
it('should not be able to check disabled radio-button', async () => {
fixture.componentInstance.disableAll = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
const radioButton = await loader.getHarness(MatRadioButtonHarness.with({selector: '#opt3'}));
expect(await radioButton.isChecked()).toBe(false);
await radioButton.check();
expect(await radioButton.isChecked()).toBe(false);
fixture.componentInstance.disableAll = false;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(await radioButton.isChecked()).toBe(false);
await radioButton.check();
expect(await radioButton.isChecked()).toBe(true);
});
// radios with group should not contain required attribute as group itself is marked if its
// required or not, see #30399
it('should have falsy required state if used with MatRadioGroup', async () => {
const radioButton = await loader.getHarness(
MatRadioButtonHarness.with({selector: '#required-radio-inside-group'}),
);
expect(await radioButton.isRequired()).toBe(false);
});
it('should set required state of radio without group', async () => {
const radioButton = await loader.getHarness(
MatRadioButtonHarness.with({selector: '#required-radio'}),
);
expect(await radioButton.isRequired()).toBe(false);
fixture.componentInstance.standaloneRequiredRadio = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(await radioButton.isRequired()).toBe(true);
});
});
});
@Component({
template: `
@for (value of values; track value) {
<mat-radio-button
[name]="value === 'opt3' ? 'group2' : 'group1'"
[disabled]="disableAll"
[disabledInteractive]="disabledInteractive"
[checked]="value === 'opt2'"
[id]="value"
[required]="value === 'opt2'"
[value]="value">
Option #{{$index + 1}}
</mat-radio-button>
}
<mat-radio-group id="my-group-1" name="my-group-1-name">
@for (value of values; track value) {
<mat-radio-button
[checked]="value === 'opt2'"
[value]="value"
[id]="value + '-group-one'">{{value}}</mat-radio-button>
}
</mat-radio-group>
<mat-radio-group [id]="secondGroupId" [name]="secondGroupId + '-name'">
<mat-radio-button id="required-radio-inside-group" required [value]="true">
Accept terms of conditions
</mat-radio-button>
</mat-radio-group>
<mat-radio-button id="required-radio" [required]="standaloneRequiredRadio" [value]="true">
Accept terms of conditions
</mat-radio-button>
<mat-radio-group [name]="thirdGroupName">
<mat-radio-button [value]="true">First</mat-radio-button>
<mat-radio-button [value]="false" [name]="thirdGroupButtonName"></mat-radio-button>
</mat-radio-group>
`,
imports: [MatRadioModule, ReactiveFormsModule],
})
class MultipleRadioButtonsHarnessTest {
values = ['opt1', 'opt2', 'opt3'];
disableAll = false;
disabledInteractive = false;
secondGroupId = 'my-group-2';
thirdGroupName: string = 'third-group-name';
thirdGroupButtonName: string | undefined = undefined;
standaloneRequiredRadio = false;
}