-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathCheckboxGroup.test.js
746 lines (631 loc) · 28.9 KB
/
CheckboxGroup.test.js
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {act, pointerMap, renderv3 as render, within} from '@react-spectrum/test-utils-internal';
import {Button} from '@react-spectrum/button';
import {Checkbox, CheckboxGroup} from '../';
import {Form} from '@react-spectrum/form';
import {Provider} from '@react-spectrum/provider';
import React from 'react';
import {theme} from '@react-spectrum/theme-default';
import userEvent from '@testing-library/user-event';
describe('CheckboxGroup', () => {
let user;
beforeAll(() => {
user = userEvent.setup({delay: null, pointerMap});
});
it('handles defaults', async () => {
let onChangeSpy = jest.fn();
let {getByRole, getAllByRole, getByLabelText} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet" onChange={onChangeSpy}>
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxGroup = getByRole('group', {exact: true});
let checkboxes = getAllByRole('checkbox');
expect(checkboxGroup).toBeInTheDocument();
expect(checkboxes.length).toBe(3);
expect(checkboxes[0]).not.toHaveAttribute('name');
expect(checkboxes[1]).not.toHaveAttribute('name');
expect(checkboxes[2]).not.toHaveAttribute('name');
expect(checkboxes[0].value).toBe('dogs');
expect(checkboxes[1].value).toBe('cats');
expect(checkboxes[2].value).toBe('dragons');
expect(checkboxes[0].checked).toBe(false);
expect(checkboxes[1].checked).toBe(false);
expect(checkboxes[2].checked).toBe(false);
let dragons = getByLabelText('Dragons');
await user.click(dragons);
expect(onChangeSpy).toHaveBeenCalledTimes(1);
expect(onChangeSpy).toHaveBeenCalledWith(['dragons']);
expect(checkboxes[0].checked).toBe(false);
expect(checkboxes[1].checked).toBe(false);
expect(checkboxes[2].checked).toBe(true);
});
it('can have a default value', () => {
let {getByLabelText} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet" value={['cats']}>
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
expect(getByLabelText('Cats').checked).toBe(true);
});
it('name can be controlled', () => {
let {getAllByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet" name="awesome-react-aria">
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxes = getAllByRole('checkbox');
expect(checkboxes[0]).toHaveAttribute('name', 'awesome-react-aria');
expect(checkboxes[1]).toHaveAttribute('name', 'awesome-react-aria');
expect(checkboxes[2]).toHaveAttribute('name', 'awesome-react-aria');
});
it('supports labeling', () => {
let {getByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet">
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxGroup = getByRole('group', {exact: true});
let labelId = checkboxGroup.getAttribute('aria-labelledby');
expect(labelId).toBeDefined();
let label = document.getElementById(labelId);
expect(label).toHaveTextContent('Favorite Pet');
});
it('supports aria-label', () => {
let {getByRole} = render(
<Provider theme={theme}>
<CheckboxGroup aria-label="My Favorite Pet">
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxGroup = getByRole('group', {exact: true});
expect(checkboxGroup).toHaveAttribute('aria-label', 'My Favorite Pet');
});
it('supports custom props', () => {
let {getByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet" data-testid="favorite-pet">
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxGroup = getByRole('group', {exact: true});
expect(checkboxGroup).toHaveAttribute('data-testid', 'favorite-pet');
});
it('sets aria-disabled when isDisabled is true', () => {
let {getAllByRole, getByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet" isDisabled>
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxGroup = getByRole('group', {exact: true});
expect(checkboxGroup).toHaveAttribute('aria-disabled', 'true');
let checkboxes = getAllByRole('checkbox');
expect(checkboxes[0]).toHaveAttribute('disabled');
expect(checkboxes[1]).toHaveAttribute('disabled');
expect(checkboxes[2]).toHaveAttribute('disabled');
});
it('doesn\'t set aria-disabled by default', () => {
let {getAllByRole, getByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet">
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxGroup = getByRole('group', {exact: true});
expect(checkboxGroup).not.toHaveAttribute('aria-disabled');
let checkboxes = getAllByRole('checkbox');
expect(checkboxes[0]).not.toHaveAttribute('disabled');
expect(checkboxes[1]).not.toHaveAttribute('disabled');
expect(checkboxes[2]).not.toHaveAttribute('disabled');
});
it('doesn\'t set aria-disabled when isDisabled is false', () => {
let {getAllByRole, getByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet" isDisabled={false}>
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxGroup = getByRole('group', {exact: true});
expect(checkboxGroup).not.toHaveAttribute('aria-disabled');
let checkboxes = getAllByRole('checkbox');
expect(checkboxes[0]).not.toHaveAttribute('disabled');
expect(checkboxes[1]).not.toHaveAttribute('disabled');
expect(checkboxes[2]).not.toHaveAttribute('disabled');
});
it('sets aria-readonly="true" on each checkbox', () => {
let {getAllByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet" isReadOnly>
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxes = getAllByRole('checkbox');
expect(checkboxes[0]).toHaveAttribute('aria-readonly', 'true');
expect(checkboxes[1]).toHaveAttribute('aria-readonly', 'true');
expect(checkboxes[2]).toHaveAttribute('aria-readonly', 'true');
});
it('should not update state for readonly checkbox', async () => {
let groupOnChangeSpy = jest.fn();
let checkboxOnChangeSpy = jest.fn();
let {getAllByRole, getByLabelText} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet" onChange={groupOnChangeSpy}>
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons" isReadOnly onChange={checkboxOnChangeSpy}>Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxes = getAllByRole('checkbox');
let dragons = getByLabelText('Dragons');
await user.click(dragons);
expect(groupOnChangeSpy).toHaveBeenCalledTimes(0);
expect(checkboxOnChangeSpy).toHaveBeenCalledTimes(0);
expect(checkboxes[2].checked).toBe(false);
});
it('adds required to group label', () => {
let {getAllByRole, getByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet" isRequired>
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxGroup = getByRole('group', {exact: true});
let labelId = checkboxGroup.getAttribute('aria-labelledby');
let label = document.getElementById(labelId);
expect(label).toHaveTextContent('Favorite Pet');
let necessityIndicator = within(label).getByRole('img');
expect(necessityIndicator).toHaveAttribute('aria-label', '(required)');
let checkboxes = getAllByRole('checkbox');
expect(checkboxes[0]).not.toHaveAttribute('required');
expect(checkboxes[1]).not.toHaveAttribute('required');
expect(checkboxes[2]).not.toHaveAttribute('required');
});
it('supports isRequired on individual checkboxes', () => {
let {getAllByRole, getByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Agree to the following" isRequired>
<Checkbox value="terms" isRequired>Terms and conditions</Checkbox>
<Checkbox value="cookies" isRequired>Cookies</Checkbox>
<Checkbox value="privacy" isRequired>Privacy policy</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxGroup = getByRole('group', {exact: true});
let labelId = checkboxGroup.getAttribute('aria-labelledby');
let label = document.getElementById(labelId);
expect(label).toHaveTextContent('Agree to the following');
let necessityIndicator = within(label).getByRole('img');
expect(necessityIndicator).toHaveAttribute('aria-label', '(required)');
let checkboxes = getAllByRole('checkbox');
expect(checkboxes[0]).toHaveAttribute('aria-required');
expect(checkboxes[1]).toHaveAttribute('aria-required');
expect(checkboxes[2]).toHaveAttribute('aria-required');
});
it('adds aria-invalid to all checkboxes when the group is invalid', () => {
let {getAllByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet" isInvalid>
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxes = getAllByRole('checkbox');
expect(checkboxes[0]).toHaveAttribute('aria-invalid');
expect(checkboxes[1]).toHaveAttribute('aria-invalid');
expect(checkboxes[2]).toHaveAttribute('aria-invalid');
});
it('supports invalid state on individual checkboxes', () => {
let {getAllByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Agree to the following">
<Checkbox value="terms" isInvalid>Terms and conditions</Checkbox>
<Checkbox value="cookies" isInvalid>Cookies</Checkbox>
<Checkbox value="privacy">Privacy policy</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxes = getAllByRole('checkbox');
expect(checkboxes[0]).toHaveAttribute('aria-invalid', 'true');
expect(checkboxes[1]).toHaveAttribute('aria-invalid', 'true');
expect(checkboxes[2]).not.toHaveAttribute('aria-invalid');
});
it.each(['isSelected', 'defaultSelected', 'isEmphasized'])('warns if %s is passed to an individual checkbox', (prop) => {
let props = {[prop]: true};
let spy = jest.spyOn(console, 'warn').mockImplementation(() => {});
render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet">
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats" {...props}>Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
expect(spy).toHaveBeenCalledWith(`${prop} is unsupported on individual <Checkbox> elements within a <CheckboxGroup>. Please apply these props to the group instead.`);
});
it('should support help text description', function () {
let {getByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet" description="Help text">
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let group = getByRole('group');
expect(group).toHaveAttribute('aria-describedby');
let description = document.getElementById(group.getAttribute('aria-describedby'));
expect(description).toHaveTextContent('Help text');
});
it('should support error message', function () {
let {getByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Favorite Pet" errorMessage="Error message" isInvalid>
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
</Provider>
);
let group = getByRole('group');
expect(group).toHaveAttribute('aria-describedby');
let description = document.getElementById(group.getAttribute('aria-describedby'));
expect(description).toHaveTextContent('Error message');
});
it('supports form reset', async () => {
function Test() {
let [value, setValue] = React.useState(['dogs']);
return (
<Provider theme={theme}>
<form>
<CheckboxGroup name="pets" label="Pets" value={value} onChange={setValue}>
<Checkbox value="dogs">Dogs</Checkbox>
<Checkbox value="cats">Cats</Checkbox>
<Checkbox value="dragons">Dragons</Checkbox>
</CheckboxGroup>
<input type="reset" data-testid="reset" />
</form>
</Provider>
);
}
let {getAllByRole, getByTestId} = render(<Test />);
let checkboxes = getAllByRole('checkbox');
expect(checkboxes[0]).toBeChecked();
expect(checkboxes[1]).not.toBeChecked();
expect(checkboxes[2]).not.toBeChecked();
await user.click(checkboxes[1]);
expect(checkboxes[0]).toBeChecked();
expect(checkboxes[1]).toBeChecked();
expect(checkboxes[2]).not.toBeChecked();
let button = getByTestId('reset');
await user.click(button);
expect(checkboxes[0]).toBeChecked();
expect(checkboxes[1]).not.toBeChecked();
expect(checkboxes[2]).not.toBeChecked();
});
describe('validation', () => {
describe('validationBehavior=native', () => {
it('supports group level isRequired', async () => {
let {getAllByRole, getByRole, getByTestId} = render(
<Provider theme={theme}>
<Form data-testid="form">
<CheckboxGroup label="Agree to the following" isRequired validationBehavior="native">
<Checkbox value="terms">Terms and conditions</Checkbox>
<Checkbox value="cookies">Cookies</Checkbox>
<Checkbox value="privacy">Privacy policy</Checkbox>
</CheckboxGroup>
</Form>
</Provider>
);
let group = getByRole('group');
expect(group).not.toHaveAttribute('aria-describedby');
let checkboxes = getAllByRole('checkbox');
for (let input of checkboxes) {
expect(input).toHaveAttribute('required');
expect(input).not.toHaveAttribute('aria-required');
expect(input.validity.valid).toBe(false);
}
act(() => {getByTestId('form').checkValidity();});
expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent('Constraints not satisfied');
await user.click(checkboxes[0]);
for (let input of checkboxes) {
expect(input).not.toHaveAttribute('required');
expect(input).not.toHaveAttribute('aria-required');
expect(input.validity.valid).toBe(true);
}
expect(group).not.toHaveAttribute('aria-describedby');
});
it('supports checkbox level isRequired', async () => {
let {getAllByRole, getByRole, getByTestId} = render(
<Provider theme={theme}>
<Form data-testid="form">
<CheckboxGroup label="Agree to the following" validationBehavior="native">
<Checkbox value="terms" isRequired>Terms and conditions</Checkbox>
<Checkbox value="cookies" isRequired>Cookies</Checkbox>
<Checkbox value="privacy">Privacy policy</Checkbox>
</CheckboxGroup>
</Form>
</Provider>
);
let group = getByRole('group');
expect(group).not.toHaveAttribute('aria-describedby');
let checkboxes = getAllByRole('checkbox');
for (let input of checkboxes.slice(0, 2)) {
expect(input).toHaveAttribute('required');
expect(input).not.toHaveAttribute('aria-required');
expect(input.validity.valid).toBe(false);
}
expect(checkboxes[2]).not.toHaveAttribute('required');
expect(checkboxes[2]).not.toHaveAttribute('aria-required');
expect(checkboxes[2].validity.valid).toBe(true);
act(() => {getByTestId('form').checkValidity();});
expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent('Constraints not satisfied');
expect(document.activeElement).toBe(checkboxes[0]);
await user.click(checkboxes[0]);
expect(checkboxes[0].validity.valid).toBe(true);
expect(checkboxes[1].validity.valid).toBe(false);
expect(group).toHaveAttribute('aria-describedby');
await user.click(checkboxes[1]);
expect(checkboxes[1].validity.valid).toBe(true);
expect(group).not.toHaveAttribute('aria-describedby');
});
it('supports group level validate function', async () => {
let {getAllByRole, getByRole, getByTestId} = render(
<Provider theme={theme}>
<Form data-testid="form">
<CheckboxGroup label="Agree to the following" validationBehavior="native" validate={v => v.length < 3 ? 'You must accept all terms' : null}>
<Checkbox value="terms">Terms and conditions</Checkbox>
<Checkbox value="cookies">Cookies</Checkbox>
<Checkbox value="privacy">Privacy policy</Checkbox>
</CheckboxGroup>
</Form>
</Provider>
);
let group = getByRole('group');
expect(group).not.toHaveAttribute('aria-describedby');
let checkboxes = getAllByRole('checkbox');
for (let input of checkboxes) {
expect(input).not.toHaveAttribute('required');
expect(input).not.toHaveAttribute('aria-required');
expect(input.validity.valid).toBe(false);
}
act(() => {getByTestId('form').checkValidity();});
expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent(['You must accept all terms']);
expect(document.activeElement).toBe(checkboxes[0]);
await user.click(checkboxes[0]);
expect(group).toHaveAttribute('aria-describedby');
for (let input of checkboxes) {
expect(input.validity.valid).toBe(false);
}
await user.click(checkboxes[1]);
expect(group).toHaveAttribute('aria-describedby');
for (let input of checkboxes) {
expect(input.validity.valid).toBe(false);
}
await user.click(checkboxes[2]);
expect(group).not.toHaveAttribute('aria-describedby');
for (let input of checkboxes) {
expect(input.validity.valid).toBe(true);
}
});
it('supports checkbox level validate function', async () => {
let {getAllByRole, getByRole, getByTestId} = render(
<Provider theme={theme}>
<Form data-testid="form">
<CheckboxGroup label="Agree to the following" validationBehavior="native">
<Checkbox value="terms" validate={v => !v ? 'You must accept the terms.' : null}>Terms and conditions</Checkbox>
<Checkbox value="cookies" validate={v => !v ? 'You must accept the cookies.' : null}>Cookies</Checkbox>
<Checkbox value="privacy">Privacy policy</Checkbox>
</CheckboxGroup>
</Form>
</Provider>
);
let group = getByRole('group');
expect(group).not.toHaveAttribute('aria-describedby');
let checkboxes = getAllByRole('checkbox');
expect(checkboxes[0].validity.valid).toBe(false);
expect(checkboxes[1].validity.valid).toBe(false);
expect(checkboxes[2].validity.valid).toBe(true);
act(() => {getByTestId('form').checkValidity();});
expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent('You must accept the terms. You must accept the cookies.');
expect(document.activeElement).toBe(checkboxes[0]);
await user.click(checkboxes[0]);
expect(checkboxes[0].validity.valid).toBe(true);
expect(checkboxes[1].validity.valid).toBe(false);
expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent('You must accept the cookies.');
await user.click(checkboxes[1]);
expect(checkboxes[0].validity.valid).toBe(true);
expect(checkboxes[1].validity.valid).toBe(true);
expect(group).not.toHaveAttribute('aria-describedby');
});
it('supports server validation', async () => {
function Test() {
let [serverErrors, setServerErrors] = React.useState({});
let onSubmit = e => {
e.preventDefault();
setServerErrors({
terms: 'You must accept the terms.'
});
};
return (
<Provider theme={theme}>
<Form onSubmit={onSubmit} validationErrors={serverErrors}>
<CheckboxGroup name="terms" label="Agree to the following" validationBehavior="native">
<Checkbox value="terms">Terms and conditions</Checkbox>
<Checkbox value="cookies">Cookies</Checkbox>
<Checkbox value="privacy">Privacy policy</Checkbox>
</CheckboxGroup>
<Button type="submit">Submit</Button>
</Form>
</Provider>
);
}
let {getAllByRole, getByRole} = render(<Test />);
let group = getByRole('group');
expect(group).not.toHaveAttribute('aria-describedby');
await user.click(getByRole('button'));
expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent('You must accept the terms.');
let checkboxes = getAllByRole('checkbox');
for (let input of checkboxes) {
expect(input.validity.valid).toBe(false);
}
await user.click(checkboxes[0]);
expect(group).not.toHaveAttribute('aria-describedby');
for (let input of checkboxes) {
expect(input.validity.valid).toBe(true);
}
});
it('supports customizing native error messages', async () => {
let {getByRole, getByTestId} = render(
<Provider theme={theme}>
<Form data-testid="form">
<CheckboxGroup label="Agree to the following" isRequired validationBehavior="native" errorMessage={e => e.validationDetails.valueMissing ? 'Please select at least one item' : null}>
<Checkbox value="terms">Terms and conditions</Checkbox>
<Checkbox value="cookies">Cookies</Checkbox>
<Checkbox value="privacy">Privacy policy</Checkbox>
</CheckboxGroup>
</Form>
</Provider>
);
let group = getByRole('group');
expect(group).not.toHaveAttribute('aria-describedby');
act(() => {getByTestId('form').checkValidity();});
expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent('Please select at least one item');
});
});
describe('validationBehavior=aria', () => {
it('supports group level validate function', async () => {
let {getAllByRole, getByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Agree to the following" validate={v => v.length < 3 ? 'You must accept all terms' : null}>
<Checkbox value="terms">Terms and conditions</Checkbox>
<Checkbox value="cookies">Cookies</Checkbox>
<Checkbox value="privacy">Privacy policy</Checkbox>
</CheckboxGroup>
</Provider>
);
let group = getByRole('group');
expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent('You must accept all terms');
let checkboxes = getAllByRole('checkbox');
for (let input of checkboxes) {
expect(input).toHaveAttribute('aria-invalid', 'true');
expect(input.validity.valid).toBe(true);
}
await user.click(checkboxes[0]);
expect(group).toHaveAttribute('aria-describedby');
await user.click(checkboxes[1]);
expect(group).toHaveAttribute('aria-describedby');
await user.click(checkboxes[2]);
expect(group).not.toHaveAttribute('aria-describedby');
});
it('supports checkbox level validate function', async () => {
let {getAllByRole, getByRole} = render(
<Provider theme={theme}>
<CheckboxGroup label="Agree to the following">
<Checkbox value="terms" validate={v => !v ? 'You must accept the terms.' : null}>Terms and conditions</Checkbox>
<Checkbox value="cookies" validate={v => !v ? 'You must accept the cookies.' : null}>Cookies</Checkbox>
<Checkbox value="privacy">Privacy policy</Checkbox>
</CheckboxGroup>
</Provider>
);
let checkboxes = getAllByRole('checkbox');
for (let input of checkboxes) {
if (input.value !== 'privacy') {
expect(input).toHaveAttribute('aria-invalid', 'true');
}
expect(input.validity.valid).toBe(true);
}
let group = getByRole('group');
expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent('You must accept the terms. You must accept the cookies.');
await user.click(checkboxes[0]);
expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent('You must accept the cookies.');
await user.click(checkboxes[1]);
expect(group).not.toHaveAttribute('aria-describedby');
});
it('supports server validation', async () => {
let {getAllByRole, getByRole} = render(
<Provider theme={theme}>
<Form validationErrors={{terms: 'You must accept the terms'}}>
<CheckboxGroup name="terms" label="Agree to the following">
<Checkbox value="terms">Terms and conditions</Checkbox>
<Checkbox value="cookies">Cookies</Checkbox>
<Checkbox value="privacy">Privacy policy</Checkbox>
</CheckboxGroup>
</Form>
</Provider>
);
let group = getByRole('group');
expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent('You must accept the terms');
let checkboxes = getAllByRole('checkbox');
for (let input of checkboxes) {
expect(input).toHaveAttribute('aria-invalid', 'true');
}
await user.click(checkboxes[0]);
expect(group).not.toHaveAttribute('aria-describedby');
});
});
});
});