-
Notifications
You must be signed in to change notification settings - Fork 943
/
Copy pathradio_test.ts
572 lines (500 loc) · 18.7 KB
/
radio_test.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {html} from 'lit';
import {Environment} from '../testing/environment.js';
import {createFormTests} from '../testing/forms.js';
import {createTokenTests} from '../testing/tokens.js';
import {RadioHarness} from './harness.js';
import {MdRadio} from './radio.js';
const defaultRadio = html`<md-radio></md-radio>`;
const radioGroup = html`
<md-radio id="a1" name="a"></md-radio>
<md-radio id="a2" name="a"></md-radio>
<md-radio id="b1" name="b"></md-radio>
`;
const radioGroupDisabled = html`
<md-radio id="a1" name="a" disabled></md-radio>
<md-radio id="a2" name="a" disabled checked></md-radio>
`;
const radioGroupPreSelected = html`
<md-radio id="a1" name="a"></md-radio>
<md-radio id="a2" name="a" checked></md-radio>
<md-radio id="a3" name="a"></md-radio>
<md-radio id="b1" name="b"></md-radio>
`;
describe('<md-radio>', () => {
const env = new Environment();
// Note, this would be better in the harness, but waiting in the test setup
// can be flaky without access to the test `env`.
async function simulateKeyDown(element: HTMLElement, key: string) {
const event = new KeyboardEvent('keydown', {key, bubbles: true});
element.dispatchEvent(event);
// We can remove the delay when FF issue addressed:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1804576
await env.waitForStability();
}
async function setupTest(template = defaultRadio) {
const root = env.render(template);
await env.waitForStability();
const radios = Array.from(root.querySelectorAll('md-radio'));
const harnesses = radios.map((radio) => new RadioHarness(radio));
return {harnesses, root};
}
describe('.styles', () => {
createTokenTests(MdRadio.styles);
});
describe('basic', () => {
it('initializes as an md-radio', async () => {
const {harnesses} = await setupTest();
expect(harnesses[0].element).toBeInstanceOf(MdRadio);
});
it('clicking a radio should select it', async () => {
const {harnesses} = await setupTest(radioGroup);
const unselected = harnesses[1];
expect(unselected.element.checked)
.withContext('unselected checked')
.toBeFalse();
await unselected.clickWithMouse();
expect(unselected.element.checked)
.withContext('after clicking checked')
.toBeTrue();
});
it('clicking a radio can be default prevented', async () => {
const {harnesses} = await setupTest(radioGroup);
const unselected = harnesses[1];
expect(unselected.element.checked)
.withContext('unselected checked')
.toBeFalse();
unselected.element.addEventListener('click', (event) => {
event.preventDefault();
});
await unselected.clickWithMouse();
expect(unselected.element.checked)
.withContext('after clicking checked')
.toBeFalse();
});
it('clicking a radio should unselect other radio which is already selected', async () => {
const {harnesses} = await setupTest(radioGroupPreSelected);
const [, a2, a3] = harnesses;
expect(a2.element.checked).withContext('already checked').toBeTrue();
await a3.clickWithMouse();
expect(a3.element.checked).withContext('new radio checked').toBeTrue();
expect(a2.element.checked)
.withContext('previous radio checked')
.toBeFalse();
});
it('disabled radio should not be selected when clicked', async () => {
const {harnesses} = await setupTest(radioGroupDisabled);
const [a1, a2] = harnesses;
expect(a1.element.checked).withContext('unchecked radio').toBeFalse();
expect(a2.element.checked).withContext('checked radio').toBeTrue();
await a1.clickWithMouse();
expect(a1.element.checked)
.withContext('still unchecked radio')
.toBeFalse();
await a2.clickWithMouse();
expect(a2.element.checked).withContext('still checked radio').toBeTrue();
});
});
describe('events', () => {
it('Should trigger change event when a radio is selected', async () => {
const {harnesses, root} = await setupTest(radioGroupPreSelected);
const changeHandler = jasmine.createSpy('changeHandler');
root.addEventListener('change', changeHandler);
const a3 = harnesses[2];
await a3.clickWithMouse();
expect(a3.element.checked)
.withContext('clicked radio checked')
.toBeTrue();
expect(changeHandler).toHaveBeenCalledTimes(1);
expect(changeHandler).toHaveBeenCalledWith(jasmine.any(Event));
});
it('Should trigger input event when a radio is selected', async () => {
const {harnesses, root} = await setupTest(radioGroupPreSelected);
const inputHandler = jasmine.createSpy('inputHandler');
root.addEventListener('input', inputHandler);
const a3 = harnesses[2];
await a3.clickWithMouse();
expect(a3.element.checked)
.withContext('clicked radio checked')
.toBeTrue();
expect(inputHandler).toHaveBeenCalledTimes(1);
expect(inputHandler).toHaveBeenCalledWith(jasmine.any(InputEvent));
});
});
describe('navigation', () => {
it('Using arrow right should select the next radio button', async () => {
const {harnesses} = await setupTest(radioGroupPreSelected);
const [, a2, a3] = harnesses;
expect(a2.element.checked)
.withContext('default checked radio')
.toBeTrue();
await simulateKeyDown(a2.element, 'ArrowRight');
expect(a3.element.checked).withContext('next radio checked').toBeTrue();
expect(a2.element.checked).withContext('prev radio checked').toBeFalse();
});
it('dispatched a change event on user navigation', async () => {
const {harnesses, root} = await setupTest(radioGroupPreSelected);
const changeHandler = jasmine.createSpy('changeHandler');
root.addEventListener('change', changeHandler);
const [, a2] = harnesses;
expect(a2.element.checked)
.withContext('default checked radio')
.toBeTrue();
await simulateKeyDown(a2.element, 'ArrowRight');
expect(changeHandler).toHaveBeenCalledTimes(1);
});
it('Using arrow right on the last radio should select the first radio in that group', async () => {
const {harnesses} = await setupTest(radioGroupPreSelected);
const [a1, a2, a3, b1] = harnesses;
expect(a2.element.checked).toBeTrue();
await simulateKeyDown(a2.element, 'ArrowRight');
await simulateKeyDown(a3.element, 'ArrowRight');
expect(a3.element.checked).withContext('last radio checked').toBeFalse();
expect(a1.element.checked).withContext('first radio checked').toBeTrue();
expect(b1.element.checked)
.withContext('unrelated radio checked')
.toBeFalse();
});
});
describe('manages selection groups', () => {
it('synchronously', async () => {
const {harnesses} = await setupTest(radioGroup);
const [a1, a2, b1] = harnesses;
expect(a1.element.checked).withContext('initially unchecked').toBeFalse();
expect(a2.element.checked).withContext('initially unchecked').toBeFalse();
expect(b1.element.checked).withContext('initially unchecked').toBeFalse();
// Should uncheck previously checked radio
a2.element.checked = true;
a1.element.checked = true;
expect(a1.element.checked).withContext('last radio checked').toBeTrue();
expect(a2.element.checked)
.withContext('unchecked by last radio')
.toBeFalse();
expect(b1.element.checked)
.withContext('unrelated radio unchecked')
.toBeFalse();
// Should re-check radio
a2.element.checked = true;
a1.element.checked = true;
a2.element.checked = true;
expect(a1.element.checked)
.withContext('unchecked by second radio')
.toBeFalse();
expect(a2.element.checked).withContext('last radio checked').toBeTrue();
expect(b1.element.checked)
.withContext('unrelated radio unchecked')
.toBeFalse();
// Should ignore unrelated radios
a1.element.checked = true;
expect(a1.element.checked)
.withContext('related checked radio')
.toBeTrue();
expect(a2.element.checked)
.withContext('related unchecked radio')
.toBeFalse();
expect(b1.element.checked)
.withContext('unrelated unchecked radio')
.toBeFalse();
b1.element.checked = true;
expect(a1.element.checked)
.withContext('related checked radio')
.toBeTrue();
expect(a2.element.checked)
.withContext('related unchecked radio')
.toBeFalse();
expect(b1.element.checked)
.withContext('unrelated checked radio')
.toBeTrue();
a1.element.checked = false;
b1.element.checked = false;
expect(a1.element.checked)
.withContext('related unchecked radio')
.toBeFalse();
expect(a2.element.checked)
.withContext('related unchecked radio')
.toBeFalse();
expect(b1.element.checked)
.withContext('unrelated unchecked radio')
.toBeFalse();
});
it('after updates settle', async () => {
const {harnesses} = await setupTest(radioGroup);
const [a1, a2, b1] = harnesses;
const allUpdatesComplete = () =>
Promise.all(harnesses.map((harness) => harness.element.updateComplete));
await allUpdatesComplete();
expect(a1.element.checked).withContext('initially unchecked').toBeFalse();
expect(a2.element.checked).withContext('initially unchecked').toBeFalse();
expect(b1.element.checked).withContext('initially unchecked').toBeFalse();
// Should uncheck previously checked radio
a2.element.checked = true;
a1.element.checked = true;
await allUpdatesComplete();
expect(a1.element.checked).withContext('last radio checked').toBeTrue();
expect(a2.element.checked)
.withContext('unchecked by last radio')
.toBeFalse();
expect(b1.element.checked)
.withContext('unrelated radio unchecked')
.toBeFalse();
// Should re-check radio
a2.element.checked = true;
a1.element.checked = true;
a2.element.checked = true;
await allUpdatesComplete();
expect(a1.element.checked)
.withContext('unchecked by second radio')
.toBeFalse();
expect(a2.element.checked).withContext('last radio checked').toBeTrue();
expect(b1.element.checked)
.withContext('unrelated radio unchecked')
.toBeFalse();
// Should ignore unrelated radios
a1.element.checked = true;
expect(a1.element.checked)
.withContext('related checked radio')
.toBeTrue();
expect(a2.element.checked)
.withContext('related unchecked radio')
.toBeFalse();
expect(b1.element.checked)
.withContext('unrelated unchecked radio')
.toBeFalse();
b1.element.checked = true;
await allUpdatesComplete();
expect(a1.element.checked)
.withContext('related checked radio')
.toBeTrue();
expect(a2.element.checked)
.withContext('related unchecked radio')
.toBeFalse();
expect(b1.element.checked)
.withContext('unrelated checked radio')
.toBeTrue();
a1.element.checked = false;
b1.element.checked = false;
await allUpdatesComplete();
expect(a1.element.checked)
.withContext('related unchecked radio')
.toBeFalse();
expect(a2.element.checked)
.withContext('related unchecked radio')
.toBeFalse();
expect(b1.element.checked)
.withContext('unrelated unchecked radio')
.toBeFalse();
});
it('when checked before connected', async () => {
const root = env.render(html`<main></main>`);
const container = root.querySelector('main')!;
const r1 = document.createElement('md-radio');
r1.setAttribute('name', 'a');
const r2 = document.createElement('md-radio');
r2.setAttribute('name', 'a');
const r3 = document.createElement('md-radio');
r3.setAttribute('name', 'a');
// r1 and r2 should both be checked, because even though they have the
// same name, they aren't yet connected to a root. Groups are scoped to
// roots, and we can't know which root a radio belongs to until it is
// connected to one. This matches native <input type="radio"> behavior.
r1.checked = true;
r2.checked = true;
expect(r1.checked).toBeTrue();
expect(r2.checked).toBeTrue();
expect(r3.checked).toBeFalse();
// Connecting r1 shouldn't change anything, since it's the only one in the
// group.
container.appendChild(r1);
expect(r1.checked).toBeTrue();
expect(r2.checked).toBeTrue();
expect(r3.checked).toBeFalse();
// Appending r2 should uncheck r1, because when a new checked radio is
// connected, it wins (this matches native input behavior).
container.appendChild(r2);
expect(r1.checked).toBeFalse();
expect(r2.checked).toBeTrue();
expect(r3.checked).toBeFalse();
// Appending r3 shouldn't change anything, because it's not checked.
container.appendChild(r3);
expect(r1.checked).toBeFalse();
expect(r2.checked).toBeTrue();
expect(r3.checked).toBeFalse();
// Checking r3 should uncheck r2 because it's now in the same group.
r3.checked = true;
expect(r1.checked).toBeFalse();
expect(r2.checked).toBeFalse();
expect(r3.checked).toBeTrue();
});
it('in a lit repeat', async () => {
const values = ['a1', 'a2'];
const repeated = values.map(
(value) => html`<md-radio value=${value} name="a"></md-radio>`,
);
const root = env.render(html`${repeated}`);
await env.waitForStability();
const [a1, a2] = root.querySelectorAll('md-radio');
expect(a1.checked).toBeFalse();
expect(a2.checked).toBeFalse();
expect(a1.value).toEqual(values[0]);
expect(a2.value).toEqual(values[1]);
a1.checked = true;
expect(a1.checked).toBeTrue();
expect(a2.checked).toBeFalse();
a2.checked = true;
expect(a1.checked).toBeFalse();
expect(a2.checked).toBeTrue();
a2.checked = false;
expect(a1.checked).toBeFalse();
expect(a2.checked).toBeFalse();
});
});
describe('label activation', () => {
async function setupLabelTest() {
const root = env.render(html`
<label> <md-radio name="a"></md-radio></label>
<label> <md-radio name="a"></md-radio></label>
`);
await env.waitForStability();
// [[label, radio]]
return Array.from(root.querySelectorAll('label')).map(
(el) => [el, el.firstElementChild as MdRadio] as const,
);
}
it('toggles when label is clicked', async () => {
const [[label1, radio1], [label2, radio2]] = await setupLabelTest();
label1.click();
await env.waitForStability();
expect(radio1.checked).toBeTrue();
expect(radio2.checked).toBeFalse();
label2.click();
await env.waitForStability();
expect(radio1.checked).toBeFalse();
expect(radio2.checked).toBeTrue();
});
});
describe('forms', () => {
createFormTests({
queryControl: (root) => root.querySelector('md-radio'),
valueTests: [
{
name: 'unnamed',
render: () => html`
<md-radio value="One" checked></md-radio>
<md-radio value="Two"></md-radio>
`,
assertValue(formData) {
expect(formData)
.withContext('should not add anything to form without a name')
.toHaveSize(0);
},
},
{
name: 'unchecked',
render: () => html`
<md-radio name="radio" value="One"></md-radio>
<md-radio name="radio" value="Two"></md-radio>
`,
assertValue(formData) {
expect(formData)
.withContext('should not add anything to form when unchecked')
.toHaveSize(0);
},
},
{
name: 'checked first value',
render: () => html`
<md-radio name="radio" value="One" checked></md-radio>
<md-radio name="radio" value="Two"></md-radio>
`,
assertValue(formData) {
expect(formData.get('radio')).toBe('One');
},
},
{
name: 'checked second value',
render: () => html`
<md-radio name="radio" value="One"></md-radio>
<md-radio name="radio" value="Two" checked></md-radio>
`,
assertValue(formData) {
expect(formData.get('radio')).toBe('Two');
},
},
{
name: 'disabled',
render: () => html`
<md-radio name="radio" value="One" checked disabled></md-radio>
<md-radio name="radio" value="Two" disabled></md-radio>
`,
assertValue(formData) {
expect(formData)
.withContext('should not add anything to form when disabled')
.toHaveSize(0);
},
},
],
resetTests: [
{
name: 'reset to unchecked',
render: () => html`
<md-radio name="radio" value="One"></md-radio>
<md-radio name="radio" value="Two"></md-radio>
`,
change(radio) {
radio.checked = true;
},
assertReset(radio) {
expect(radio.checked)
.withContext('radio.checked after reset')
.toBeFalse();
},
},
{
name: 'reset to checked',
render: () => html`
<md-radio name="radio" value="One" checked></md-radio>
<md-radio name="radio" value="Two"></md-radio>
`,
change(radio) {
radio.checked = false;
},
assertReset(radio) {
expect(radio.checked)
.withContext('radio.checked after reset')
.toBeTrue();
},
},
],
restoreTests: [
{
name: 'restore unchecked',
render: () => html`
<md-radio name="radio" value="One"></md-radio>
<md-radio name="radio" value="Two"></md-radio>
`,
assertRestored(radio) {
expect(radio.checked)
.withContext('radio.checked after restore')
.toBeFalse();
},
},
{
name: 'restore checked',
render: () => html`
<md-radio name="radio" value="One" checked></md-radio>
<md-radio name="radio" value="Two"></md-radio>
`,
assertRestored(radio) {
expect(radio.checked)
.withContext('radio.checked after restore')
.toBeTrue();
},
},
],
});
});
});