@instructure/ui-select 9.0.2-snapshot-6 → 9.0.2-snapshot-9
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.
- package/CHANGELOG.md +1 -1
- package/es/Select/__new-tests__/Select.test.js +517 -8
- package/lib/Select/__new-tests__/Select.test.js +516 -9
- package/package.json +26 -23
- package/src/Select/__new-tests__/Select.test.tsx +709 -14
- package/tsconfig.build.json +2 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/Select/__new-tests__/Select.test.d.ts.map +1 -1
|
@@ -22,12 +22,37 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
import React from 'react'
|
|
25
|
-
import { render } from '@testing-library/react'
|
|
25
|
+
import { render, screen, waitFor } from '@testing-library/react'
|
|
26
|
+
import userEvent from '@testing-library/user-event'
|
|
26
27
|
import '@testing-library/jest-dom'
|
|
28
|
+
|
|
29
|
+
// eslint-disable-next-line no-restricted-imports
|
|
30
|
+
import { generateA11yTests } from '@instructure/ui-scripts/lib/test/generateA11yTests'
|
|
31
|
+
import { runAxeCheck } from '@instructure/ui-axe-check'
|
|
27
32
|
import Select from '../index'
|
|
33
|
+
import SelectExamples from '../__examples__/Select.examples'
|
|
28
34
|
import * as utils from '@instructure/ui-utils'
|
|
29
35
|
|
|
30
36
|
type ExampleOption = 'foo' | 'bar' | 'baz'
|
|
37
|
+
const defaultOptions: ExampleOption[] = ['foo', 'bar', 'baz']
|
|
38
|
+
const getOptions = (
|
|
39
|
+
highlighted?: ExampleOption,
|
|
40
|
+
selected?: ExampleOption,
|
|
41
|
+
disabled?: ExampleOption
|
|
42
|
+
) =>
|
|
43
|
+
defaultOptions.map((opt) => (
|
|
44
|
+
<Select.Option
|
|
45
|
+
id={opt}
|
|
46
|
+
key={opt}
|
|
47
|
+
value={opt}
|
|
48
|
+
isHighlighted={opt === highlighted}
|
|
49
|
+
isSelected={opt === selected}
|
|
50
|
+
isDisabled={opt === disabled}
|
|
51
|
+
>
|
|
52
|
+
{opt}
|
|
53
|
+
</Select.Option>
|
|
54
|
+
))
|
|
55
|
+
|
|
31
56
|
jest.mock('@instructure/ui-utils', () => {
|
|
32
57
|
const originalModule = jest.requireActual('@instructure/ui-utils')
|
|
33
58
|
|
|
@@ -41,19 +66,16 @@ jest.mock('@instructure/ui-utils', () => {
|
|
|
41
66
|
const mockUtils = utils as jest.Mocked<typeof utils>
|
|
42
67
|
|
|
43
68
|
describe('<Select />', () => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
{opt}
|
|
55
|
-
</Select.Option>
|
|
56
|
-
))
|
|
69
|
+
let consoleErrorMock: jest.SpyInstance
|
|
70
|
+
|
|
71
|
+
beforeEach(() => {
|
|
72
|
+
// Mocking console to prevent test output pollution and expect for messages
|
|
73
|
+
consoleErrorMock = jest.spyOn(console, 'error').mockImplementation()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
afterEach(() => {
|
|
77
|
+
consoleErrorMock.mockRestore()
|
|
78
|
+
})
|
|
57
79
|
|
|
58
80
|
it('should have role button in Safari without onInputChange', async () => {
|
|
59
81
|
const { container } = render(
|
|
@@ -83,4 +105,677 @@ describe('<Select />', () => {
|
|
|
83
105
|
const input = container.querySelector('input')
|
|
84
106
|
expect(input).toHaveAttribute('role', 'combobox')
|
|
85
107
|
})
|
|
108
|
+
|
|
109
|
+
it('should render an input and a list', async () => {
|
|
110
|
+
const { container } = render(
|
|
111
|
+
<Select
|
|
112
|
+
renderLabel="Choose an option"
|
|
113
|
+
isShowingOptions
|
|
114
|
+
data-testid="subidubi"
|
|
115
|
+
>
|
|
116
|
+
{getOptions()}
|
|
117
|
+
</Select>
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
const select = container.querySelector('span[class$="-select"]')
|
|
121
|
+
const label = screen.getByLabelText('Choose an option')
|
|
122
|
+
const input = container.querySelector('input[id^="Select_"]')
|
|
123
|
+
const list = screen.getByRole('listbox')
|
|
124
|
+
|
|
125
|
+
const options = screen.getAllByRole('option')
|
|
126
|
+
const optionsCount = defaultOptions.length
|
|
127
|
+
|
|
128
|
+
expect(select).toBeInTheDocument()
|
|
129
|
+
expect(label).toBeInTheDocument()
|
|
130
|
+
expect(input).toBeInTheDocument()
|
|
131
|
+
expect(list).toBeInTheDocument()
|
|
132
|
+
|
|
133
|
+
expect(options.length).toBe(optionsCount)
|
|
134
|
+
expect(options[0]).toHaveTextContent(defaultOptions[0])
|
|
135
|
+
expect(options[1]).toHaveTextContent(defaultOptions[1])
|
|
136
|
+
expect(options[2]).toHaveTextContent(defaultOptions[2])
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('should render groups', async () => {
|
|
140
|
+
render(
|
|
141
|
+
<Select renderLabel="Choose an option" isShowingOptions>
|
|
142
|
+
<Select.Option id="0">ungrouped option one</Select.Option>
|
|
143
|
+
<Select.Group renderLabel="Group one">
|
|
144
|
+
<Select.Option id="1">grouped option one</Select.Option>
|
|
145
|
+
</Select.Group>
|
|
146
|
+
<Select.Group renderLabel="Group two">
|
|
147
|
+
<Select.Option id="2">grouped option two</Select.Option>
|
|
148
|
+
</Select.Group>
|
|
149
|
+
<Select.Option id="3">ungrouped option two</Select.Option>
|
|
150
|
+
</Select>
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
const groups = screen.getAllByRole('group')
|
|
154
|
+
const groupLabel = screen.getByText('Group one')
|
|
155
|
+
|
|
156
|
+
expect(groupLabel).toHaveAttribute('role', 'presentation')
|
|
157
|
+
expect(groups[0].getAttribute('aria-labelledby')).toEqual(
|
|
158
|
+
groupLabel.getAttribute('id')
|
|
159
|
+
)
|
|
160
|
+
expect(groups.length).toBe(2)
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
it('should ignore invalid children', async () => {
|
|
164
|
+
render(
|
|
165
|
+
<Select renderLabel="Choose an option" isShowingOptions>
|
|
166
|
+
<Select.Option id="0">valid</Select.Option>
|
|
167
|
+
<div>invalid</div>
|
|
168
|
+
</Select>
|
|
169
|
+
)
|
|
170
|
+
const invalidChild = screen.queryByText('invalid')
|
|
171
|
+
|
|
172
|
+
expect(invalidChild).not.toBeInTheDocument()
|
|
173
|
+
|
|
174
|
+
const expectedErrorMessage = 'Expected one of Group, Option'
|
|
175
|
+
|
|
176
|
+
expect(consoleErrorMock).toHaveBeenCalledWith(
|
|
177
|
+
expect.any(String),
|
|
178
|
+
expect.any(String),
|
|
179
|
+
expect.stringContaining(expectedErrorMessage),
|
|
180
|
+
expect.any(String)
|
|
181
|
+
)
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
it('should provide a focus method', async () => {
|
|
185
|
+
const ref = React.createRef<Select>()
|
|
186
|
+
render(
|
|
187
|
+
<Select renderLabel="Choose an option" ref={ref}>
|
|
188
|
+
{getOptions()}
|
|
189
|
+
</Select>
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
const input = screen.getByLabelText('Choose an option')
|
|
193
|
+
|
|
194
|
+
ref.current?.focus()
|
|
195
|
+
|
|
196
|
+
await waitFor(() => {
|
|
197
|
+
expect(document.activeElement).toBe(input)
|
|
198
|
+
})
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
it('should provide a focused getter', async () => {
|
|
202
|
+
const ref = React.createRef<Select>()
|
|
203
|
+
|
|
204
|
+
render(
|
|
205
|
+
<Select renderLabel="Choose an option" ref={ref}>
|
|
206
|
+
{getOptions()}
|
|
207
|
+
</Select>
|
|
208
|
+
)
|
|
209
|
+
expect(ref.current?.focused).toBe(false)
|
|
210
|
+
|
|
211
|
+
ref.current?.focus()
|
|
212
|
+
|
|
213
|
+
await waitFor(() => {
|
|
214
|
+
expect(ref.current?.focused).toBe(true)
|
|
215
|
+
})
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
describe('input', () => {
|
|
219
|
+
it('should render with a generated id by default', () => {
|
|
220
|
+
const { container } = render(<Select renderLabel="Choose an option" />)
|
|
221
|
+
const input = container.querySelector('input[id^="Select_"]')
|
|
222
|
+
|
|
223
|
+
expect(input).toBeInTheDocument()
|
|
224
|
+
expect(input).toHaveAttribute('id', expect.stringContaining('Select_'))
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
it('should render with a custom id if given', () => {
|
|
228
|
+
const { container } = render(
|
|
229
|
+
<Select renderLabel="Choose an option" id="customSelect" />
|
|
230
|
+
)
|
|
231
|
+
const input = container.querySelector('input[id^="customSelect"]')
|
|
232
|
+
|
|
233
|
+
expect(input).toBeInTheDocument()
|
|
234
|
+
expect(input!.getAttribute('id')).toEqual('customSelect')
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
it('should render readonly when interaction="enabled" with no onInputChange', () => {
|
|
238
|
+
render(<Select renderLabel="Choose an option" />)
|
|
239
|
+
const input = screen.getByLabelText('Choose an option')
|
|
240
|
+
|
|
241
|
+
expect(input).toHaveAttribute('readonly')
|
|
242
|
+
expect(input).not.toHaveAttribute('disabled')
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
it('should not render readonly when interaction="enabled" with onInputChange', () => {
|
|
246
|
+
render(<Select renderLabel="Choose an option" onInputChange={() => {}} />)
|
|
247
|
+
const input = screen.getByLabelText('Choose an option')
|
|
248
|
+
|
|
249
|
+
expect(input).not.toHaveAttribute('readonly')
|
|
250
|
+
expect(input).not.toHaveAttribute('disabled')
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
it('should render readonly when interaction="readonly"', () => {
|
|
254
|
+
render(
|
|
255
|
+
<Select
|
|
256
|
+
renderLabel="Choose an option"
|
|
257
|
+
interaction="readonly"
|
|
258
|
+
onInputChange={() => {}}
|
|
259
|
+
/>
|
|
260
|
+
)
|
|
261
|
+
const input = screen.getByLabelText('Choose an option')
|
|
262
|
+
|
|
263
|
+
expect(input).toHaveAttribute('readonly')
|
|
264
|
+
expect(input).not.toHaveAttribute('disabled')
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
it('should render disabled when interaction="disabled"', () => {
|
|
268
|
+
render(
|
|
269
|
+
<Select
|
|
270
|
+
renderLabel="Choose an option"
|
|
271
|
+
interaction="disabled"
|
|
272
|
+
onInputChange={() => {}}
|
|
273
|
+
/>
|
|
274
|
+
)
|
|
275
|
+
const input = screen.getByLabelText('Choose an option')
|
|
276
|
+
|
|
277
|
+
expect(input).toHaveAttribute('disabled')
|
|
278
|
+
expect(input).not.toHaveAttribute('readonly')
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
it('should not render readonly when disabled', () => {
|
|
282
|
+
render(<Select renderLabel="Choose an option" interaction="disabled" />)
|
|
283
|
+
const input = screen.getByLabelText('Choose an option')
|
|
284
|
+
|
|
285
|
+
expect(input).toHaveAttribute('disabled')
|
|
286
|
+
expect(input).not.toHaveAttribute('readonly')
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
it('should render required when isRequired={true}', () => {
|
|
290
|
+
render(<Select renderLabel="Choose an option" isRequired />)
|
|
291
|
+
const input = screen.getByLabelText('Choose an option')
|
|
292
|
+
|
|
293
|
+
expect(input).toHaveAttribute('required')
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
it('should render with inputValue', () => {
|
|
297
|
+
const val = 'hello world'
|
|
298
|
+
|
|
299
|
+
render(<Select renderLabel="Choose an option" inputValue={val} />)
|
|
300
|
+
const input = screen.getByLabelText('Choose an option')
|
|
301
|
+
|
|
302
|
+
expect(input).toHaveAttribute('value', val)
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
it('should set aria-activedescendant based on the highlighted option', () => {
|
|
306
|
+
render(
|
|
307
|
+
<Select renderLabel="Choose an option" isShowingOptions>
|
|
308
|
+
{getOptions(defaultOptions[1])}
|
|
309
|
+
</Select>
|
|
310
|
+
)
|
|
311
|
+
const input = screen.getByLabelText('Choose an option')
|
|
312
|
+
|
|
313
|
+
expect(input).toHaveAttribute('aria-activedescendant', defaultOptions[1])
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
it('should not set aria-activedescendant when not showing options', () => {
|
|
317
|
+
render(
|
|
318
|
+
<Select renderLabel="Choose an option">
|
|
319
|
+
{getOptions(defaultOptions[1])}
|
|
320
|
+
</Select>
|
|
321
|
+
)
|
|
322
|
+
const input = screen.getByLabelText('Choose an option')
|
|
323
|
+
|
|
324
|
+
expect(input).not.toHaveAttribute('aria-activedescendant')
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
it('should allow assistive text', () => {
|
|
328
|
+
render(
|
|
329
|
+
<Select renderLabel="Choose an option" assistiveText="hello world">
|
|
330
|
+
{getOptions()}
|
|
331
|
+
</Select>
|
|
332
|
+
)
|
|
333
|
+
const input = screen.getByLabelText('Choose an option')
|
|
334
|
+
const assistiveText = screen.getByText('hello world')
|
|
335
|
+
const assistiveTextId = assistiveText.getAttribute('id')
|
|
336
|
+
|
|
337
|
+
expect(input).toHaveAttribute('aria-describedby', assistiveTextId)
|
|
338
|
+
})
|
|
339
|
+
|
|
340
|
+
it('should allow custom props to pass through', () => {
|
|
341
|
+
render(
|
|
342
|
+
<Select renderLabel="Choose an option" data-custom-attr="true">
|
|
343
|
+
{getOptions()}
|
|
344
|
+
</Select>
|
|
345
|
+
)
|
|
346
|
+
const input = screen.getByLabelText('Choose an option')
|
|
347
|
+
|
|
348
|
+
expect(input).toHaveAttribute('data-custom-attr', 'true')
|
|
349
|
+
})
|
|
350
|
+
|
|
351
|
+
it('should allow override of autoComplete prop', () => {
|
|
352
|
+
const { rerender } = render(
|
|
353
|
+
<Select renderLabel="Choose an option">{getOptions()}</Select>
|
|
354
|
+
)
|
|
355
|
+
const input = screen.getByLabelText('Choose an option')
|
|
356
|
+
|
|
357
|
+
expect(input).toHaveAttribute('autocomplete', 'off')
|
|
358
|
+
|
|
359
|
+
rerender(
|
|
360
|
+
<Select renderLabel="Choose an option" autoComplete="false">
|
|
361
|
+
{getOptions()}
|
|
362
|
+
</Select>
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
expect(input).toHaveAttribute('autocomplete', 'false')
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
it('should provide a ref to the input element', async () => {
|
|
369
|
+
const inputRef = jest.fn()
|
|
370
|
+
|
|
371
|
+
render(
|
|
372
|
+
<Select renderLabel="Choose an option" inputRef={inputRef}>
|
|
373
|
+
{getOptions()}
|
|
374
|
+
</Select>
|
|
375
|
+
)
|
|
376
|
+
|
|
377
|
+
const input = screen.getByLabelText('Choose an option')
|
|
378
|
+
|
|
379
|
+
await waitFor(() => {
|
|
380
|
+
expect(inputRef).toHaveBeenCalledWith(input)
|
|
381
|
+
})
|
|
382
|
+
})
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
describe('list', () => {
|
|
386
|
+
it('should set aria-selected on options with isSelected={true}', () => {
|
|
387
|
+
render(
|
|
388
|
+
<Select renderLabel="Choose an option" isShowingOptions>
|
|
389
|
+
{getOptions(undefined, defaultOptions[1])}
|
|
390
|
+
</Select>
|
|
391
|
+
)
|
|
392
|
+
const options = screen.getAllByRole('option')
|
|
393
|
+
|
|
394
|
+
expect(options[1].getAttribute('aria-selected')).toEqual('true')
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
it('should set aria-disabled on options when isDisabled={true}', () => {
|
|
398
|
+
render(
|
|
399
|
+
<Select renderLabel="Choose an option" isShowingOptions>
|
|
400
|
+
{getOptions(undefined, undefined, defaultOptions[2])}
|
|
401
|
+
</Select>
|
|
402
|
+
)
|
|
403
|
+
const options = screen.getAllByRole('option')
|
|
404
|
+
|
|
405
|
+
expect(options[0]).not.toHaveAttribute('aria-disabled')
|
|
406
|
+
expect(options[2]).toHaveAttribute('aria-disabled', 'true')
|
|
407
|
+
})
|
|
408
|
+
|
|
409
|
+
it('should set list element role to "listbox"', async () => {
|
|
410
|
+
render(
|
|
411
|
+
<Select renderLabel="Choose an option" isShowingOptions>
|
|
412
|
+
{getOptions()}
|
|
413
|
+
</Select>
|
|
414
|
+
)
|
|
415
|
+
const listbox = screen.getByRole('listbox')
|
|
416
|
+
|
|
417
|
+
expect(listbox).toBeInTheDocument()
|
|
418
|
+
expect(listbox.tagName).toBe('UL')
|
|
419
|
+
})
|
|
420
|
+
|
|
421
|
+
it('should provide a ref to the list element', async () => {
|
|
422
|
+
const listRef = jest.fn()
|
|
423
|
+
|
|
424
|
+
render(
|
|
425
|
+
<Select
|
|
426
|
+
renderLabel="Choose an option"
|
|
427
|
+
isShowingOptions
|
|
428
|
+
listRef={listRef}
|
|
429
|
+
>
|
|
430
|
+
{getOptions()}
|
|
431
|
+
</Select>
|
|
432
|
+
)
|
|
433
|
+
|
|
434
|
+
const listbox = screen.getByRole('listbox')
|
|
435
|
+
|
|
436
|
+
await waitFor(() => {
|
|
437
|
+
expect(listRef).toHaveBeenCalledWith(listbox)
|
|
438
|
+
})
|
|
439
|
+
})
|
|
440
|
+
})
|
|
441
|
+
|
|
442
|
+
describe('with callbacks', () => {
|
|
443
|
+
describe('should fire onRequestShowOptions', () => {
|
|
444
|
+
it('when root is clicked', async () => {
|
|
445
|
+
const onRequestShowOptions = jest.fn()
|
|
446
|
+
|
|
447
|
+
const { container, rerender } = render(
|
|
448
|
+
<Select
|
|
449
|
+
renderLabel="Choose an option"
|
|
450
|
+
onRequestShowOptions={onRequestShowOptions}
|
|
451
|
+
>
|
|
452
|
+
{getOptions()}
|
|
453
|
+
</Select>
|
|
454
|
+
)
|
|
455
|
+
|
|
456
|
+
const icon = container.querySelector('svg[name="IconArrowOpenDown"]')
|
|
457
|
+
const label = screen.getByText('Choose an option')
|
|
458
|
+
|
|
459
|
+
expect(icon).toBeInTheDocument()
|
|
460
|
+
expect(label).toBeInTheDocument()
|
|
461
|
+
|
|
462
|
+
await userEvent.click(label)
|
|
463
|
+
await waitFor(() => {
|
|
464
|
+
expect(onRequestShowOptions).toHaveBeenCalledTimes(1)
|
|
465
|
+
})
|
|
466
|
+
|
|
467
|
+
await userEvent.click(icon!)
|
|
468
|
+
await waitFor(() => {
|
|
469
|
+
expect(onRequestShowOptions).toHaveBeenCalledTimes(2)
|
|
470
|
+
})
|
|
471
|
+
|
|
472
|
+
rerender(
|
|
473
|
+
<Select
|
|
474
|
+
renderLabel="Choose an option"
|
|
475
|
+
onRequestShowOptions={onRequestShowOptions}
|
|
476
|
+
isShowingOptions={true}
|
|
477
|
+
>
|
|
478
|
+
{getOptions()}
|
|
479
|
+
</Select>
|
|
480
|
+
)
|
|
481
|
+
|
|
482
|
+
await userEvent.click(label)
|
|
483
|
+
await waitFor(() => {
|
|
484
|
+
expect(onRequestShowOptions).toHaveBeenCalledTimes(2)
|
|
485
|
+
})
|
|
486
|
+
})
|
|
487
|
+
|
|
488
|
+
it('when input is clicked', async () => {
|
|
489
|
+
const onRequestShowOptions = jest.fn()
|
|
490
|
+
|
|
491
|
+
const { rerender } = render(
|
|
492
|
+
<Select
|
|
493
|
+
renderLabel="Choose an option"
|
|
494
|
+
onRequestShowOptions={onRequestShowOptions}
|
|
495
|
+
>
|
|
496
|
+
{getOptions()}
|
|
497
|
+
</Select>
|
|
498
|
+
)
|
|
499
|
+
|
|
500
|
+
const input = screen.getByLabelText('Choose an option')
|
|
501
|
+
|
|
502
|
+
await userEvent.click(input)
|
|
503
|
+
await waitFor(() => {
|
|
504
|
+
expect(onRequestShowOptions).toHaveBeenCalledTimes(1)
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
rerender(
|
|
508
|
+
<Select
|
|
509
|
+
renderLabel="Choose an option"
|
|
510
|
+
onRequestShowOptions={onRequestShowOptions}
|
|
511
|
+
isShowingOptions={true}
|
|
512
|
+
>
|
|
513
|
+
{getOptions()}
|
|
514
|
+
</Select>
|
|
515
|
+
)
|
|
516
|
+
|
|
517
|
+
await userEvent.click(input)
|
|
518
|
+
await waitFor(() => {
|
|
519
|
+
expect(onRequestShowOptions).toHaveBeenCalledTimes(1)
|
|
520
|
+
})
|
|
521
|
+
})
|
|
522
|
+
|
|
523
|
+
it('when up/down arrows are pressed', async () => {
|
|
524
|
+
const onRequestShowOptions = jest.fn()
|
|
525
|
+
|
|
526
|
+
render(
|
|
527
|
+
<Select
|
|
528
|
+
renderLabel="Choose an option"
|
|
529
|
+
onRequestShowOptions={onRequestShowOptions}
|
|
530
|
+
>
|
|
531
|
+
{getOptions()}
|
|
532
|
+
</Select>
|
|
533
|
+
)
|
|
534
|
+
|
|
535
|
+
const input = screen.getByLabelText('Choose an option')
|
|
536
|
+
|
|
537
|
+
await userEvent.type(input, '{arrowdown}')
|
|
538
|
+
await waitFor(() => {
|
|
539
|
+
expect(onRequestShowOptions).toHaveBeenCalledTimes(1)
|
|
540
|
+
})
|
|
541
|
+
|
|
542
|
+
await userEvent.type(input, '{arrowup}')
|
|
543
|
+
await waitFor(() => {
|
|
544
|
+
expect(onRequestShowOptions).toHaveBeenCalledTimes(2)
|
|
545
|
+
})
|
|
546
|
+
})
|
|
547
|
+
|
|
548
|
+
it('when space is pressed', async () => {
|
|
549
|
+
const onRequestShowOptions = jest.fn()
|
|
550
|
+
|
|
551
|
+
const { rerender } = render(
|
|
552
|
+
<Select
|
|
553
|
+
renderLabel="Choose an option"
|
|
554
|
+
onRequestShowOptions={onRequestShowOptions}
|
|
555
|
+
>
|
|
556
|
+
{getOptions()}
|
|
557
|
+
</Select>
|
|
558
|
+
)
|
|
559
|
+
|
|
560
|
+
const input = screen.getByLabelText('Choose an option')
|
|
561
|
+
|
|
562
|
+
await userEvent.type(input, '{space}')
|
|
563
|
+
await waitFor(() => {
|
|
564
|
+
expect(onRequestShowOptions).toHaveBeenCalledTimes(1)
|
|
565
|
+
})
|
|
566
|
+
|
|
567
|
+
rerender(
|
|
568
|
+
<Select
|
|
569
|
+
renderLabel="Choose an option"
|
|
570
|
+
onRequestShowOptions={onRequestShowOptions}
|
|
571
|
+
isShowingOptions={true}
|
|
572
|
+
>
|
|
573
|
+
{getOptions()}
|
|
574
|
+
</Select>
|
|
575
|
+
)
|
|
576
|
+
|
|
577
|
+
await userEvent.type(input, '{space}')
|
|
578
|
+
await waitFor(() => {
|
|
579
|
+
expect(onRequestShowOptions).toHaveBeenCalledTimes(1)
|
|
580
|
+
})
|
|
581
|
+
})
|
|
582
|
+
})
|
|
583
|
+
|
|
584
|
+
describe('should fire onRequestHideOptions', () => {
|
|
585
|
+
it('when root is clicked and isShowingOptions is true', async () => {
|
|
586
|
+
const onRequestHideOptions = jest.fn()
|
|
587
|
+
|
|
588
|
+
const { container } = render(
|
|
589
|
+
<Select
|
|
590
|
+
renderLabel="Choose an option"
|
|
591
|
+
isShowingOptions
|
|
592
|
+
onRequestHideOptions={onRequestHideOptions}
|
|
593
|
+
>
|
|
594
|
+
{getOptions()}
|
|
595
|
+
</Select>
|
|
596
|
+
)
|
|
597
|
+
|
|
598
|
+
const icon = container.querySelector('svg[name="IconArrowOpenUp"]')
|
|
599
|
+
const label = screen.getByText('Choose an option')
|
|
600
|
+
|
|
601
|
+
expect(icon).toBeInTheDocument()
|
|
602
|
+
expect(label).toBeInTheDocument()
|
|
603
|
+
|
|
604
|
+
await userEvent.click(label)
|
|
605
|
+
await waitFor(() => {
|
|
606
|
+
expect(onRequestHideOptions).toHaveBeenCalledTimes(1)
|
|
607
|
+
})
|
|
608
|
+
|
|
609
|
+
await userEvent.click(icon!)
|
|
610
|
+
await waitFor(() => {
|
|
611
|
+
expect(onRequestHideOptions).toHaveBeenCalledTimes(2)
|
|
612
|
+
})
|
|
613
|
+
})
|
|
614
|
+
|
|
615
|
+
it('when root is clicked and isShowingOptions is false should NOT fire onRequestHideOptions', async () => {
|
|
616
|
+
const onRequestHideOptions = jest.fn()
|
|
617
|
+
|
|
618
|
+
render(
|
|
619
|
+
<Select
|
|
620
|
+
renderLabel="Choose an option"
|
|
621
|
+
isShowingOptions={false}
|
|
622
|
+
onRequestHideOptions={onRequestHideOptions}
|
|
623
|
+
>
|
|
624
|
+
{getOptions()}
|
|
625
|
+
</Select>
|
|
626
|
+
)
|
|
627
|
+
|
|
628
|
+
const label = screen.getByText('Choose an option')
|
|
629
|
+
|
|
630
|
+
expect(label).toBeInTheDocument()
|
|
631
|
+
|
|
632
|
+
await userEvent.click(label)
|
|
633
|
+
await waitFor(() => {
|
|
634
|
+
expect(onRequestHideOptions).not.toHaveBeenCalled()
|
|
635
|
+
})
|
|
636
|
+
})
|
|
637
|
+
|
|
638
|
+
it('when input is clicked', async () => {
|
|
639
|
+
const onRequestHideOptions = jest.fn()
|
|
640
|
+
|
|
641
|
+
const { rerender } = render(
|
|
642
|
+
<Select
|
|
643
|
+
renderLabel="Choose an option"
|
|
644
|
+
isShowingOptions
|
|
645
|
+
onRequestHideOptions={onRequestHideOptions}
|
|
646
|
+
>
|
|
647
|
+
{getOptions()}
|
|
648
|
+
</Select>
|
|
649
|
+
)
|
|
650
|
+
|
|
651
|
+
const input = screen.getByLabelText('Choose an option')
|
|
652
|
+
|
|
653
|
+
await userEvent.click(input)
|
|
654
|
+
await waitFor(() => {
|
|
655
|
+
expect(onRequestHideOptions).toHaveBeenCalledTimes(1)
|
|
656
|
+
})
|
|
657
|
+
|
|
658
|
+
rerender(
|
|
659
|
+
<Select
|
|
660
|
+
renderLabel="Choose an option"
|
|
661
|
+
isShowingOptions={false}
|
|
662
|
+
onRequestHideOptions={onRequestHideOptions}
|
|
663
|
+
>
|
|
664
|
+
{getOptions()}
|
|
665
|
+
</Select>
|
|
666
|
+
)
|
|
667
|
+
await userEvent.click(input)
|
|
668
|
+
await waitFor(() => {
|
|
669
|
+
expect(onRequestHideOptions).toHaveBeenCalledTimes(1)
|
|
670
|
+
})
|
|
671
|
+
})
|
|
672
|
+
|
|
673
|
+
it('when escape is pressed', async () => {
|
|
674
|
+
const onRequestHideOptions = jest.fn()
|
|
675
|
+
|
|
676
|
+
render(
|
|
677
|
+
<Select
|
|
678
|
+
renderLabel="Choose an option"
|
|
679
|
+
isShowingOptions
|
|
680
|
+
onRequestHideOptions={onRequestHideOptions}
|
|
681
|
+
>
|
|
682
|
+
{getOptions()}
|
|
683
|
+
</Select>
|
|
684
|
+
)
|
|
685
|
+
|
|
686
|
+
const input = screen.getByLabelText('Choose an option')
|
|
687
|
+
|
|
688
|
+
await userEvent.type(input, '{esc}')
|
|
689
|
+
await waitFor(() => {
|
|
690
|
+
expect(onRequestHideOptions).toHaveBeenCalledTimes(1)
|
|
691
|
+
})
|
|
692
|
+
})
|
|
693
|
+
})
|
|
694
|
+
|
|
695
|
+
describe('should fire onRequestHighlightOption', () => {
|
|
696
|
+
it('when options are hovered', async () => {
|
|
697
|
+
const onRequestHighlightOption = jest.fn()
|
|
698
|
+
|
|
699
|
+
render(
|
|
700
|
+
<Select
|
|
701
|
+
renderLabel="Choose an option"
|
|
702
|
+
isShowingOptions
|
|
703
|
+
onRequestHighlightOption={onRequestHighlightOption}
|
|
704
|
+
>
|
|
705
|
+
{getOptions()}
|
|
706
|
+
</Select>
|
|
707
|
+
)
|
|
708
|
+
const options = screen.getAllByRole('option')
|
|
709
|
+
|
|
710
|
+
await userEvent.hover(options[0])
|
|
711
|
+
await waitFor(() => {
|
|
712
|
+
expect(onRequestHighlightOption).toHaveBeenCalledWith(
|
|
713
|
+
expect.anything(),
|
|
714
|
+
expect.objectContaining({
|
|
715
|
+
id: defaultOptions[0]
|
|
716
|
+
})
|
|
717
|
+
)
|
|
718
|
+
})
|
|
719
|
+
|
|
720
|
+
await userEvent.hover(options[1])
|
|
721
|
+
await waitFor(() => {
|
|
722
|
+
expect(onRequestHighlightOption).toHaveBeenCalledWith(
|
|
723
|
+
expect.anything(),
|
|
724
|
+
expect.objectContaining({
|
|
725
|
+
id: defaultOptions[1]
|
|
726
|
+
})
|
|
727
|
+
)
|
|
728
|
+
})
|
|
729
|
+
})
|
|
730
|
+
})
|
|
731
|
+
|
|
732
|
+
describe('input callbacks', () => {
|
|
733
|
+
it('should fire onInputChange when input is typed in', async () => {
|
|
734
|
+
const onInputChange = jest.fn()
|
|
735
|
+
|
|
736
|
+
render(
|
|
737
|
+
<Select renderLabel="Choose an option" onInputChange={onInputChange}>
|
|
738
|
+
{getOptions()}
|
|
739
|
+
</Select>
|
|
740
|
+
)
|
|
741
|
+
|
|
742
|
+
const input = screen.getByLabelText('Choose an option')
|
|
743
|
+
|
|
744
|
+
await userEvent.type(input, 'h')
|
|
745
|
+
await waitFor(() => {
|
|
746
|
+
expect(onInputChange).toHaveBeenCalled()
|
|
747
|
+
})
|
|
748
|
+
})
|
|
749
|
+
|
|
750
|
+
it('should fire onFocus when input gains focus', async () => {
|
|
751
|
+
const onFocus = jest.fn()
|
|
752
|
+
|
|
753
|
+
render(
|
|
754
|
+
<Select renderLabel="Choose an option" onFocus={onFocus}>
|
|
755
|
+
{getOptions()}
|
|
756
|
+
</Select>
|
|
757
|
+
)
|
|
758
|
+
|
|
759
|
+
const input = screen.getByLabelText('Choose an option')
|
|
760
|
+
|
|
761
|
+
input.focus()
|
|
762
|
+
await waitFor(() => {
|
|
763
|
+
expect(onFocus).toHaveBeenCalled()
|
|
764
|
+
})
|
|
765
|
+
})
|
|
766
|
+
})
|
|
767
|
+
})
|
|
768
|
+
|
|
769
|
+
describe('with generated examples', () => {
|
|
770
|
+
const generatedComponents = generateA11yTests(Select, SelectExamples)
|
|
771
|
+
|
|
772
|
+
it.each(generatedComponents)(
|
|
773
|
+
'should be accessible with example: $description',
|
|
774
|
+
async ({ content }) => {
|
|
775
|
+
const { container } = render(content)
|
|
776
|
+
const axeCheck = await runAxeCheck(container)
|
|
777
|
+
expect(axeCheck).toBe(true)
|
|
778
|
+
}
|
|
779
|
+
)
|
|
780
|
+
})
|
|
86
781
|
})
|