-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathComboBox.test.tsx
More file actions
1866 lines (1575 loc) · 56.1 KB
/
Copy pathComboBox.test.tsx
File metadata and controls
1866 lines (1575 loc) · 56.1 KB
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import React from 'react'
import { screen, render, waitFor, within } from '@testing-library/react'
import { userEvent } from '@testing-library/user-event'
import { ComboBox, ComboBoxOption, ComboBoxRef } from './ComboBox'
import { TextInput } from '../TextInput/TextInput'
import { fruits, veggies } from './foods'
/*
Source of truth for combo box behavior is USWDS storybook examples and tests. For more:
- https://designsystem.digital.gov/form-controls/03-combo-box/
- https://github.com/uswds/uswds/tree/7a89611fe649650922e4d431b78c39fed6a867e1/spec/unit/combo-box
*/
const fruitOptions: ComboBoxOption[] = Object.entries(fruits).map(
([value, key]) => ({
value: value,
label: key,
})
)
const veggieOptions: ComboBoxOption[] = Object.entries(veggies).map(
([value, key]) => ({
value: value,
label: key,
})
)
const veggieCustomOptions: ComboBoxOption[] = Object.entries(veggies).map(
([value, key]) => ({
value: value,
label: key,
render: () => {
return (
<div className="padding-2 border border-base-lighter radius-md bg-white">
<div className="font-sans-md text-bold">
{value}
</div>
<div className="font-sans-sm text-base">
Category: Veggies
</div>
</div>
)
}
})
)
describe('ComboBox component', () => {
it('renders the expected markup without errors', () => {
render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
const comboBoxContainer = screen.getByTestId('combo-box')
expect(comboBoxContainer).toBeInTheDocument()
expect(comboBoxContainer).toHaveClass('usa-combo-box')
expect(comboBoxContainer).not.toHaveClass('usa-combo-box--pristine')
expect(comboBoxContainer).toHaveAttribute('data-enhanced', 'true')
const comboBoxSelect = screen.getByTestId('combo-box-select')
expect(comboBoxSelect).toBeInstanceOf(HTMLSelectElement)
expect(comboBoxSelect).toHaveAttribute('aria-hidden', 'true')
expect(comboBoxSelect).toHaveClass(
'usa-select usa-sr-only usa-combo-box__select'
)
const comboBoxInput = screen.getByRole('combobox')
expect(comboBoxInput).toBeInTheDocument()
expect(comboBoxInput).toBeInstanceOf(HTMLInputElement)
expect(comboBoxInput).toHaveAttribute('aria-owns', 'favorite-fruit--list')
expect(comboBoxInput).toHaveAttribute(
'aria-controls',
'favorite-fruit--list'
)
expect(comboBoxInput).toHaveAttribute('aria-autocomplete', 'list')
expect(comboBoxInput).toHaveAttribute(
'aria-describedby',
'favorite-fruit--assistiveHint'
)
expect(comboBoxInput).toHaveAttribute('aria-expanded', 'false')
expect(comboBoxInput).toHaveAttribute('autocapitalize', 'off')
expect(comboBoxInput).toHaveAttribute('autocomplete', 'off')
expect(comboBoxInput).toHaveAttribute('type', 'text')
const comboBoxList = screen.getByTestId('combo-box-option-list')
expect(comboBoxList).toBeInstanceOf(HTMLUListElement)
expect(comboBoxList).toHaveAttribute('id', 'favorite-fruit--list')
expect(comboBoxList).toHaveAttribute('role', 'listbox')
expect(comboBoxList).not.toBeVisible()
})
it('renders the expected markup with a default value', () => {
render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
defaultValue="avocado"
/>
)
const comboBoxContainer = screen.getByTestId('combo-box')
expect(comboBoxContainer).toHaveClass('usa-combo-box--pristine')
const comboBoxSelect = screen.getByTestId('combo-box-select')
expect(comboBoxSelect).toHaveValue('avocado')
const comboBoxInput = screen.getByRole('combobox')
expect(comboBoxInput).toHaveValue('Avocado')
})
it('updates options when prop changes', async () => {
const Wrapper = (props: { options: ComboBoxOption[] }) => {
return (
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={props.options}
onChange={vi.fn()}
/>
)
}
const { rerender } = render(<Wrapper options={fruitOptions} />)
const comboBoxSelect = screen.getByTestId('combo-box-select')
expect(comboBoxSelect).toHaveValue(fruitOptions[0].value)
rerender(<Wrapper options={veggieOptions} />)
expect(comboBoxSelect).toHaveValue(veggieOptions[0].value)
})
describe('toggling the list', () => {
it('renders all options when the list is open', async () => {
const fruitAbridged = fruitOptions.slice(0, 3)
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitAbridged}
onChange={vi.fn()}
/>
)
await userEvent.click(getByTestId('combo-box-toggle'))
expect(screen.getAllByRole('option')).toHaveLength(fruitAbridged.length)
fruitAbridged.forEach((item, index) => {
const optionEl = screen.getByRole('option', { name: item.label })
expect(optionEl).toBeInTheDocument()
expect(optionEl).toHaveAttribute('value', item.value)
expect(optionEl).toHaveAttribute(
'aria-setsize',
`${fruitAbridged.length}`
)
expect(optionEl).toHaveAttribute('aria-posinset', `${index + 1}`)
expect(optionEl).toHaveAttribute(
'id',
`favorite-fruit--list--option-${index}`
)
})
})
it('shows options list when input toggle clicked', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
await userEvent.click(getByTestId('combo-box-toggle'))
expect(getByTestId('combo-box-option-list')).toBeVisible()
})
it('shows list when input is clicked', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
await userEvent.click(getByTestId('combo-box-input'))
expect(getByTestId('combo-box-option-list')).toBeVisible()
})
it('shows list when input is typed into', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
await userEvent.type(getByTestId('combo-box-input'), 'b')
expect(getByTestId('combo-box-option-list')).toBeVisible()
})
it('highlights the first option when opening the menu, when no default value exists', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
const firstItem = getByTestId('combo-box-option-list').children[0]
const inputEl = getByTestId('combo-box-input')
await userEvent.click(getByTestId('combo-box-toggle'))
expect(firstItem).toBeVisible()
expect(firstItem).not.toHaveFocus()
expect(firstItem).toHaveClass('usa-combo-box__list-option--focused')
expect(inputEl).toHaveAttribute('aria-activedescendant', firstItem.id)
})
it('highlights the default value when opening the menu, when one exists', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
defaultValue="avocado"
/>
)
await userEvent.click(getByTestId('combo-box-input'))
expect(getByTestId('combo-box-option-avocado')).toHaveAttribute(
'aria-selected',
'true'
)
})
})
it('can be disabled', () => {
render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={[]}
onChange={vi.fn()}
disabled={true}
/>
)
expect(screen.getByLabelText('Clear the select contents')).toBeDisabled()
expect(screen.getByLabelText('Clear the select contents')).not.toBeVisible()
expect(screen.getByRole('combobox')).toBeDisabled()
expect(
screen.getByRole('button', { name: 'Toggle the dropdown list' })
).toBeDisabled()
expect(screen.getByTestId('combo-box-select')).not.toBeDisabled()
})
it('does not show the list when clicking the disabled component', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
disabled={true}
/>
)
await userEvent.click(getByTestId('combo-box-toggle'))
expect(getByTestId('combo-box-option-list')).not.toBeVisible()
})
it('renders input with default value if passed in', () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
defaultValue="apple"
/>
)
expect(getByTestId('combo-box-input')).toHaveValue('Apple')
expect(getByTestId('combo-box-option-list')).not.toBeVisible()
})
it('renders custom JSX options', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-veggie"
name="favorite-veggie"
options={veggieCustomOptions}
onChange={vi.fn()}
/>
)
await userEvent.click(getByTestId('combo-box-toggle'))
expect(screen.getAllByRole('option')).toHaveLength(veggieCustomOptions.length)
veggieCustomOptions.forEach(item => {
const listEl = screen.getByTestId('combo-box-option-' + item.value)
expect(listEl).toBeInTheDocument()
expect(listEl).toHaveAttribute('value', item.value)
const categoryEl = within(listEl).getByText('Category: Veggies')
expect(categoryEl).toBeInTheDocument()
})
})
describe('with custom props', () => {
it('renders select with custom props if passed in', () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
selectProps={{ required: true, role: 'testing' }}
/>
)
const comboBoxSelect = getByTestId('combo-box-select')
expect(comboBoxSelect).toHaveAttribute('required')
expect(comboBoxSelect).toHaveAttribute('role', 'testing')
})
it('renders input with custom props if passed in', () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
inputProps={{ required: true, type: 'url' }}
/>
)
const comboBoxInput = getByTestId('combo-box-input')
expect(comboBoxInput).toHaveAttribute('required')
expect(comboBoxInput).toHaveAttribute('type', 'url')
})
it('allows a custom input onChange handler to be called', async () => {
const mockOnInputChange = vi.fn()
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
inputProps={{ onChange: mockOnInputChange }}
/>
)
const input = getByTestId('combo-box-input')
await userEvent.type(input, 'x')
expect(mockOnInputChange).toHaveBeenCalled()
})
it('renders list with custom props if passed in', () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
ulProps={{ 'aria-labelledby': 'test-label-id' }}
/>
)
const comboBoxOptionList = getByTestId('combo-box-option-list')
expect(comboBoxOptionList).toHaveAttribute(
'aria-labelledby',
'test-label-id'
)
})
})
describe('scrolling to a focused option', () => {
it('scrolls options list to the very top when the menu opens if nothing is selected', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
const listEl = getByTestId('combo-box-option-list')
vi.spyOn(listEl, 'offsetHeight', 'get').mockReturnValue(205)
listEl.scrollTop = 2000 // Scroll list 2000px down
await userEvent.click(getByTestId('combo-box-toggle'))
expect(listEl.scrollTop).toEqual(0)
})
it('scrolls down to the selected option when the list is opened', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
defaultValue={'mango'}
/>
)
const mango = getByTestId('combo-box-option-mango')
const listEl = getByTestId('combo-box-option-list')
vi.spyOn(mango, 'offsetTop', 'get').mockReturnValue(1365)
vi.spyOn(mango, 'offsetHeight', 'get').mockReturnValue(39)
vi.spyOn(listEl, 'offsetHeight', 'get').mockReturnValue(205)
listEl.scrollTop = 0 // Scroll list to the top
await userEvent.click(getByTestId('combo-box-toggle'))
expect(mango).toHaveClass(
'usa-combo-box__list-option--focused usa-combo-box__list-option--selected'
)
expect(listEl.scrollTop).toEqual(1199)
})
it('scrolls up to the selected option when the list is opened', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
defaultValue={'mango'}
/>
)
const mango = getByTestId('combo-box-option-mango')
const listEl = getByTestId('combo-box-option-list')
vi.spyOn(mango, 'offsetTop', 'get').mockReturnValue(1365)
vi.spyOn(mango, 'offsetHeight', 'get').mockReturnValue(39)
vi.spyOn(listEl, 'offsetHeight', 'get').mockReturnValue(205)
listEl.scrollTop = 2292 // Scroll list 2292px down
await userEvent.click(getByTestId('combo-box-toggle'))
expect(mango).toHaveClass(
'usa-combo-box__list-option--focused usa-combo-box__list-option--selected'
)
expect(listEl.scrollTop).toEqual(1365)
})
})
describe('filtering', () => {
it('shows all options on initial load when no default value exists', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
await userEvent.click(getByTestId('combo-box-input'))
expect(getByTestId('combo-box-option-list').children.length).toBe(
fruitOptions.length
)
})
it('shows all options on initial load when a default value exists', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
defaultValue="banana"
onChange={vi.fn()}
/>
)
await userEvent.click(getByTestId('combo-box-input'))
expect(getByTestId('combo-box-option-list').children.length).toBe(
fruitOptions.length
)
})
it('filters the options list after a character is typed', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
const input = getByTestId('combo-box-input')
await userEvent.type(input, 'a')
expect(getByTestId('combo-box-option-list').children.length).toEqual(43)
})
it('persists filter options if dropdown is closed and open without selection', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
const input = getByTestId('combo-box-input')
await userEvent.type(input, 'yu')
await userEvent.click(getByTestId('combo-box-toggle'))
expect(getByTestId('combo-box-option-list').children.length).toEqual(1)
await userEvent.click(getByTestId('combo-box-toggle'))
expect(getByTestId('combo-box-option-list').children.length).toEqual(1)
})
it('clears filter when item selected', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
const input = getByTestId('combo-box-input')
await userEvent.type(input, 'yu')
await userEvent.click(getByTestId('combo-box-option-yuzu'))
expect(getByTestId('combo-box-option-list').children.length).toEqual(
fruitOptions.length
)
})
it('clears filters when ComboBox is un-focused and no option was selected (implicitly clearing the ComboBox)', async () => {
const { getByTestId } = render(
<>
<div data-testid="outside" />
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
</>
)
const list = getByTestId('combo-box-option-list')
const input = getByTestId('combo-box-input')
// Filter the list
await userEvent.type(input, 'Av')
expect(list.children.length).toEqual(2) // Avocado and Guava
// Click somewhere else
await userEvent.click(getByTestId('outside'))
expect(input).toHaveTextContent('')
expect(list.children.length).toEqual(fruitOptions.length)
// Return the combo box
await userEvent.click(input)
expect(input).toHaveTextContent('')
expect(list.children.length).toEqual(fruitOptions.length)
expect(list.children[0]).toHaveClass(
'usa-combo-box__list-option--focused'
)
})
it('shows no results message when there is no match', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
await userEvent.type(getByTestId('combo-box-input'), 'zz')
const firstItem = getByTestId('combo-box-option-list').children[0]
expect(firstItem).not.toHaveFocus()
expect(firstItem).not.toHaveAttribute('tabindex', '0')
expect(firstItem).toHaveTextContent('No results found')
})
it('shows all results when typed value is cleared', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
const input = getByTestId('combo-box-input')
await userEvent.type(input, 'apple')
await userEvent.clear(input)
expect(getByTestId('combo-box-option-list').children.length).toEqual(
fruitOptions.length
)
})
it('resets the list of items if filtered then blurred without selecting an element', async () => {
const { getByTestId } = render(
<>
<div data-testid="outside" />
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
</>
)
const input = getByTestId('combo-box-input')
const optionsList = getByTestId('combo-box-option-list')
await userEvent.type(input, 'apple')
expect(optionsList.children.length).toBeLessThan(fruitOptions.length)
await userEvent.click(getByTestId('outside'))
expect(input).toHaveTextContent('')
expect(optionsList.children.length).toEqual(fruitOptions.length)
})
it('does not hide items when disableFiltering is selected', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
disableFiltering
/>
)
const input = getByTestId('combo-box-input')
await userEvent.click(input)
await userEvent.type(input, 'zzzzzzzzzz')
expect(getByTestId('combo-box-option-list').children.length).toBe(
fruitOptions.length
)
})
})
describe('clear button', () => {
it('is visible when input has content', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
await userEvent.type(getByTestId('combo-box-input'), 'app')
await userEvent.click(getByTestId('combo-box-option-apple'))
expect(getByTestId('combo-box-clear-button')).toBeVisible()
})
it('resets the filter on click', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
await userEvent.type(getByTestId('combo-box-input'), 'b')
await userEvent.click(getByTestId('combo-box-clear-button'))
expect(getByTestId('combo-box-clear-button')).not.toBeVisible()
expect(getByTestId('combo-box-option-list').children.length).toBe(
fruitOptions.length
)
})
it('calls onChange prop with undefined on click', async () => {
const onChange = vi.fn()
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={onChange}
/>
)
await userEvent.click(getByTestId('combo-box-clear-button'))
expect(onChange).toHaveBeenCalledWith(undefined)
})
it('clears the input on click', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
await userEvent.type(getByTestId('combo-box-input'), 'b')
await userEvent.click(getByTestId('combo-box-clear-button'))
expect(getByTestId('combo-box-clear-button')).not.toBeVisible()
expect(getByTestId('combo-box-input')).toHaveValue('')
})
it('works as expected when input is loaded with default value', async () => {
const onChange = vi.fn()
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
defaultValue="apple"
onChange={onChange}
/>
)
expect(getByTestId('combo-box-clear-button')).toBeVisible()
await userEvent.click(getByTestId('combo-box-clear-button'))
expect(getByTestId('combo-box-clear-button')).not.toBeVisible()
expect(getByTestId('combo-box-input')).toHaveValue('')
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(2))
expect(onChange).toHaveBeenNthCalledWith(2, undefined)
const comboBoxOptionList = getByTestId('combo-box-option-list')
expect(comboBoxOptionList).not.toBeVisible()
expect(comboBoxOptionList.children.length).toBe(fruitOptions.length)
})
it('remains focused on the input after click', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
await userEvent.type(getByTestId('combo-box-input'), 'b')
expect(getByTestId('combo-box-input')).toHaveFocus()
await userEvent.click(getByTestId('combo-box-clear-button'))
expect(getByTestId('combo-box-input')).toHaveFocus()
})
it('focuses the input after clearing when the when FocusMode is None', async () => {
const { getByTestId } = render(
<>
<button type="button" data-testid="outside">
Testing
</button>
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
</>
)
await userEvent.type(getByTestId('combo-box-input'), 'b')
await userEvent.click(getByTestId('outside'))
await userEvent.click(getByTestId('combo-box-clear-button'))
await waitFor(() => {
expect(getByTestId('combo-box-input')).toHaveFocus()
})
})
})
it('clears input value and closes list when an incomplete item is remaining on blur', async () => {
const { getByTestId } = render(
<>
<div data-testid="outside" />
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
</>
)
await userEvent.type(getByTestId('combo-box-input'), 'a')
await userEvent.click(getByTestId('outside'))
expect(getByTestId('combo-box-input')).toHaveValue('')
expect(getByTestId('combo-box-input')).toHaveAttribute(
'aria-activedescendant',
''
)
expect(getByTestId('combo-box-option-list')).not.toBeVisible()
})
describe('keyboard actions', () => {
it('clears input when there is no match and enter is pressed', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
await userEvent.type(getByTestId('combo-box-input'), 'zzz{enter}')
const comboBoxInput = getByTestId('combo-box-input')
expect(getByTestId('combo-box-option-list')).not.toBeVisible()
expect(comboBoxInput).toHaveValue('')
expect(comboBoxInput).toHaveFocus()
})
it('clears filter when there is no match and enter is pressed', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
await userEvent.type(getByTestId('combo-box-input'), 'zzz{enter}')
const comboBoxOptionList = getByTestId('combo-box-option-list')
expect(comboBoxOptionList).not.toBeVisible()
expect(comboBoxOptionList.children.length).toBe(fruitOptions.length)
})
it('reverts to the selected option when there is not an exact match and enter is pressed', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
defaultValue="avocado"
onChange={vi.fn()}
/>
)
const input = getByTestId('combo-box-input')
await userEvent.type(input, 'zzz{enter}')
expect(getByTestId('combo-box-option-list')).not.toBeVisible()
expect(input).toHaveValue('Avocado')
expect(input).toHaveFocus()
expect(getByTestId('combo-box-clear-button')).toBeVisible()
})
it('selects the exactly matching option when an item was previously selected and enter is pressed', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
defaultValue="avocado"
onChange={vi.fn()}
/>
)
const input = getByTestId('combo-box-input')
for (let i = 0; i < 'avocado'.length; i++) {
await userEvent.type(input, '{backspace}')
}
await userEvent.type(input, 'Banana{enter}')
expect(getByTestId('combo-box-option-list')).not.toBeVisible()
expect(input).toHaveValue('Banana')
expect(input).toHaveFocus()
expect(getByTestId('combo-box-clear-button')).toBeVisible()
})
it('focuses the first filtered option with tab', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
await userEvent.type(getByTestId('combo-box-input'), 'a')
await userEvent.tab()
const firstItem = getByTestId('combo-box-option-list').children[0]
expect(firstItem).toHaveFocus()
expect(firstItem).toHaveAttribute('tabindex', '0')
})
it('focuses the first option with tab', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
await userEvent.click(getByTestId('combo-box-input')) // open menu
await userEvent.tab()
const appleOption = getByTestId('combo-box-option-apple')
expect(appleOption).toHaveFocus()
expect(appleOption).toHaveAttribute('tabindex', '0')
})
it('selects the focused option with tab', async () => {
const onChange = vi.fn()
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={onChange}
/>
)
// focus mango
await userEvent.type(getByTestId('combo-box-input'), 'mang')
await userEvent.tab()
// select mango
await userEvent.tab()
expect(getByTestId('combo-box-input')).toHaveValue('Mango')
expect(onChange).toHaveBeenLastCalledWith('mango')
})
it('switches focus when there are no filtered options', async () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={vi.fn()}
/>
)
const comboBoxInput = getByTestId('combo-box-input')
await userEvent.type(comboBoxInput, 'zzz')
await userEvent.tab()
expect(comboBoxInput).not.toHaveFocus()
})
it('selects the focused option with enter', async () => {
const onChange = vi.fn()
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={onChange}