@instructure/ui-select 10.19.2-snapshot-3 → 10.19.2-snapshot-4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1316 +0,0 @@
1
- /*
2
- * The MIT License (MIT)
3
- *
4
- * Copyright (c) 2015 - present Instructure, Inc.
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
-
25
- import { createRef } from 'react'
26
- import { render, screen, waitFor } from '@testing-library/react'
27
- import { vi } from 'vitest'
28
- import type { MockInstance } from 'vitest'
29
- import userEvent from '@testing-library/user-event'
30
- import '@testing-library/jest-dom'
31
-
32
- // eslint-disable-next-line no-restricted-imports
33
- import { generateA11yTests } from '@instructure/ui-scripts/lib/test/generateA11yTests'
34
- import { runAxeCheck } from '@instructure/ui-axe-check'
35
- import Select from '../index'
36
- import SelectExamples from '../__examples__/Select.examples'
37
- import * as utils from '@instructure/ui-utils'
38
- import {
39
- IconAddLine,
40
- IconCheckSolid,
41
- IconDownloadSolid,
42
- IconEyeSolid
43
- } from '@instructure/ui-icons'
44
-
45
- type ExampleOption = 'foo' | 'bar' | 'baz'
46
- const defaultOptions: ExampleOption[] = ['foo', 'bar', 'baz']
47
- const getOptions = (
48
- highlighted?: ExampleOption,
49
- selected?: ExampleOption,
50
- disabled?: ExampleOption
51
- ) =>
52
- defaultOptions.map((opt) => (
53
- <Select.Option
54
- id={opt}
55
- key={opt}
56
- value={opt}
57
- isHighlighted={opt === highlighted}
58
- isSelected={opt === selected}
59
- isDisabled={opt === disabled}
60
- >
61
- {opt}
62
- </Select.Option>
63
- ))
64
-
65
- type ExampleOptionWithContent = 'opt1' | 'op2' | 'opt3'
66
- type GroupExampleOptionWithContent =
67
- | 'opt1'
68
- | 'op2'
69
- | 'opt3'
70
- | 'opt4'
71
- | 'op5'
72
- | 'opt6'
73
-
74
- type groupOptionWithBeforeContent = {
75
- id: string
76
- label: string
77
- renderBeforeLabel: string | React.ReactNode
78
- }
79
- type groupOptionWithAfterContent = {
80
- id: string
81
- label: string
82
- renderAfterLabel: string | React.ReactNode
83
- }
84
-
85
- const optionsWithBeforeContent = [
86
- {
87
- id: 'opt1',
88
- label: 'Text',
89
- renderBeforeLabel: 'XY'
90
- },
91
- {
92
- id: 'opt2',
93
- label: 'Icon',
94
- renderBeforeLabel: <IconCheckSolid />
95
- },
96
- {
97
- id: 'opt3',
98
- label: 'Colored Icon',
99
- renderBeforeLabel: <IconEyeSolid />
100
- }
101
- ]
102
-
103
- const groupOptionsWithBeforeContent: {
104
- [key: string]: groupOptionWithBeforeContent[]
105
- } = {
106
- Options1: [
107
- {
108
- id: 'opt1',
109
- label: 'Text1',
110
- renderBeforeLabel: 'XY'
111
- },
112
- {
113
- id: 'opt2',
114
- label: 'Icon1',
115
- renderBeforeLabel: <IconCheckSolid />
116
- },
117
- {
118
- id: 'opt3',
119
- label: 'Colored Icon1',
120
- renderBeforeLabel: <IconEyeSolid />
121
- }
122
- ],
123
- Options2: [
124
- {
125
- id: 'opt4',
126
- label: 'Text2',
127
- renderBeforeLabel: 'AB'
128
- },
129
- {
130
- id: 'opt5',
131
- label: 'Icon2',
132
- renderBeforeLabel: <IconAddLine />
133
- },
134
- {
135
- id: 'opt6',
136
- label: 'Colored Icon2',
137
- renderBeforeLabel: <IconDownloadSolid />
138
- }
139
- ]
140
- }
141
-
142
- const optionsWithAfterContent = [
143
- {
144
- id: 'opt1',
145
- label: 'Text',
146
- renderAfterLabel: 'XY'
147
- },
148
- {
149
- id: 'opt2',
150
- label: 'Icon',
151
- renderAfterLabel: <IconCheckSolid />
152
- },
153
- {
154
- id: 'opt3',
155
- label: 'Colored Icon',
156
- renderAfterLabel: <IconEyeSolid />
157
- }
158
- ]
159
-
160
- const groupOptionsWithAfterContent: {
161
- [key: string]: groupOptionWithAfterContent[]
162
- } = {
163
- Options1: [
164
- {
165
- id: 'opt1',
166
- label: 'Text1',
167
- renderAfterLabel: 'XY'
168
- },
169
- {
170
- id: 'opt2',
171
- label: 'Icon1',
172
- renderAfterLabel: <IconCheckSolid />
173
- },
174
- {
175
- id: 'opt3',
176
- label: 'Colored Icon1',
177
- renderAfterLabel: <IconEyeSolid />
178
- }
179
- ],
180
- Options2: [
181
- {
182
- id: 'opt4',
183
- label: 'Text2',
184
- renderAfterLabel: 'AB'
185
- },
186
- {
187
- id: 'opt5',
188
- label: 'Icon2',
189
- renderAfterLabel: <IconAddLine />
190
- },
191
- {
192
- id: 'opt6',
193
- label: 'Colored Icon2',
194
- renderAfterLabel: <IconDownloadSolid />
195
- }
196
- ]
197
- }
198
-
199
- const optionsWithBeforeAndAfterContent = [
200
- {
201
- id: 'opt1',
202
- label: 'Text',
203
- renderBeforeLabel: 'XY',
204
- renderAfterLabel: 'ZZ'
205
- },
206
- {
207
- id: 'opt2',
208
- label: 'Icon',
209
- renderAfterLabel: <IconCheckSolid />,
210
- renderBeforeLabel: <IconEyeSolid />
211
- },
212
- {
213
- id: 'opt3',
214
- label: 'Colored Icon',
215
- renderBeforeLabel: <IconEyeSolid />,
216
- renderAfterLabel: <IconCheckSolid />
217
- }
218
- ]
219
-
220
- const getOptionsWithBeforeContent = (selected: ExampleOptionWithContent) =>
221
- optionsWithBeforeContent.map((opt) => (
222
- <Select.Option {...opt} key={opt.id} isSelected={opt.id === selected} />
223
- ))
224
-
225
- const getGroupOptionsWithBeforeContent = (
226
- selected: GroupExampleOptionWithContent
227
- ) => {
228
- return Object.keys(groupOptionsWithBeforeContent).map((key, index) => {
229
- return (
230
- <Select.Group key={index} renderLabel={key}>
231
- {groupOptionsWithBeforeContent[key].map(
232
- (opt: groupOptionWithBeforeContent) => (
233
- <Select.Option
234
- id={opt.id}
235
- key={opt.id}
236
- value={opt.label}
237
- renderBeforeLabel={opt.renderBeforeLabel}
238
- isSelected={opt.id === selected}
239
- ></Select.Option>
240
- )
241
- )}
242
- </Select.Group>
243
- )
244
- })
245
- }
246
-
247
- const getOptionsWithAfterContent = (selected: ExampleOptionWithContent) =>
248
- optionsWithAfterContent.map((opt) => (
249
- <Select.Option
250
- id={opt.id}
251
- key={opt.id}
252
- value={opt.label}
253
- renderAfterLabel={opt.renderAfterLabel}
254
- isSelected={opt.id === selected}
255
- ></Select.Option>
256
- ))
257
-
258
- const getGroupOptionsWithAfterContent = (
259
- selected: GroupExampleOptionWithContent
260
- ) => {
261
- return Object.keys(groupOptionsWithAfterContent).map((key, index) => {
262
- return (
263
- <Select.Group key={index} renderLabel={key}>
264
- {groupOptionsWithAfterContent[key].map(
265
- (opt: groupOptionWithAfterContent) => (
266
- <Select.Option
267
- id={opt.id}
268
- key={opt.id}
269
- value={opt.label}
270
- renderAfterLabel={opt.renderAfterLabel}
271
- isSelected={opt.id === selected}
272
- ></Select.Option>
273
- )
274
- )}
275
- </Select.Group>
276
- )
277
- })
278
- }
279
-
280
- const getOptionsWithBeforeAndAfterContent = (
281
- selected: ExampleOptionWithContent
282
- ) =>
283
- optionsWithBeforeAndAfterContent.map((opt) => (
284
- <Select.Option
285
- id={opt.id}
286
- key={opt.id}
287
- value={opt.label}
288
- renderBeforeLabel={opt.renderBeforeLabel}
289
- renderAfterLabel={opt.renderAfterLabel}
290
- isSelected={opt.id === selected}
291
- ></Select.Option>
292
- ))
293
-
294
- vi.mock('@instructure/ui-utils', async (importOriginal) => {
295
- const originalModule = (await importOriginal()) as any
296
- return {
297
- __esModule: true,
298
- ...originalModule,
299
- isSafari: vi.fn(() => true)
300
- }
301
- })
302
-
303
- const mockUtils = utils as any
304
-
305
- describe('<Select />', () => {
306
- let consoleErrorMock: ReturnType<typeof vi.spyOn>
307
-
308
- beforeEach(() => {
309
- // Mocking console to prevent test output pollution and expect for messages
310
- consoleErrorMock = vi
311
- .spyOn(console, 'error')
312
- .mockImplementation(() => {}) as MockInstance
313
- })
314
-
315
- afterEach(() => {
316
- consoleErrorMock.mockRestore()
317
- })
318
-
319
- it('should not crash for weird option ids', async () => {
320
- const weirdID = 'some_`w@ei:r|!@#$%^&*(()|\\.l/d"id'
321
- vi.useFakeTimers()
322
- const { container } = render(
323
- <Select
324
- renderLabel="Choose an option"
325
- scrollToHighlightedOption
326
- isShowingOptions
327
- >
328
- <Select.Option id={weirdID} key="1" value="2" isHighlighted={true}>
329
- op1
330
- </Select.Option>
331
- <Select.Option id="sdfsdfsd" key="2" value="2">
332
- op2
333
- </Select.Option>
334
- </Select>
335
- )
336
- vi.advanceTimersToNextFrame()
337
- vi.useRealTimers()
338
- const input = container.querySelector('input')
339
- expect(input).toBeInTheDocument()
340
- })
341
-
342
- it('should have role button in Safari without onInputChange', async () => {
343
- const { container } = render(
344
- <Select renderLabel="Choose an option">{getOptions()}</Select>
345
- )
346
- const input = container.querySelector('input')
347
- expect(input).toHaveAttribute('role', 'button')
348
- })
349
-
350
- it('should have role combobox in different browsers than Safari without onInputChange', async () => {
351
- mockUtils.isSafari = vi.fn(() => false)
352
-
353
- const { container } = render(
354
- <Select renderLabel="Choose an option">{getOptions()}</Select>
355
- )
356
- const input = container.querySelector('input')
357
- expect(input).toHaveAttribute('role', 'combobox')
358
- })
359
-
360
- it('should have role combobox with onInputChange', async () => {
361
- const { container } = render(
362
- <Select renderLabel="Choose an option" onInputChange={() => {}}>
363
- {getOptions()}
364
- </Select>
365
- )
366
- const input = container.querySelector('input')
367
- expect(input).toHaveAttribute('role', 'combobox')
368
- })
369
-
370
- it('should render an input and a list', async () => {
371
- const { container } = render(
372
- <Select
373
- renderLabel="Choose an option"
374
- isShowingOptions
375
- data-testid="subidubi"
376
- >
377
- {getOptions()}
378
- </Select>
379
- )
380
-
381
- const select = container.querySelector('span[class$="-select"]')
382
- const label = screen.getByLabelText('Choose an option')
383
- const input = container.querySelector('input[id^="Select_"]')
384
- const list = screen.getByRole('listbox')
385
-
386
- const options = screen.getAllByRole('option')
387
- const optionsCount = defaultOptions.length
388
-
389
- expect(select).toBeInTheDocument()
390
- expect(label).toBeInTheDocument()
391
- expect(input).toBeInTheDocument()
392
- expect(list).toBeInTheDocument()
393
-
394
- expect(options.length).toBe(optionsCount)
395
- expect(options[0]).toHaveTextContent(defaultOptions[0])
396
- expect(options[1]).toHaveTextContent(defaultOptions[1])
397
- expect(options[2]).toHaveTextContent(defaultOptions[2])
398
- })
399
-
400
- it('should render groups', async () => {
401
- render(
402
- <Select renderLabel="Choose an option" isShowingOptions>
403
- <Select.Option id="0">ungrouped option one</Select.Option>
404
- <Select.Group renderLabel="Group one">
405
- <Select.Option id="1">grouped option one</Select.Option>
406
- </Select.Group>
407
- <Select.Group renderLabel="Group two">
408
- <Select.Option id="2">grouped option two</Select.Option>
409
- </Select.Group>
410
- <Select.Option id="3">ungrouped option two</Select.Option>
411
- </Select>
412
- )
413
-
414
- const groups = screen.getAllByRole('group')
415
- const groupLabel = screen.getByText('Group one')
416
-
417
- expect(groupLabel).toHaveAttribute('role', 'presentation')
418
- expect(groups[0].getAttribute('aria-labelledby')).toEqual(
419
- groupLabel.getAttribute('id')
420
- )
421
- expect(groups.length).toBe(2)
422
- })
423
-
424
- it('should ignore invalid children', async () => {
425
- render(
426
- <Select renderLabel="Choose an option" isShowingOptions>
427
- <Select.Option id="0">valid</Select.Option>
428
- <div>invalid</div>
429
- </Select>
430
- )
431
- const invalidChild = screen.queryByText('invalid')
432
-
433
- expect(invalidChild).not.toBeInTheDocument()
434
-
435
- const expectedErrorMessage = 'Expected one of Group, Option'
436
-
437
- expect(consoleErrorMock).toHaveBeenCalledWith(
438
- expect.any(String),
439
- expect.any(String),
440
- expect.stringContaining(expectedErrorMessage),
441
- expect.any(String)
442
- )
443
- })
444
-
445
- it('should provide a focus method', async () => {
446
- const ref = createRef<Select>()
447
- render(
448
- <Select renderLabel="Choose an option" ref={ref}>
449
- {getOptions()}
450
- </Select>
451
- )
452
-
453
- const input = screen.getByLabelText('Choose an option')
454
-
455
- ref.current?.focus()
456
-
457
- await waitFor(() => {
458
- expect(document.activeElement).toBe(input)
459
- })
460
- })
461
-
462
- it('should provide a focused getter', async () => {
463
- const ref = createRef<Select>()
464
-
465
- render(
466
- <Select renderLabel="Choose an option" ref={ref}>
467
- {getOptions()}
468
- </Select>
469
- )
470
- expect(ref.current?.focused).toBe(false)
471
-
472
- ref.current?.focus()
473
-
474
- await waitFor(() => {
475
- expect(ref.current?.focused).toBe(true)
476
- })
477
- })
478
-
479
- describe('input', () => {
480
- it('should render with a generated id by default', () => {
481
- const { container } = render(<Select renderLabel="Choose an option" />)
482
- const input = container.querySelector('input[id^="Select_"]')
483
-
484
- expect(input).toBeInTheDocument()
485
- expect(input).toHaveAttribute('id', expect.stringContaining('Select_'))
486
- })
487
-
488
- it('should render with a custom id if given', () => {
489
- const { container } = render(
490
- <Select renderLabel="Choose an option" id="customSelect" />
491
- )
492
- const input = container.querySelector('input[id^="customSelect"]')
493
-
494
- expect(input).toBeInTheDocument()
495
- expect(input!.getAttribute('id')).toEqual('customSelect')
496
- })
497
-
498
- it('should render readonly when interaction="enabled" with no onInputChange', () => {
499
- render(<Select renderLabel="Choose an option" />)
500
- const input = screen.getByLabelText('Choose an option')
501
-
502
- expect(input).toHaveAttribute('readonly')
503
- expect(input).not.toHaveAttribute('disabled')
504
- })
505
-
506
- it('should not render readonly when interaction="enabled" with onInputChange', () => {
507
- render(<Select renderLabel="Choose an option" onInputChange={() => {}} />)
508
- const input = screen.getByLabelText('Choose an option')
509
-
510
- expect(input).not.toHaveAttribute('readonly')
511
- expect(input).not.toHaveAttribute('disabled')
512
- })
513
-
514
- it('should render readonly when interaction="readonly"', () => {
515
- render(
516
- <Select
517
- renderLabel="Choose an option"
518
- interaction="readonly"
519
- onInputChange={() => {}}
520
- />
521
- )
522
- const input = screen.getByLabelText('Choose an option')
523
-
524
- expect(input).toHaveAttribute('readonly')
525
- expect(input).not.toHaveAttribute('disabled')
526
- })
527
-
528
- it('should render disabled when interaction="disabled"', () => {
529
- render(
530
- <Select
531
- renderLabel="Choose an option"
532
- interaction="disabled"
533
- onInputChange={() => {}}
534
- />
535
- )
536
- const input = screen.getByLabelText('Choose an option')
537
-
538
- expect(input).toHaveAttribute('disabled')
539
- expect(input).not.toHaveAttribute('readonly')
540
- })
541
-
542
- it('should not render readonly when disabled', () => {
543
- render(<Select renderLabel="Choose an option" interaction="disabled" />)
544
- const input = screen.getByLabelText('Choose an option')
545
-
546
- expect(input).toHaveAttribute('disabled')
547
- expect(input).not.toHaveAttribute('readonly')
548
- })
549
-
550
- it('should render required when isRequired={true}', () => {
551
- render(<Select renderLabel="Choose an option" isRequired />)
552
- const input = screen.getByLabelText('Choose an option *')
553
-
554
- expect(input).toHaveAttribute('required')
555
- })
556
-
557
- it('should render with inputValue', () => {
558
- const val = 'hello world'
559
-
560
- render(<Select renderLabel="Choose an option" inputValue={val} />)
561
- const input = screen.getByLabelText('Choose an option')
562
-
563
- expect(input).toHaveAttribute('value', val)
564
- })
565
-
566
- it('should set aria-activedescendant based on the highlighted option', () => {
567
- render(
568
- <Select renderLabel="Choose an option" isShowingOptions>
569
- {getOptions(defaultOptions[1])}
570
- </Select>
571
- )
572
- const input = screen.getByLabelText('Choose an option')
573
-
574
- expect(input).toHaveAttribute('aria-activedescendant', defaultOptions[1])
575
- })
576
-
577
- it('should not set aria-activedescendant when not showing options', () => {
578
- render(
579
- <Select renderLabel="Choose an option">
580
- {getOptions(defaultOptions[1])}
581
- </Select>
582
- )
583
- const input = screen.getByLabelText('Choose an option')
584
-
585
- expect(input).not.toHaveAttribute('aria-activedescendant')
586
- })
587
-
588
- it('should allow assistive text', () => {
589
- render(
590
- <Select renderLabel="Choose an option" assistiveText="hello world">
591
- {getOptions()}
592
- </Select>
593
- )
594
- const input = screen.getByLabelText('Choose an option')
595
- const assistiveText = screen.getByText('hello world')
596
- const assistiveTextId = assistiveText.getAttribute('id')
597
-
598
- expect(input).toHaveAttribute('aria-describedby', assistiveTextId)
599
- })
600
-
601
- it('should allow custom props to pass through', () => {
602
- render(
603
- <Select renderLabel="Choose an option" data-custom-attr="true">
604
- {getOptions()}
605
- </Select>
606
- )
607
- const input = screen.getByLabelText('Choose an option')
608
-
609
- expect(input).toHaveAttribute('data-custom-attr', 'true')
610
- })
611
-
612
- it('should allow override of autoComplete prop', () => {
613
- const { rerender } = render(
614
- <Select renderLabel="Choose an option">{getOptions()}</Select>
615
- )
616
- const input = screen.getByLabelText('Choose an option')
617
-
618
- expect(input).toHaveAttribute('autocomplete', 'off')
619
-
620
- rerender(
621
- <Select renderLabel="Choose an option" autoComplete="false">
622
- {getOptions()}
623
- </Select>
624
- )
625
-
626
- expect(input).toHaveAttribute('autocomplete', 'false')
627
- })
628
-
629
- it('should provide a ref to the input element', async () => {
630
- const inputRef = vi.fn()
631
-
632
- render(
633
- <Select renderLabel="Choose an option" inputRef={inputRef}>
634
- {getOptions()}
635
- </Select>
636
- )
637
-
638
- const input = screen.getByLabelText('Choose an option')
639
-
640
- await waitFor(() => {
641
- expect(inputRef).toHaveBeenCalledWith(input)
642
- })
643
- })
644
-
645
- it("should not render option's before content in input field when isOptionContentAppliedToInput is set to false", async () => {
646
- const { container } = render(
647
- <Select
648
- renderLabel="Choose an option"
649
- isOptionContentAppliedToInput={false}
650
- inputValue={optionsWithBeforeContent[0].label}
651
- >
652
- {getOptionsWithBeforeContent('opt1')}
653
- </Select>
654
- )
655
-
656
- const beforeContent = container.querySelector(
657
- 'span[class$="-textInput__layout"]'
658
- )
659
-
660
- expect(beforeContent).not.toHaveTextContent('XY')
661
- })
662
-
663
- it('should render arrow in input field when isOptionContentAppliedToInput is set to false', async () => {
664
- const { container } = render(
665
- <Select
666
- renderLabel="Choose an option"
667
- isOptionContentAppliedToInput={false}
668
- inputValue={optionsWithBeforeContent[0].label}
669
- >
670
- {getOptionsWithBeforeContent('opt1')}
671
- </Select>
672
- )
673
-
674
- const spanElement = container.querySelector(
675
- 'span[class$="-textInput__afterElement"]'
676
- )
677
- const svgElement = spanElement!.querySelector(
678
- 'svg[name="IconArrowOpenDown"]'
679
- )
680
- expect(svgElement).toBeInTheDocument()
681
- })
682
-
683
- it("should render option's before content in input field when isOptionContentAppliedToInput is set to true", async () => {
684
- const { container } = render(
685
- <Select
686
- renderLabel="Choose an option"
687
- isOptionContentAppliedToInput={true}
688
- inputValue={optionsWithBeforeContent[0].label}
689
- >
690
- {getOptionsWithBeforeContent('opt1')}
691
- </Select>
692
- )
693
-
694
- const beforeContent = container.querySelector(
695
- 'span[class$="-textInput__layout"]'
696
- )
697
-
698
- expect(beforeContent).toHaveTextContent('XY')
699
- })
700
-
701
- it('should render arrow icon when isOptionContentAppliedToInput is set to true with before content', async () => {
702
- const { container } = render(
703
- <Select
704
- renderLabel="Choose an option"
705
- isOptionContentAppliedToInput={true}
706
- inputValue={optionsWithBeforeContent[0].label}
707
- >
708
- {getOptionsWithBeforeContent('opt1')}
709
- </Select>
710
- )
711
-
712
- const spanElement = container.querySelector(
713
- 'span[class$="-textInput__afterElement"]'
714
- )
715
- const svgElement = spanElement!.querySelector(
716
- 'svg[name="IconArrowOpenDown"]'
717
- )
718
- expect(svgElement).toBeInTheDocument()
719
- })
720
-
721
- it("should render option's after content in input field when isOptionContentAppliedToInput is set to true", async () => {
722
- const { container } = render(
723
- <Select
724
- renderLabel="Choose an option"
725
- isOptionContentAppliedToInput={true}
726
- inputValue={optionsWithAfterContent[0].label}
727
- >
728
- {getOptionsWithAfterContent('opt1')}
729
- </Select>
730
- )
731
-
732
- const beforeContent = container.querySelector(
733
- 'span[class$="-textInput__afterElement"]'
734
- )
735
- expect(beforeContent).toHaveTextContent('XY')
736
- })
737
-
738
- it("should render option's before content in input field when isOptionContentAppliedToInput is set to true but renderBeforeInput is also set", async () => {
739
- const { container } = render(
740
- <Select
741
- renderLabel="Choose an option"
742
- isOptionContentAppliedToInput={true}
743
- inputValue={optionsWithBeforeContent[0].label}
744
- renderBeforeInput="ZZ"
745
- >
746
- {getOptionsWithBeforeContent('opt1')}
747
- </Select>
748
- )
749
-
750
- const beforeContent = container.querySelector(
751
- 'span[class$="-textInput__layout"]'
752
- )
753
-
754
- expect(beforeContent).toHaveTextContent('XY')
755
- })
756
-
757
- it("should render option's after content in input field when isOptionContentAppliedToInput is set to true but renderAfterInput is also set", async () => {
758
- const { container } = render(
759
- <Select
760
- renderLabel="Choose an option"
761
- isOptionContentAppliedToInput={true}
762
- inputValue={optionsWithAfterContent[0].label}
763
- renderAfterInput="ZZ"
764
- >
765
- {getOptionsWithAfterContent('opt1')}
766
- </Select>
767
- )
768
-
769
- const afterContent = container.querySelector(
770
- 'span[class$="-textInput__afterElement"]'
771
- )
772
- expect(afterContent).toHaveTextContent('XY')
773
- })
774
-
775
- it("should not render option's before content in input field when isOptionContentAppliedToInput is set to true but inputValue is not set", async () => {
776
- const { container } = render(
777
- <Select
778
- renderLabel="Choose an option"
779
- isOptionContentAppliedToInput={true}
780
- >
781
- {getOptionsWithBeforeContent('opt1')}
782
- </Select>
783
- )
784
-
785
- const beforeContent = container.querySelector(
786
- 'span[class$="-textInput__layout"]'
787
- )
788
-
789
- expect(beforeContent).not.toHaveTextContent('XY')
790
- })
791
-
792
- it("should render option's before content in input field when isOptionContentAppliedToInput is set to true and both optionBeforeContent and optionAfterContent are provided", async () => {
793
- const { container } = render(
794
- <Select
795
- renderLabel="Choose an option"
796
- isOptionContentAppliedToInput={true}
797
- inputValue={optionsWithBeforeAndAfterContent[0].label}
798
- >
799
- {getOptionsWithBeforeAndAfterContent('opt1')}
800
- </Select>
801
- )
802
-
803
- const beforeContent = container.querySelector(
804
- 'span[class$="-textInput__layout"]'
805
- )
806
-
807
- expect(beforeContent).toHaveTextContent('XY')
808
- })
809
-
810
- it("should render option's after content in input field when isOptionContentAppliedToInput is set to true and both optionBeforeContent and optionAfterContent are provided", async () => {
811
- const { container } = render(
812
- <Select
813
- renderLabel="Choose an option"
814
- isOptionContentAppliedToInput={true}
815
- inputValue={optionsWithBeforeAndAfterContent[0].label}
816
- >
817
- {getOptionsWithBeforeAndAfterContent('opt1')}
818
- </Select>
819
- )
820
-
821
- const afterContent = container.querySelector(
822
- 'span[class$="-textInput__afterElement"]'
823
- )
824
- expect(afterContent).toHaveTextContent('ZZ')
825
- })
826
-
827
- it('should render arrow in input field when isOptionContentAppliedToInput is set to true but inputValue is not set', async () => {
828
- const { container } = render(
829
- <Select
830
- renderLabel="Choose an option"
831
- isOptionContentAppliedToInput={true}
832
- >
833
- {getOptionsWithBeforeContent('opt1')}
834
- </Select>
835
- )
836
-
837
- const spanElement = container.querySelector(
838
- 'span[class$="-textInput__afterElement"]'
839
- )
840
- const svgElement = spanElement!.querySelector(
841
- 'svg[name="IconArrowOpenDown"]'
842
- )
843
-
844
- expect(svgElement).toBeInTheDocument()
845
- })
846
-
847
- it("should not render option's after content in input field when isOptionContentAppliedToInput is set to true but inputValue is not set", async () => {
848
- const { container } = render(
849
- <Select
850
- renderLabel="Choose an option"
851
- isOptionContentAppliedToInput={true}
852
- >
853
- {getOptionsWithBeforeContent('opt1')}
854
- </Select>
855
- )
856
-
857
- const afterContent = container.querySelector(
858
- 'span[class$="-textInput__afterElement"]'
859
- )
860
-
861
- expect(afterContent).not.toHaveTextContent('XY')
862
- })
863
-
864
- it("should render option's before content input field when isOptionContentAppliedToInput is set to true with group options", async () => {
865
- const { container } = render(
866
- <Select
867
- renderLabel="Choose an option"
868
- isOptionContentAppliedToInput={true}
869
- inputValue={groupOptionsWithBeforeContent.Options1[0].label}
870
- >
871
- {getGroupOptionsWithBeforeContent('opt1')}
872
- </Select>
873
- )
874
-
875
- const beforeContent = container.querySelector(
876
- 'span[class$="-textInput__layout"]'
877
- )
878
-
879
- expect(beforeContent).toHaveTextContent('XY')
880
- })
881
-
882
- it('should render arrow icon when isOptionContentAppliedToInput is set to true with before content and group options', async () => {
883
- const { container } = render(
884
- <Select
885
- renderLabel="Choose an option"
886
- isOptionContentAppliedToInput={true}
887
- inputValue={groupOptionsWithBeforeContent.Options1[0].label}
888
- >
889
- {getGroupOptionsWithBeforeContent('opt1')}
890
- </Select>
891
- )
892
-
893
- const spanElement = container.querySelector(
894
- 'span[class$="-textInput__afterElement"]'
895
- )
896
- const svgElement = spanElement!.querySelector(
897
- 'svg[name="IconArrowOpenDown"]'
898
- )
899
- expect(svgElement).toBeInTheDocument()
900
- })
901
-
902
- it("should render option's after content input field when isOptionContentAppliedToInput is set to true with group options", async () => {
903
- const { container } = render(
904
- <Select
905
- renderLabel="Choose an option"
906
- isOptionContentAppliedToInput={true}
907
- inputValue={groupOptionsWithAfterContent.Options2[0].label}
908
- >
909
- {getGroupOptionsWithAfterContent('opt4')}
910
- </Select>
911
- )
912
-
913
- const afterContent = container.querySelector(
914
- 'span[class$="-textInput__afterElement"]'
915
- )
916
-
917
- expect(afterContent).toHaveTextContent('AB')
918
- })
919
- })
920
-
921
- describe('list', () => {
922
- it('should set aria-selected on options with isSelected={true}', () => {
923
- render(
924
- <Select renderLabel="Choose an option" isShowingOptions>
925
- {getOptions(undefined, defaultOptions[1])}
926
- </Select>
927
- )
928
- const options = screen.getAllByRole('option')
929
-
930
- expect(options[1].getAttribute('aria-selected')).toEqual('true')
931
- })
932
-
933
- it('should set aria-disabled on options when isDisabled={true}', () => {
934
- render(
935
- <Select renderLabel="Choose an option" isShowingOptions>
936
- {getOptions(undefined, undefined, defaultOptions[2])}
937
- </Select>
938
- )
939
- const options = screen.getAllByRole('option')
940
-
941
- expect(options[0]).not.toHaveAttribute('aria-disabled')
942
- expect(options[2]).toHaveAttribute('aria-disabled', 'true')
943
- })
944
-
945
- it('should set list element role to "listbox"', async () => {
946
- render(
947
- <Select renderLabel="Choose an option" isShowingOptions>
948
- {getOptions()}
949
- </Select>
950
- )
951
- const listbox = screen.getByRole('listbox')
952
-
953
- expect(listbox).toBeInTheDocument()
954
- expect(listbox.tagName).toBe('UL')
955
- })
956
-
957
- it('should provide a ref to the list element', async () => {
958
- const listRef = vi.fn()
959
-
960
- render(
961
- <Select
962
- renderLabel="Choose an option"
963
- isShowingOptions
964
- listRef={listRef}
965
- >
966
- {getOptions()}
967
- </Select>
968
- )
969
-
970
- const listbox = screen.getByRole('listbox')
971
-
972
- await waitFor(() => {
973
- expect(listRef).toHaveBeenCalledWith(listbox)
974
- })
975
- })
976
- })
977
-
978
- describe('with callbacks', () => {
979
- describe('should fire onRequestShowOptions', () => {
980
- it('when root is clicked', async () => {
981
- const onRequestShowOptions = vi.fn()
982
-
983
- const { container, rerender } = render(
984
- <Select
985
- renderLabel="Choose an option"
986
- onRequestShowOptions={onRequestShowOptions}
987
- >
988
- {getOptions()}
989
- </Select>
990
- )
991
-
992
- const icon = container.querySelector('svg[name="IconArrowOpenDown"]')
993
- const label = screen.getByText('Choose an option')
994
-
995
- expect(icon).toBeInTheDocument()
996
- expect(label).toBeInTheDocument()
997
-
998
- await userEvent.click(label)
999
- await waitFor(() => {
1000
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1)
1001
- })
1002
-
1003
- await userEvent.click(icon!)
1004
- await waitFor(() => {
1005
- expect(onRequestShowOptions).toHaveBeenCalledTimes(2)
1006
- })
1007
-
1008
- rerender(
1009
- <Select
1010
- renderLabel="Choose an option"
1011
- onRequestShowOptions={onRequestShowOptions}
1012
- isShowingOptions={true}
1013
- >
1014
- {getOptions()}
1015
- </Select>
1016
- )
1017
-
1018
- await userEvent.click(label)
1019
- await waitFor(() => {
1020
- expect(onRequestShowOptions).toHaveBeenCalledTimes(2)
1021
- })
1022
- })
1023
-
1024
- it('when input is clicked', async () => {
1025
- const onRequestShowOptions = vi.fn()
1026
-
1027
- const { rerender } = render(
1028
- <Select
1029
- renderLabel="Choose an option"
1030
- onRequestShowOptions={onRequestShowOptions}
1031
- >
1032
- {getOptions()}
1033
- </Select>
1034
- )
1035
- const input = screen.getByLabelText('Choose an option')
1036
-
1037
- await userEvent.click(input)
1038
- await waitFor(() => {
1039
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1)
1040
- })
1041
-
1042
- rerender(
1043
- <Select
1044
- renderLabel="Choose an option"
1045
- onRequestShowOptions={onRequestShowOptions}
1046
- isShowingOptions={true}
1047
- >
1048
- {getOptions()}
1049
- </Select>
1050
- )
1051
-
1052
- await userEvent.click(input)
1053
- await waitFor(() => {
1054
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1)
1055
- })
1056
- })
1057
-
1058
- it('when up/down arrows are pressed', async () => {
1059
- const onRequestShowOptions = vi.fn()
1060
-
1061
- render(
1062
- <Select
1063
- renderLabel="Choose an option"
1064
- onRequestShowOptions={onRequestShowOptions}
1065
- >
1066
- {getOptions()}
1067
- </Select>
1068
- )
1069
-
1070
- const input = screen.getByLabelText('Choose an option')
1071
-
1072
- await userEvent.type(input, '{arrowdown}')
1073
- await waitFor(() => {
1074
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1)
1075
- })
1076
-
1077
- await userEvent.type(input, '{arrowup}')
1078
- await waitFor(() => {
1079
- expect(onRequestShowOptions).toHaveBeenCalledTimes(2)
1080
- })
1081
- })
1082
-
1083
- it('when space is pressed', async () => {
1084
- const onRequestShowOptions = vi.fn()
1085
-
1086
- const { rerender } = render(
1087
- <Select
1088
- renderLabel="Choose an option"
1089
- onRequestShowOptions={onRequestShowOptions}
1090
- >
1091
- {getOptions()}
1092
- </Select>
1093
- )
1094
-
1095
- const input = screen.getByLabelText('Choose an option')
1096
-
1097
- await userEvent.type(input, '{space}')
1098
- await waitFor(() => {
1099
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1)
1100
- })
1101
-
1102
- rerender(
1103
- <Select
1104
- renderLabel="Choose an option"
1105
- onRequestShowOptions={onRequestShowOptions}
1106
- isShowingOptions={true}
1107
- >
1108
- {getOptions()}
1109
- </Select>
1110
- )
1111
-
1112
- await userEvent.type(input, '{space}')
1113
- await waitFor(() => {
1114
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1)
1115
- })
1116
- })
1117
- })
1118
-
1119
- describe('should fire onRequestHideOptions', () => {
1120
- it('when root is clicked and isShowingOptions is true', async () => {
1121
- const onRequestHideOptions = vi.fn()
1122
-
1123
- const { container } = render(
1124
- <Select
1125
- renderLabel="Choose an option"
1126
- isShowingOptions
1127
- onRequestHideOptions={onRequestHideOptions}
1128
- >
1129
- {getOptions()}
1130
- </Select>
1131
- )
1132
-
1133
- const icon = container.querySelector('svg[name="IconArrowOpenUp"]')
1134
- const label = screen.getByText('Choose an option')
1135
-
1136
- expect(icon).toBeInTheDocument()
1137
- expect(label).toBeInTheDocument()
1138
-
1139
- await userEvent.click(label)
1140
- await waitFor(() => {
1141
- expect(onRequestHideOptions).toHaveBeenCalledTimes(1)
1142
- })
1143
-
1144
- await userEvent.click(icon!)
1145
- await waitFor(() => {
1146
- expect(onRequestHideOptions).toHaveBeenCalledTimes(2)
1147
- })
1148
- })
1149
-
1150
- it('when root is clicked and isShowingOptions is false should NOT fire onRequestHideOptions', async () => {
1151
- const onRequestHideOptions = vi.fn()
1152
-
1153
- render(
1154
- <Select
1155
- renderLabel="Choose an option"
1156
- isShowingOptions={false}
1157
- onRequestHideOptions={onRequestHideOptions}
1158
- >
1159
- {getOptions()}
1160
- </Select>
1161
- )
1162
-
1163
- const label = screen.getByText('Choose an option')
1164
-
1165
- expect(label).toBeInTheDocument()
1166
-
1167
- await userEvent.click(label)
1168
- await waitFor(() => {
1169
- expect(onRequestHideOptions).not.toHaveBeenCalled()
1170
- })
1171
- })
1172
-
1173
- it('when input is clicked', async () => {
1174
- const onRequestHideOptions = vi.fn()
1175
-
1176
- const { rerender } = render(
1177
- <Select
1178
- renderLabel="Choose an option"
1179
- isShowingOptions
1180
- onRequestHideOptions={onRequestHideOptions}
1181
- >
1182
- {getOptions()}
1183
- </Select>
1184
- )
1185
-
1186
- const input = screen.getByLabelText('Choose an option')
1187
-
1188
- await userEvent.click(input)
1189
- await waitFor(() => {
1190
- expect(onRequestHideOptions).toHaveBeenCalledTimes(1)
1191
- })
1192
-
1193
- rerender(
1194
- <Select
1195
- renderLabel="Choose an option"
1196
- isShowingOptions={false}
1197
- onRequestHideOptions={onRequestHideOptions}
1198
- >
1199
- {getOptions()}
1200
- </Select>
1201
- )
1202
- await userEvent.click(input)
1203
- await waitFor(() => {
1204
- expect(onRequestHideOptions).toHaveBeenCalledTimes(1)
1205
- })
1206
- })
1207
-
1208
- it('when escape is pressed', async () => {
1209
- const onRequestHideOptions = vi.fn()
1210
-
1211
- render(
1212
- <Select
1213
- renderLabel="Choose an option"
1214
- isShowingOptions
1215
- onRequestHideOptions={onRequestHideOptions}
1216
- >
1217
- {getOptions()}
1218
- </Select>
1219
- )
1220
-
1221
- const input = screen.getByLabelText('Choose an option')
1222
-
1223
- await userEvent.type(input, '{esc}')
1224
- await waitFor(() => {
1225
- expect(onRequestHideOptions).toHaveBeenCalledTimes(1)
1226
- })
1227
- })
1228
- })
1229
-
1230
- describe('should fire onRequestHighlightOption', () => {
1231
- it('when options are hovered', async () => {
1232
- const onRequestHighlightOption = vi.fn()
1233
-
1234
- render(
1235
- <Select
1236
- renderLabel="Choose an option"
1237
- isShowingOptions
1238
- onRequestHighlightOption={onRequestHighlightOption}
1239
- >
1240
- {getOptions()}
1241
- </Select>
1242
- )
1243
- const options = screen.getAllByRole('option')
1244
-
1245
- await userEvent.hover(options[0])
1246
- await waitFor(() => {
1247
- expect(onRequestHighlightOption).toHaveBeenCalledWith(
1248
- expect.anything(),
1249
- expect.objectContaining({
1250
- id: defaultOptions[0]
1251
- })
1252
- )
1253
- })
1254
-
1255
- await userEvent.hover(options[1])
1256
- await waitFor(() => {
1257
- expect(onRequestHighlightOption).toHaveBeenCalledWith(
1258
- expect.anything(),
1259
- expect.objectContaining({
1260
- id: defaultOptions[1]
1261
- })
1262
- )
1263
- })
1264
- })
1265
- })
1266
-
1267
- describe('input callbacks', () => {
1268
- it('should fire onInputChange when input is typed in', async () => {
1269
- const onInputChange = vi.fn()
1270
-
1271
- render(
1272
- <Select renderLabel="Choose an option" onInputChange={onInputChange}>
1273
- {getOptions()}
1274
- </Select>
1275
- )
1276
-
1277
- const input = screen.getByLabelText('Choose an option')
1278
-
1279
- await userEvent.type(input, 'h')
1280
- await waitFor(() => {
1281
- expect(onInputChange).toHaveBeenCalled()
1282
- })
1283
- })
1284
-
1285
- it('should fire onFocus when input gains focus', async () => {
1286
- const onFocus = vi.fn()
1287
-
1288
- render(
1289
- <Select renderLabel="Choose an option" onFocus={onFocus}>
1290
- {getOptions()}
1291
- </Select>
1292
- )
1293
-
1294
- const input = screen.getByLabelText('Choose an option')
1295
-
1296
- input.focus()
1297
- await waitFor(() => {
1298
- expect(onFocus).toHaveBeenCalled()
1299
- })
1300
- })
1301
- })
1302
- })
1303
-
1304
- describe('with generated examples', () => {
1305
- const generatedComponents = generateA11yTests(Select, SelectExamples)
1306
-
1307
- it.each(generatedComponents)(
1308
- 'should be accessible with example: $description',
1309
- async ({ content }) => {
1310
- const { container } = render(content)
1311
- const axeCheck = await runAxeCheck(container)
1312
- expect(axeCheck).toBe(true)
1313
- }
1314
- )
1315
- })
1316
- })