@instructure/ui-selectable 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,1084 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- var _react = require("@testing-library/react");
5
- var _vitest = require("vitest");
6
- var _userEvent = _interopRequireDefault(require("@testing-library/user-event"));
7
- require("@testing-library/jest-dom");
8
- var _index = require("../index");
9
- var _jsxRuntime = require("@emotion/react/jsx-runtime");
10
- /*
11
- * The MIT License (MIT)
12
- *
13
- * Copyright (c) 2015 - present Instructure, Inc.
14
- *
15
- * Permission is hereby granted, free of charge, to any person obtaining a copy
16
- * of this software and associated documentation files (the "Software"), to deal
17
- * in the Software without restriction, including without limitation the rights
18
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
- * copies of the Software, and to permit persons to whom the Software is
20
- * furnished to do so, subject to the following conditions:
21
- *
22
- * The above copyright notice and this permission notice shall be included in all
23
- * copies or substantial portions of the Software.
24
- *
25
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
- * SOFTWARE.
32
- */
33
-
34
- const defaultOptions = ['foo', 'bar', 'baz'];
35
- describe('<Selectable />', () => {
36
- const getSelectable = selectable => (0, _jsxRuntime.jsxs)("span", {
37
- ...selectable.getRootProps(),
38
- children: [(0, _jsxRuntime.jsx)("label", {
39
- ...selectable.getLabelProps(),
40
- children: "Selectable"
41
- }), (0, _jsxRuntime.jsx)("input", {
42
- type: "text",
43
- ...selectable.getTriggerProps(),
44
- ...selectable.getInputProps()
45
- }), (0, _jsxRuntime.jsx)("ul", {
46
- ...selectable.getListProps(),
47
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
48
- ...selectable.getOptionProps({
49
- id: opt
50
- }),
51
- children: opt
52
- }, opt))
53
- })]
54
- });
55
- it('should focus trigger when label is clicked', async () => {
56
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
57
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
58
- ...selectable.getRootProps(),
59
- children: [(0, _jsxRuntime.jsx)("label", {
60
- ...selectable.getLabelProps(),
61
- children: "Selectable"
62
- }), (0, _jsxRuntime.jsx)("input", {
63
- type: "text",
64
- ...selectable.getTriggerProps(),
65
- ...selectable.getInputProps()
66
- }), (0, _jsxRuntime.jsx)("ul", {
67
- ...selectable.getListProps(),
68
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
69
- ...selectable.getOptionProps({
70
- id: opt
71
- }),
72
- children: opt
73
- }, opt))
74
- })]
75
- })
76
- }));
77
- const label = _react.screen.getByText('Selectable');
78
- const input = _react.screen.getByRole('combobox');
79
- expect(document.activeElement).not.toBe(input);
80
- _userEvent.default.click(label);
81
- await (0, _react.waitFor)(() => {
82
- expect(document.activeElement).toBe(input);
83
- });
84
- });
85
- it('should not blur trigger when label is clicked', async () => {
86
- const onFocus = _vitest.vi.fn();
87
- const onBlur = _vitest.vi.fn();
88
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
89
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
90
- ...selectable.getRootProps(),
91
- children: [(0, _jsxRuntime.jsx)("label", {
92
- ...selectable.getLabelProps(),
93
- children: "Selectable"
94
- }), (0, _jsxRuntime.jsx)("input", {
95
- type: "text",
96
- ...selectable.getTriggerProps(),
97
- ...selectable.getInputProps(),
98
- onBlur: onBlur,
99
- onFocus: onFocus
100
- }), (0, _jsxRuntime.jsx)("ul", {
101
- ...selectable.getListProps(),
102
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
103
- ...selectable.getOptionProps({
104
- id: opt
105
- }),
106
- children: opt
107
- }, opt))
108
- })]
109
- })
110
- }));
111
- const label = _react.screen.getByText('Selectable');
112
- const input = _react.screen.getByRole('combobox');
113
- expect(document.activeElement).not.toBe(input);
114
- _userEvent.default.click(label);
115
- await (0, _react.waitFor)(() => {
116
- expect(onFocus).toHaveBeenCalledTimes(1);
117
- expect(onBlur).not.toHaveBeenCalled();
118
- });
119
- });
120
- it('should not hide options when list is clicked', async () => {
121
- const onClick = _vitest.vi.fn();
122
- const onMouseDown = _vitest.vi.fn();
123
- const onRequestHideOptions = _vitest.vi.fn();
124
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
125
- isShowingOptions: true,
126
- onRequestHideOptions: onRequestHideOptions,
127
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
128
- ...selectable.getRootProps(),
129
- children: [(0, _jsxRuntime.jsx)("label", {
130
- ...selectable.getLabelProps(),
131
- children: "Selectable"
132
- }), (0, _jsxRuntime.jsx)("input", {
133
- type: "text",
134
- ...selectable.getTriggerProps(),
135
- ...selectable.getInputProps()
136
- }), (0, _jsxRuntime.jsx)("ul", {
137
- ...selectable.getListProps({
138
- onClick,
139
- onMouseDown
140
- }),
141
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
142
- ...selectable.getOptionProps({
143
- id: opt
144
- }),
145
- children: opt
146
- }, opt))
147
- })]
148
- })
149
- }));
150
- const input = _react.screen.getByRole('combobox');
151
- const list = _react.screen.getByRole('listbox');
152
- input.focus();
153
- _userEvent.default.click(list);
154
- await (0, _react.waitFor)(() => {
155
- expect(document.activeElement).toBe(input);
156
- expect(onClick).toHaveBeenCalledTimes(1);
157
- expect(onMouseDown).toHaveBeenCalledTimes(1);
158
- expect(onRequestHideOptions).not.toHaveBeenCalled();
159
- });
160
- });
161
- it('should not hide options when an option is clicked', async () => {
162
- const onClick = _vitest.vi.fn();
163
- const onMouseDown = _vitest.vi.fn();
164
- const onRequestHideOptions = _vitest.vi.fn();
165
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
166
- isShowingOptions: true,
167
- onRequestHideOptions: onRequestHideOptions,
168
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
169
- ...selectable.getRootProps(),
170
- children: [(0, _jsxRuntime.jsx)("label", {
171
- ...selectable.getLabelProps(),
172
- children: "Selectable"
173
- }), (0, _jsxRuntime.jsx)("input", {
174
- type: "text",
175
- ...selectable.getTriggerProps(),
176
- ...selectable.getInputProps()
177
- }), (0, _jsxRuntime.jsx)("ul", {
178
- ...selectable.getListProps(),
179
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
180
- ...selectable.getOptionProps({
181
- id: opt,
182
- onClick,
183
- onMouseDown
184
- }),
185
- children: opt
186
- }, opt))
187
- })]
188
- })
189
- }));
190
- const input = _react.screen.getByRole('combobox');
191
- const option = _react.screen.getByText('foo');
192
- input.focus();
193
- _userEvent.default.click(option);
194
- await (0, _react.waitFor)(() => {
195
- expect(document.activeElement).toBe(input);
196
- expect(onClick).toHaveBeenCalledTimes(1);
197
- expect(onMouseDown).toHaveBeenCalledTimes(1);
198
- expect(onRequestHideOptions).not.toHaveBeenCalled();
199
- });
200
- });
201
- it('should not prevent events on other children', async () => {
202
- var _button;
203
- const onMouseDown = _vitest.vi.fn();
204
- const onClick = _vitest.vi.fn();
205
- const onKeyDown = _vitest.vi.fn();
206
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
207
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
208
- ...selectable.getRootProps(),
209
- children: [(0, _jsxRuntime.jsx)("label", {
210
- ...selectable.getLabelProps(),
211
- children: "Selectable"
212
- }), _button || (_button = (0, _jsxRuntime.jsx)("button", {
213
- onMouseDown: onMouseDown,
214
- onClick: onClick,
215
- onKeyDown: onKeyDown,
216
- children: "Selected"
217
- })), (0, _jsxRuntime.jsx)("input", {
218
- type: "text",
219
- ...selectable.getTriggerProps(),
220
- ...selectable.getInputProps()
221
- }), (0, _jsxRuntime.jsx)("ul", {
222
- ...selectable.getListProps(),
223
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
224
- ...selectable.getOptionProps({
225
- id: opt
226
- }),
227
- children: opt
228
- }, opt))
229
- })]
230
- })
231
- }));
232
- const button = _react.screen.getByText('Selected');
233
- _userEvent.default.click(button);
234
- await _userEvent.default.type(button, '{enter}');
235
- await (0, _react.waitFor)(() => {
236
- expect(onClick).toHaveBeenCalled();
237
- expect(onMouseDown).toHaveBeenCalled();
238
- expect(onKeyDown).toHaveBeenCalled();
239
- });
240
- });
241
- describe('with callbacks', () => {
242
- describe('should fire onRequestShowOptions', () => {
243
- it('when label is clicked', async () => {
244
- const onRequestShowOptions = _vitest.vi.fn();
245
- const _render = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
246
- isShowingOptions: false,
247
- onRequestShowOptions: onRequestShowOptions,
248
- children: selectable => getSelectable(selectable)
249
- })),
250
- rerender = _render.rerender;
251
- const label = _react.screen.getByText('Selectable');
252
- _userEvent.default.click(label);
253
- await (0, _react.waitFor)(() => {
254
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
255
- });
256
-
257
- // Set isShowingOptions:
258
- rerender((0, _jsxRuntime.jsx)(_index.Selectable, {
259
- isShowingOptions: true,
260
- onRequestShowOptions: onRequestShowOptions,
261
- children: selectable => getSelectable(selectable)
262
- }));
263
- _userEvent.default.click(label);
264
- await (0, _react.waitFor)(() => {
265
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
266
- });
267
- });
268
- it('when input is clicked', async () => {
269
- const onRequestShowOptions = _vitest.vi.fn();
270
- const _render2 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
271
- isShowingOptions: false,
272
- onRequestShowOptions: onRequestShowOptions,
273
- children: selectable => getSelectable(selectable)
274
- })),
275
- rerender = _render2.rerender;
276
- const input = _react.screen.getByRole('combobox');
277
- _userEvent.default.click(input);
278
- await (0, _react.waitFor)(() => {
279
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
280
- });
281
- rerender((0, _jsxRuntime.jsx)(_index.Selectable, {
282
- isShowingOptions: true,
283
- onRequestShowOptions: onRequestShowOptions,
284
- children: selectable => getSelectable(selectable)
285
- }));
286
- _userEvent.default.click(input);
287
- await (0, _react.waitFor)(() => {
288
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
289
- });
290
- });
291
- it('when up/down arrows are pressed', async () => {
292
- const onRequestShowOptions = _vitest.vi.fn();
293
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
294
- isShowingOptions: false,
295
- onRequestShowOptions: onRequestShowOptions,
296
- children: selectable => getSelectable(selectable)
297
- }));
298
- const input = _react.screen.getByRole('combobox');
299
- await _userEvent.default.type(input, '{arrowdown}');
300
- await (0, _react.waitFor)(() => {
301
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
302
- });
303
- await _userEvent.default.type(input, '{arrowup}');
304
- await (0, _react.waitFor)(() => {
305
- expect(onRequestShowOptions).toHaveBeenCalledTimes(2);
306
- });
307
- });
308
- it('when space is pressed', async () => {
309
- const onRequestShowOptions = _vitest.vi.fn();
310
- const _render3 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
311
- onRequestShowOptions: onRequestShowOptions,
312
- children: selectable => getSelectable(selectable)
313
- })),
314
- rerender = _render3.rerender;
315
- const input = _react.screen.getByRole('combobox');
316
- await _userEvent.default.type(input, '{space}');
317
- rerender((0, _jsxRuntime.jsx)(_index.Selectable, {
318
- isShowingOptions: true,
319
- onRequestShowOptions: onRequestShowOptions,
320
- children: selectable => getSelectable(selectable)
321
- }));
322
- await _userEvent.default.type(input, '{space}');
323
- await (0, _react.waitFor)(() => {
324
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
325
- });
326
- });
327
- });
328
- describe('should fire onRequestHideOptions', () => {
329
- it('when label is clicked', async () => {
330
- const onRequestHideOptions = _vitest.vi.fn();
331
- const _render4 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
332
- isShowingOptions: true,
333
- onRequestHideOptions: onRequestHideOptions,
334
- children: selectable => getSelectable(selectable)
335
- })),
336
- rerender = _render4.rerender;
337
- const label = _react.screen.getByText('Selectable');
338
- await _userEvent.default.click(label);
339
- await (0, _react.waitFor)(() => {
340
- expect(onRequestHideOptions).toHaveBeenCalledTimes(1);
341
- });
342
- rerender((0, _jsxRuntime.jsx)(_index.Selectable, {
343
- isShowingOptions: false,
344
- onRequestHideOptions: onRequestHideOptions,
345
- children: selectable => getSelectable(selectable)
346
- }));
347
- await _userEvent.default.click(label);
348
- await (0, _react.waitFor)(() => {
349
- expect(onRequestHideOptions).toHaveBeenCalledTimes(1);
350
- });
351
- });
352
- it('when input is clicked', async () => {
353
- const onRequestHideOptions = _vitest.vi.fn();
354
- const _render5 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
355
- isShowingOptions: true,
356
- onRequestHideOptions: onRequestHideOptions,
357
- children: selectable => getSelectable(selectable)
358
- })),
359
- rerender = _render5.rerender;
360
- const input = _react.screen.getByRole('combobox');
361
- await _userEvent.default.click(input);
362
- await (0, _react.waitFor)(() => {
363
- expect(onRequestHideOptions).toHaveBeenCalledTimes(1);
364
- });
365
- rerender((0, _jsxRuntime.jsx)(_index.Selectable, {
366
- isShowingOptions: false,
367
- onRequestHideOptions: onRequestHideOptions,
368
- children: selectable => getSelectable(selectable)
369
- }));
370
- await _userEvent.default.click(input);
371
- await (0, _react.waitFor)(() => {
372
- expect(onRequestHideOptions).toHaveBeenCalledTimes(1);
373
- });
374
- });
375
- });
376
- });
377
- describe('getRootProps()', () => {
378
- it('should provide prop getter for root element', async () => {
379
- const renderSpy = _vitest.vi.fn();
380
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
381
- children: selectable => {
382
- renderSpy(selectable);
383
- return null;
384
- }
385
- }));
386
- const selectableProps = renderSpy.mock.calls[0][0];
387
- expect(selectableProps.getRootProps).toBeDefined();
388
- });
389
- it('should allow custom props to pass through', async () => {
390
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
391
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
392
- ...selectable.getRootProps({
393
- 'data-custom-attr': true,
394
- className: 'customClass',
395
- style: {
396
- color: 'red'
397
- }
398
- }),
399
- children: [(0, _jsxRuntime.jsx)("label", {
400
- ...selectable.getLabelProps(),
401
- children: "Selectable"
402
- }), (0, _jsxRuntime.jsx)("input", {
403
- type: "text",
404
- ...selectable.getTriggerProps(),
405
- ...selectable.getInputProps()
406
- })]
407
- })
408
- }));
409
- const root = _react.screen.getByText('Selectable').closest('span');
410
- expect(root).toHaveAttribute('data-custom-attr', 'true');
411
- expect(root).toHaveClass('customClass');
412
- expect(root).toHaveStyle('color: rgb(255, 0, 0)');
413
- });
414
- it('should allow supplemental onClick behavior', async () => {
415
- const onClick = _vitest.vi.fn();
416
- const onRequestShowOptions = _vitest.vi.fn();
417
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
418
- onRequestShowOptions: onRequestShowOptions,
419
- children: selectable => (0, _jsxRuntime.jsx)("span", {
420
- children: (0, _jsxRuntime.jsx)("input", {
421
- type: "text",
422
- ...selectable.getTriggerProps({
423
- onClick
424
- }),
425
- ...selectable.getInputProps()
426
- })
427
- })
428
- }));
429
- const input = _react.screen.getByRole('combobox');
430
- await _userEvent.default.click(input);
431
- await (0, _react.waitFor)(() => {
432
- expect(onClick).toHaveBeenCalledTimes(1);
433
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
434
- });
435
- });
436
- });
437
- describe('getLabelProps()', () => {
438
- it('should provide prop getter for label element', async () => {
439
- const renderSpy = _vitest.vi.fn();
440
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
441
- children: selectable => {
442
- renderSpy(selectable);
443
- return null;
444
- }
445
- }));
446
- expect(renderSpy.mock.calls[0].length > 0).toBeTruthy();
447
- });
448
- it('should set htmlFor prop', async () => {
449
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
450
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
451
- children: [(0, _jsxRuntime.jsx)("label", {
452
- ...selectable.getLabelProps(),
453
- children: "Selectable"
454
- }), (0, _jsxRuntime.jsx)("input", {
455
- type: "text",
456
- ...selectable.getTriggerProps(),
457
- ...selectable.getInputProps()
458
- })]
459
- })
460
- }));
461
- const label = _react.screen.getByText('Selectable');
462
- const input = _react.screen.getByRole('combobox');
463
- expect(label).toHaveAttribute('for', input.getAttribute('id'));
464
- });
465
- it('should set htmlFor prop with custom defined id', async () => {
466
- const customId = 'CustomSelect';
467
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
468
- id: customId,
469
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
470
- children: [(0, _jsxRuntime.jsx)("label", {
471
- ...selectable.getLabelProps(),
472
- children: "Selectable"
473
- }), (0, _jsxRuntime.jsx)("input", {
474
- type: "text",
475
- ...selectable.getTriggerProps(),
476
- ...selectable.getInputProps()
477
- })]
478
- })
479
- }));
480
- const label = _react.screen.getByText('Selectable');
481
- const input = _react.screen.getByRole('combobox');
482
- expect(input).toHaveAttribute('id', customId);
483
- expect(label).toHaveAttribute('for', customId);
484
- });
485
- it('should allow custom props to pass through', async () => {
486
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
487
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
488
- children: [(0, _jsxRuntime.jsx)("label", {
489
- ...selectable.getLabelProps({
490
- 'data-custom-attr': true,
491
- className: 'customClass',
492
- style: {
493
- color: 'red'
494
- }
495
- }),
496
- children: "Selectable"
497
- }), (0, _jsxRuntime.jsx)("input", {
498
- type: "text",
499
- ...selectable.getTriggerProps(),
500
- ...selectable.getInputProps()
501
- })]
502
- })
503
- }));
504
- const label = _react.screen.getByText('Selectable');
505
- expect(label).toHaveAttribute('data-custom-attr', 'true');
506
- expect(label).toHaveClass('customClass');
507
- expect(label).toHaveStyle({
508
- color: 'rgb(255, 0, 0)'
509
- });
510
- });
511
- });
512
- describe('getTriggerProps()', () => {
513
- it('should provide prop getter for trigger element', async () => {
514
- const renderSpy = _vitest.vi.fn();
515
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
516
- children: selectable => {
517
- renderSpy(selectable);
518
- return null;
519
- }
520
- }));
521
- expect(renderSpy.mock.calls[0][0].getTriggerProps).toBeDefined();
522
- });
523
- it('should set appropriate prop defaults', async () => {
524
- const _render6 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
525
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
526
- children: [(0, _jsxRuntime.jsx)("input", {
527
- type: "text",
528
- ...selectable.getTriggerProps()
529
- }), (0, _jsxRuntime.jsx)("span", {
530
- ...selectable.getDescriptionProps(),
531
- children: "description"
532
- })]
533
- })
534
- })),
535
- container = _render6.container;
536
- const input = container.querySelector('input');
537
- const desc = _react.screen.getByText('description');
538
- expect(input).toHaveAttribute('aria-haspopup', 'listbox');
539
- expect(input).toHaveAttribute('aria-describedby', desc.id);
540
- expect(input).toHaveAttribute('id');
541
- });
542
- it('should set appropriate props based on isShowingOptions', async () => {
543
- const _render7 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
544
- isShowingOptions: false,
545
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
546
- children: [(0, _jsxRuntime.jsx)("input", {
547
- type: "text",
548
- ...selectable.getTriggerProps()
549
- }), (0, _jsxRuntime.jsx)("ul", {
550
- ...selectable.getListProps()
551
- })]
552
- })
553
- })),
554
- container = _render7.container,
555
- rerender = _render7.rerender;
556
- const input = container.querySelector('input');
557
- const list = _react.screen.getByRole('listbox');
558
- expect(input).toHaveAttribute('aria-expanded', 'false');
559
- expect(input).not.toHaveAttribute('aria-controls');
560
-
561
- // Set prop: isShowingOptions true
562
- rerender((0, _jsxRuntime.jsx)(_index.Selectable, {
563
- isShowingOptions: true,
564
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
565
- children: [(0, _jsxRuntime.jsx)("input", {
566
- type: "text",
567
- ...selectable.getTriggerProps()
568
- }), (0, _jsxRuntime.jsx)("ul", {
569
- ...selectable.getListProps()
570
- })]
571
- })
572
- }));
573
- expect(input).toHaveAttribute('aria-expanded', 'true');
574
- expect(input).toHaveAttribute('aria-controls', list.id);
575
- });
576
- it('should set appropriate props based on highlightedOptionId', async () => {
577
- const _render8 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
578
- isShowingOptions: true,
579
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
580
- children: [(0, _jsxRuntime.jsx)("input", {
581
- type: "text",
582
- ...selectable.getTriggerProps()
583
- }), (0, _jsxRuntime.jsx)("ul", {
584
- ...selectable.getListProps(),
585
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
586
- ...selectable.getOptionProps({
587
- id: opt
588
- })
589
- }, opt))
590
- })]
591
- })
592
- })),
593
- container = _render8.container,
594
- rerender = _render8.rerender;
595
- const input = container.querySelector('input');
596
- const options = _react.screen.getAllByRole('option');
597
- expect(input).not.toHaveAttribute('aria-activedescendant');
598
-
599
- // Set prop: highlightedOptionId
600
- rerender((0, _jsxRuntime.jsx)(_index.Selectable, {
601
- isShowingOptions: true,
602
- highlightedOptionId: defaultOptions[0],
603
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
604
- children: [(0, _jsxRuntime.jsx)("input", {
605
- type: "text",
606
- ...selectable.getTriggerProps()
607
- }), (0, _jsxRuntime.jsx)("ul", {
608
- ...selectable.getListProps(),
609
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
610
- ...selectable.getOptionProps({
611
- id: opt
612
- })
613
- }, opt))
614
- })]
615
- })
616
- }));
617
- expect(input).toHaveAttribute('aria-activedescendant', options[0].id);
618
-
619
- // Set prop: highlightedOptionId
620
- rerender((0, _jsxRuntime.jsx)(_index.Selectable, {
621
- isShowingOptions: true,
622
- highlightedOptionId: defaultOptions[1],
623
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
624
- children: [(0, _jsxRuntime.jsx)("input", {
625
- type: "text",
626
- ...selectable.getTriggerProps()
627
- }), (0, _jsxRuntime.jsx)("ul", {
628
- ...selectable.getListProps(),
629
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
630
- ...selectable.getOptionProps({
631
- id: opt
632
- })
633
- }, opt))
634
- })]
635
- })
636
- }));
637
- expect(input).toHaveAttribute('aria-activedescendant', options[1].id);
638
-
639
- // Set prop: isShowingOptions
640
- rerender((0, _jsxRuntime.jsx)(_index.Selectable, {
641
- isShowingOptions: false,
642
- highlightedOptionId: defaultOptions[1],
643
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
644
- children: [(0, _jsxRuntime.jsx)("input", {
645
- type: "text",
646
- ...selectable.getTriggerProps()
647
- }), (0, _jsxRuntime.jsx)("ul", {
648
- ...selectable.getListProps(),
649
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
650
- ...selectable.getOptionProps({
651
- id: opt
652
- })
653
- }, opt))
654
- })]
655
- })
656
- }));
657
- expect(input).not.toHaveAttribute('aria-activedescendant');
658
- });
659
- it('should allow custom props to pass through', async () => {
660
- const _render9 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
661
- children: selectable => (0, _jsxRuntime.jsx)("span", {
662
- children: (0, _jsxRuntime.jsx)("input", {
663
- type: "text",
664
- ...selectable.getTriggerProps({
665
- 'data-custom-attr': true,
666
- placeholder: 'Type to enter text',
667
- className: 'customClass',
668
- style: {
669
- color: 'red'
670
- }
671
- })
672
- })
673
- })
674
- })),
675
- container = _render9.container;
676
- const input = container.querySelector('input');
677
- expect(input).toHaveAttribute('data-custom-attr', 'true');
678
- expect(input).toHaveAttribute('style', 'color: red;');
679
- expect(input).toHaveAttribute('placeholder');
680
- expect(input).toHaveClass('customClass');
681
- });
682
- it('should allow props to be overridden', async () => {
683
- const _render10 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
684
- children: selectable => (0, _jsxRuntime.jsx)("span", {
685
- children: (0, _jsxRuntime.jsx)("input", {
686
- type: "text",
687
- ...selectable.getTriggerProps({
688
- 'aria-haspopup': 'menu',
689
- 'aria-controls': 'customId'
690
- })
691
- })
692
- })
693
- })),
694
- container = _render10.container;
695
- const input = container.querySelector('input');
696
- expect(input).toHaveAttribute('aria-haspopup', 'menu');
697
- expect(input).toHaveAttribute('aria-controls', 'customId');
698
- });
699
- it('should provide a ref to the text input', async () => {
700
- const inputRef = _vitest.vi.fn();
701
- const _render11 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
702
- children: selectable => (0, _jsxRuntime.jsx)("span", {
703
- children: (0, _jsxRuntime.jsx)("input", {
704
- type: "text",
705
- ...selectable.getTriggerProps({
706
- ref: inputRef
707
- })
708
- })
709
- })
710
- })),
711
- container = _render11.container;
712
- const input = container.querySelector('input');
713
- expect(inputRef).toHaveBeenCalledWith(input);
714
- });
715
- it('should allow role and aria-autocomplete props to be overridden', async () => {
716
- const _render12 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
717
- children: selectable => (0, _jsxRuntime.jsx)("span", {
718
- children: (0, _jsxRuntime.jsx)("input", {
719
- type: "text",
720
- ...selectable.getTriggerProps({
721
- role: 'textbox',
722
- 'aria-autocomplete': 'list'
723
- })
724
- })
725
- })
726
- })),
727
- container = _render12.container;
728
- const input = container.querySelector('input');
729
- expect(input).toHaveAttribute('role', 'textbox');
730
- expect(input).toHaveAttribute('aria-autocomplete', 'list');
731
- });
732
- it('should allow supplemental onClick behavior', async () => {
733
- const onClick = _vitest.vi.fn();
734
- const onRequestShowOptions = _vitest.vi.fn();
735
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
736
- onRequestShowOptions: onRequestShowOptions,
737
- children: selectable => (0, _jsxRuntime.jsx)("span", {
738
- ...selectable.getRootProps(),
739
- children: (0, _jsxRuntime.jsx)("input", {
740
- type: "text",
741
- ...selectable.getTriggerProps({
742
- onClick
743
- }),
744
- ...selectable.getInputProps()
745
- })
746
- })
747
- }));
748
- const input = _react.screen.getByRole('combobox');
749
- await input.click();
750
- expect(onClick).toHaveBeenCalledTimes(1);
751
- expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
752
- });
753
- });
754
- describe('getInputProps()', () => {
755
- it('should provide prop getter for trigger element', async () => {
756
- const renderSpy = _vitest.vi.fn();
757
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
758
- children: selectable => {
759
- renderSpy(selectable);
760
- return null;
761
- }
762
- }));
763
- const args = renderSpy.mock.calls[0][0];
764
- expect(args.getInputProps).toBeDefined();
765
- });
766
- it('should set appropriate prop defaults', async () => {
767
- const _render13 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
768
- children: selectable => (0, _jsxRuntime.jsx)("span", {
769
- children: (0, _jsxRuntime.jsx)("input", {
770
- type: "text",
771
- ...selectable.getInputProps()
772
- })
773
- })
774
- })),
775
- container = _render13.container;
776
- const input = container.querySelector('input');
777
- expect(input).toHaveAttribute('role', 'combobox');
778
- expect(input).toHaveAttribute('aria-autocomplete', 'both');
779
- expect(input).toHaveAttribute('autocomplete', 'off');
780
- });
781
- it('should set appropriate props when readOnly', async () => {
782
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
783
- children: selectable => (0, _jsxRuntime.jsx)("span", {
784
- children: (0, _jsxRuntime.jsx)("input", {
785
- type: "text",
786
- ...selectable.getInputProps({
787
- readOnly: true
788
- })
789
- })
790
- })
791
- }));
792
- const input = _react.screen.getByRole('combobox');
793
- expect(input).toHaveAttribute('aria-autocomplete', 'none');
794
- expect(input).toHaveAttribute('readOnly');
795
- expect(input).not.toHaveAttribute('disabled');
796
- });
797
- it('should allow props to be overridden', async () => {
798
- const _render14 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
799
- children: selectable => (0, _jsxRuntime.jsx)("span", {
800
- children: (0, _jsxRuntime.jsx)("input", {
801
- type: "text",
802
- ...selectable.getInputProps({
803
- role: 'textbox',
804
- 'aria-autocomplete': 'inline'
805
- })
806
- })
807
- })
808
- })),
809
- container = _render14.container;
810
- const input = container.querySelector('input');
811
- expect(input).toHaveAttribute('role', 'textbox');
812
- expect(input).toHaveAttribute('aria-autocomplete', 'inline');
813
- });
814
- it('should allow custom props to pass through', async () => {
815
- const _render15 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
816
- children: selectable => (0, _jsxRuntime.jsx)("span", {
817
- children: (0, _jsxRuntime.jsx)("input", {
818
- type: "text",
819
- ...selectable.getInputProps({
820
- 'data-custom-attr': true,
821
- placeholder: 'Type to enter text',
822
- className: 'customClass',
823
- style: {
824
- color: 'red'
825
- }
826
- })
827
- })
828
- })
829
- })),
830
- container = _render15.container;
831
- const input = container.querySelector('input');
832
- expect(input).toHaveAttribute('data-custom-attr', 'true');
833
- expect(input).toHaveAttribute('placeholder');
834
- expect(input).toHaveClass('customClass');
835
- expect(input).toHaveAttribute('style', 'color: red;');
836
- });
837
- });
838
- describe('getListProps()', () => {
839
- it('should provide prop getter for list element', async () => {
840
- const renderSpy = _vitest.vi.fn();
841
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
842
- children: selectable => {
843
- renderSpy(selectable);
844
- return null;
845
- }
846
- }));
847
- const args = renderSpy.mock.calls[0][0];
848
- expect(args.getListProps).toBeDefined();
849
- });
850
- it('should set appropriate prop defaults', async () => {
851
- const _render16 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
852
- isShowingOptions: true,
853
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
854
- ...selectable.getRootProps(),
855
- children: [(0, _jsxRuntime.jsx)("input", {
856
- type: "text",
857
- ...selectable.getTriggerProps()
858
- }), (0, _jsxRuntime.jsx)("ul", {
859
- ...selectable.getListProps()
860
- })]
861
- })
862
- })),
863
- container = _render16.container;
864
- const list = container.querySelector('ul');
865
- expect(list).toHaveAttribute('id');
866
- expect(list).toHaveAttribute('role', 'listbox');
867
- });
868
- it('should allow custom props to pass through', async () => {
869
- const _render17 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
870
- isShowingOptions: true,
871
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
872
- ...selectable.getRootProps(),
873
- children: [(0, _jsxRuntime.jsx)("input", {
874
- type: "text",
875
- ...selectable.getTriggerProps()
876
- }), (0, _jsxRuntime.jsx)("ul", {
877
- ...selectable.getListProps({
878
- 'data-custom-attr': true,
879
- className: 'customClass',
880
- style: {
881
- color: 'red'
882
- }
883
- })
884
- })]
885
- })
886
- })),
887
- container = _render17.container;
888
- const list = container.querySelector('ul');
889
- expect(list).toHaveAttribute('data-custom-attr', 'true');
890
- expect(list).toHaveClass('customClass');
891
- expect(list).toHaveAttribute('style', 'color: red;');
892
- });
893
- });
894
- describe('getOptionProps()', () => {
895
- it('should provide prop getter for option element', async () => {
896
- const renderSpy = _vitest.vi.fn();
897
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
898
- children: selectable => {
899
- renderSpy(selectable);
900
- return null;
901
- }
902
- }));
903
- const args = renderSpy.mock.calls[0][0];
904
- expect(args.getOptionProps).toBeDefined();
905
- });
906
- it('should set appropriate prop defaults', async () => {
907
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
908
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
909
- children: [(0, _jsxRuntime.jsx)("input", {
910
- type: "text",
911
- ...selectable.getTriggerProps()
912
- }), (0, _jsxRuntime.jsx)("ul", {
913
- ...selectable.getListProps(),
914
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
915
- ...selectable.getOptionProps({
916
- id: opt
917
- }),
918
- children: opt
919
- }, opt))
920
- })]
921
- })
922
- }));
923
- const option = _react.screen.getByText(defaultOptions[0]);
924
- expect(option.tagName).toBe('LI');
925
- expect(option).toHaveAttribute('role', 'option');
926
- expect(option).toHaveAttribute('aria-selected', 'false');
927
- });
928
- it('should set aria-selected based on selectedOptionId', async () => {
929
- const _render18 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
930
- selectedOptionId: defaultOptions[1],
931
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
932
- children: [(0, _jsxRuntime.jsx)("input", {
933
- type: "text",
934
- ...selectable.getTriggerProps()
935
- }), (0, _jsxRuntime.jsx)("ul", {
936
- ...selectable.getListProps(),
937
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
938
- ...selectable.getOptionProps({
939
- id: opt
940
- }),
941
- children: opt
942
- }, opt))
943
- })]
944
- })
945
- })),
946
- container = _render18.container,
947
- rerender = _render18.rerender;
948
- const options = container.querySelectorAll('li');
949
- expect(options[0]).toHaveAttribute('aria-selected', 'false');
950
- expect(options[1]).toHaveAttribute('aria-selected', 'true');
951
- expect(options[2]).toHaveAttribute('aria-selected', 'false');
952
-
953
- // Set prop: selectedOptionId
954
- rerender((0, _jsxRuntime.jsx)(_index.Selectable, {
955
- selectedOptionId: [defaultOptions[0], defaultOptions[1]],
956
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
957
- children: [(0, _jsxRuntime.jsx)("input", {
958
- type: "text",
959
- ...selectable.getTriggerProps()
960
- }), (0, _jsxRuntime.jsx)("ul", {
961
- ...selectable.getListProps(),
962
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
963
- ...selectable.getOptionProps({
964
- id: opt
965
- }),
966
- children: opt
967
- }, opt))
968
- })]
969
- })
970
- }));
971
- expect(options[0]).toHaveAttribute('aria-selected', 'true');
972
- expect(options[1]).toHaveAttribute('aria-selected', 'true');
973
- expect(options[2]).toHaveAttribute('aria-selected', 'false');
974
- });
975
- it('should allow custom props to pass through', async () => {
976
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
977
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
978
- children: [(0, _jsxRuntime.jsx)("input", {
979
- type: "text",
980
- ...selectable.getTriggerProps()
981
- }), (0, _jsxRuntime.jsx)("ul", {
982
- ...selectable.getListProps(),
983
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
984
- ...selectable.getOptionProps({
985
- id: opt,
986
- 'data-custom-attr': true,
987
- className: 'customClass',
988
- style: {
989
- color: 'red'
990
- }
991
- }),
992
- children: opt
993
- }, opt))
994
- })]
995
- })
996
- }));
997
- const option = _react.screen.getByText(defaultOptions[0]);
998
- expect(option.tagName).toBe('LI');
999
- expect(option).toHaveAttribute('data-custom-attr', 'true');
1000
- expect(option).toHaveClass('customClass');
1001
- expect(option).toHaveAttribute('style', 'color: red;');
1002
- });
1003
- it('should allow supplemental onClick behavior', async () => {
1004
- const onClick = _vitest.vi.fn();
1005
- const onRequestSelectOption = _vitest.vi.fn();
1006
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
1007
- isShowingOptions: true,
1008
- onRequestSelectOption: onRequestSelectOption,
1009
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
1010
- children: [(0, _jsxRuntime.jsx)("input", {
1011
- type: "text",
1012
- ...selectable.getInputProps()
1013
- }), (0, _jsxRuntime.jsx)("ul", {
1014
- ...selectable.getListProps(),
1015
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
1016
- ...selectable.getOptionProps({
1017
- id: opt,
1018
- onClick
1019
- }),
1020
- children: opt
1021
- }, opt))
1022
- })]
1023
- })
1024
- }));
1025
- const option_0 = _react.screen.getByText(defaultOptions[0]);
1026
- const option_1 = _react.screen.getByText(defaultOptions[1]);
1027
- _userEvent.default.click(option_0);
1028
- _userEvent.default.click(option_1);
1029
- await (0, _react.waitFor)(() => {
1030
- expect(onRequestSelectOption).toHaveBeenCalledTimes(2);
1031
- expect(onClick).toHaveBeenCalledTimes(2);
1032
- });
1033
- });
1034
- });
1035
- describe('getDisabledOptionProps()', () => {
1036
- it('should provide prop getter for disabled option element', async () => {
1037
- const renderSpy = _vitest.vi.fn();
1038
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
1039
- children: selectable => {
1040
- renderSpy(selectable);
1041
- return null;
1042
- }
1043
- }));
1044
- const args = renderSpy.mock.calls[0][0];
1045
- expect(args.getDisabledOptionProps).toBeDefined();
1046
- });
1047
- it('should set aria-disabled prop', async () => {
1048
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
1049
- children: selectable => (0, _jsxRuntime.jsxs)("span", {
1050
- children: [(0, _jsxRuntime.jsx)("input", {
1051
- type: "text",
1052
- ...selectable.getTriggerProps(),
1053
- ...selectable.getInputProps()
1054
- }), (0, _jsxRuntime.jsx)("ul", {
1055
- ...selectable.getListProps(),
1056
- children: defaultOptions.map(opt => (0, _jsxRuntime.jsx)("li", {
1057
- ...selectable.getOptionProps({
1058
- id: opt
1059
- }),
1060
- ...selectable.getDisabledOptionProps(),
1061
- children: opt
1062
- }, opt))
1063
- })]
1064
- })
1065
- }));
1066
- const option = _react.screen.getByText(defaultOptions[0]);
1067
- expect(option.tagName).toBe('LI');
1068
- expect(option).toHaveAttribute('aria-disabled', 'true');
1069
- });
1070
- });
1071
- describe('getDescriptionProps()', () => {
1072
- it('should provide prop getter for description element', async () => {
1073
- const renderSpy = _vitest.vi.fn();
1074
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.Selectable, {
1075
- children: selectable => {
1076
- renderSpy(selectable);
1077
- return null;
1078
- }
1079
- }));
1080
- const selectableProps = renderSpy.mock.calls[0][0];
1081
- expect(selectableProps.getDescriptionProps).toBeDefined();
1082
- });
1083
- });
1084
- });