@instructure/ui-selectable 10.11.1-snapshot-2 → 10.11.1-snapshot-3

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.
@@ -0,0 +1,772 @@
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 React from 'react';
26
+ import { render, screen, waitFor } from '@testing-library/react';
27
+ import { vi } from 'vitest';
28
+ import userEvent from '@testing-library/user-event';
29
+ import '@testing-library/jest-dom';
30
+ import { Selectable } from '../index';
31
+ const defaultOptions = ['foo', 'bar', 'baz'];
32
+ describe('<Selectable />', () => {
33
+ const getSelectable = selectable => /*#__PURE__*/React.createElement("span", selectable.getRootProps(), /*#__PURE__*/React.createElement("label", selectable.getLabelProps(), "Selectable"), /*#__PURE__*/React.createElement("input", Object.assign({
34
+ type: "text"
35
+ }, selectable.getTriggerProps(), selectable.getInputProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
36
+ key: opt
37
+ }, selectable.getOptionProps({
38
+ id: opt
39
+ })), opt))));
40
+ it('should focus trigger when label is clicked', async () => {
41
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", selectable.getRootProps(), /*#__PURE__*/React.createElement("label", selectable.getLabelProps(), "Selectable"), /*#__PURE__*/React.createElement("input", Object.assign({
42
+ type: "text"
43
+ }, selectable.getTriggerProps(), selectable.getInputProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
44
+ key: opt
45
+ }, selectable.getOptionProps({
46
+ id: opt
47
+ })), opt))))));
48
+ const label = screen.getByText('Selectable');
49
+ const input = screen.getByRole('combobox');
50
+ expect(document.activeElement).not.toBe(input);
51
+ userEvent.click(label);
52
+ await waitFor(() => {
53
+ expect(document.activeElement).toBe(input);
54
+ });
55
+ });
56
+ it('should not blur trigger when label is clicked', async () => {
57
+ const onFocus = vi.fn();
58
+ const onBlur = vi.fn();
59
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", selectable.getRootProps(), /*#__PURE__*/React.createElement("label", selectable.getLabelProps(), "Selectable"), /*#__PURE__*/React.createElement("input", Object.assign({
60
+ type: "text"
61
+ }, selectable.getTriggerProps(), selectable.getInputProps(), {
62
+ onBlur: onBlur,
63
+ onFocus: onFocus
64
+ })), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
65
+ key: opt
66
+ }, selectable.getOptionProps({
67
+ id: opt
68
+ })), opt))))));
69
+ const label = screen.getByText('Selectable');
70
+ const input = screen.getByRole('combobox');
71
+ expect(document.activeElement).not.toBe(input);
72
+ userEvent.click(label);
73
+ await waitFor(() => {
74
+ expect(onFocus).toHaveBeenCalledTimes(1);
75
+ expect(onBlur).not.toHaveBeenCalled();
76
+ });
77
+ });
78
+ it('should not hide options when list is clicked', async () => {
79
+ const onClick = vi.fn();
80
+ const onMouseDown = vi.fn();
81
+ const onRequestHideOptions = vi.fn();
82
+ render(/*#__PURE__*/React.createElement(Selectable, {
83
+ isShowingOptions: true,
84
+ onRequestHideOptions: onRequestHideOptions
85
+ }, selectable => /*#__PURE__*/React.createElement("span", selectable.getRootProps(), /*#__PURE__*/React.createElement("label", selectable.getLabelProps(), "Selectable"), /*#__PURE__*/React.createElement("input", Object.assign({
86
+ type: "text"
87
+ }, selectable.getTriggerProps(), selectable.getInputProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps({
88
+ onClick,
89
+ onMouseDown
90
+ }), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
91
+ key: opt
92
+ }, selectable.getOptionProps({
93
+ id: opt
94
+ })), opt))))));
95
+ const input = screen.getByRole('combobox');
96
+ const list = screen.getByRole('listbox');
97
+ input.focus();
98
+ userEvent.click(list);
99
+ await waitFor(() => {
100
+ expect(document.activeElement).toBe(input);
101
+ expect(onClick).toHaveBeenCalledTimes(1);
102
+ expect(onMouseDown).toHaveBeenCalledTimes(1);
103
+ expect(onRequestHideOptions).not.toHaveBeenCalled();
104
+ });
105
+ });
106
+ it('should not hide options when an option is clicked', async () => {
107
+ const onClick = vi.fn();
108
+ const onMouseDown = vi.fn();
109
+ const onRequestHideOptions = vi.fn();
110
+ render(/*#__PURE__*/React.createElement(Selectable, {
111
+ isShowingOptions: true,
112
+ onRequestHideOptions: onRequestHideOptions
113
+ }, selectable => /*#__PURE__*/React.createElement("span", selectable.getRootProps(), /*#__PURE__*/React.createElement("label", selectable.getLabelProps(), "Selectable"), /*#__PURE__*/React.createElement("input", Object.assign({
114
+ type: "text"
115
+ }, selectable.getTriggerProps(), selectable.getInputProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
116
+ key: opt
117
+ }, selectable.getOptionProps({
118
+ id: opt,
119
+ onClick,
120
+ onMouseDown
121
+ })), opt))))));
122
+ const input = screen.getByRole('combobox');
123
+ const option = screen.getByText('foo');
124
+ input.focus();
125
+ userEvent.click(option);
126
+ await waitFor(() => {
127
+ expect(document.activeElement).toBe(input);
128
+ expect(onClick).toHaveBeenCalledTimes(1);
129
+ expect(onMouseDown).toHaveBeenCalledTimes(1);
130
+ expect(onRequestHideOptions).not.toHaveBeenCalled();
131
+ });
132
+ });
133
+ it('should not prevent events on other children', async () => {
134
+ var _button;
135
+ const onMouseDown = vi.fn();
136
+ const onClick = vi.fn();
137
+ const onKeyDown = vi.fn();
138
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", selectable.getRootProps(), /*#__PURE__*/React.createElement("label", selectable.getLabelProps(), "Selectable"), _button || (_button = /*#__PURE__*/React.createElement("button", {
139
+ onMouseDown: onMouseDown,
140
+ onClick: onClick,
141
+ onKeyDown: onKeyDown
142
+ }, "Selected")), /*#__PURE__*/React.createElement("input", Object.assign({
143
+ type: "text"
144
+ }, selectable.getTriggerProps(), selectable.getInputProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
145
+ key: opt
146
+ }, selectable.getOptionProps({
147
+ id: opt
148
+ })), opt))))));
149
+ const button = screen.getByText('Selected');
150
+ userEvent.click(button);
151
+ await userEvent.type(button, '{enter}');
152
+ await waitFor(() => {
153
+ expect(onClick).toHaveBeenCalled();
154
+ expect(onMouseDown).toHaveBeenCalled();
155
+ expect(onKeyDown).toHaveBeenCalled();
156
+ });
157
+ });
158
+ describe('with callbacks', () => {
159
+ describe('should fire onRequestShowOptions', () => {
160
+ it('when label is clicked', async () => {
161
+ const onRequestShowOptions = vi.fn();
162
+ const _render = render(/*#__PURE__*/React.createElement(Selectable, {
163
+ isShowingOptions: false,
164
+ onRequestShowOptions: onRequestShowOptions
165
+ }, selectable => getSelectable(selectable))),
166
+ rerender = _render.rerender;
167
+ const label = screen.getByText('Selectable');
168
+ userEvent.click(label);
169
+ await waitFor(() => {
170
+ expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
171
+ });
172
+
173
+ // Set isShowingOptions:
174
+ rerender(/*#__PURE__*/React.createElement(Selectable, {
175
+ isShowingOptions: true,
176
+ onRequestShowOptions: onRequestShowOptions
177
+ }, selectable => getSelectable(selectable)));
178
+ userEvent.click(label);
179
+ await waitFor(() => {
180
+ expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
181
+ });
182
+ });
183
+ it('when input is clicked', async () => {
184
+ const onRequestShowOptions = vi.fn();
185
+ const _render2 = render(/*#__PURE__*/React.createElement(Selectable, {
186
+ isShowingOptions: false,
187
+ onRequestShowOptions: onRequestShowOptions
188
+ }, selectable => getSelectable(selectable))),
189
+ rerender = _render2.rerender;
190
+ const input = screen.getByRole('combobox');
191
+ userEvent.click(input);
192
+ await waitFor(() => {
193
+ expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
194
+ });
195
+ rerender(/*#__PURE__*/React.createElement(Selectable, {
196
+ isShowingOptions: true,
197
+ onRequestShowOptions: onRequestShowOptions
198
+ }, selectable => getSelectable(selectable)));
199
+ userEvent.click(input);
200
+ await waitFor(() => {
201
+ expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
202
+ });
203
+ });
204
+ it('when up/down arrows are pressed', async () => {
205
+ const onRequestShowOptions = vi.fn();
206
+ render(/*#__PURE__*/React.createElement(Selectable, {
207
+ isShowingOptions: false,
208
+ onRequestShowOptions: onRequestShowOptions
209
+ }, selectable => getSelectable(selectable)));
210
+ const input = screen.getByRole('combobox');
211
+ await userEvent.type(input, '{arrowdown}');
212
+ await waitFor(() => {
213
+ expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
214
+ });
215
+ await userEvent.type(input, '{arrowup}');
216
+ await waitFor(() => {
217
+ expect(onRequestShowOptions).toHaveBeenCalledTimes(2);
218
+ });
219
+ });
220
+ it('when space is pressed', async () => {
221
+ const onRequestShowOptions = vi.fn();
222
+ const _render3 = render(/*#__PURE__*/React.createElement(Selectable, {
223
+ onRequestShowOptions: onRequestShowOptions
224
+ }, selectable => getSelectable(selectable))),
225
+ rerender = _render3.rerender;
226
+ const input = screen.getByRole('combobox');
227
+ await userEvent.type(input, '{space}');
228
+ rerender(/*#__PURE__*/React.createElement(Selectable, {
229
+ isShowingOptions: true,
230
+ onRequestShowOptions: onRequestShowOptions
231
+ }, selectable => getSelectable(selectable)));
232
+ await userEvent.type(input, '{space}');
233
+ await waitFor(() => {
234
+ expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
235
+ });
236
+ });
237
+ });
238
+ describe('should fire onRequestHideOptions', () => {
239
+ it('when label is clicked', async () => {
240
+ const onRequestHideOptions = vi.fn();
241
+ const _render4 = render(/*#__PURE__*/React.createElement(Selectable, {
242
+ isShowingOptions: true,
243
+ onRequestHideOptions: onRequestHideOptions
244
+ }, selectable => getSelectable(selectable))),
245
+ rerender = _render4.rerender;
246
+ const label = screen.getByText('Selectable');
247
+ await userEvent.click(label);
248
+ await waitFor(() => {
249
+ expect(onRequestHideOptions).toHaveBeenCalledTimes(1);
250
+ });
251
+ rerender(/*#__PURE__*/React.createElement(Selectable, {
252
+ isShowingOptions: false,
253
+ onRequestHideOptions: onRequestHideOptions
254
+ }, selectable => getSelectable(selectable)));
255
+ await userEvent.click(label);
256
+ await waitFor(() => {
257
+ expect(onRequestHideOptions).toHaveBeenCalledTimes(1);
258
+ });
259
+ });
260
+ it('when input is clicked', async () => {
261
+ const onRequestHideOptions = vi.fn();
262
+ const _render5 = render(/*#__PURE__*/React.createElement(Selectable, {
263
+ isShowingOptions: true,
264
+ onRequestHideOptions: onRequestHideOptions
265
+ }, selectable => getSelectable(selectable))),
266
+ rerender = _render5.rerender;
267
+ const input = screen.getByRole('combobox');
268
+ await userEvent.click(input);
269
+ await waitFor(() => {
270
+ expect(onRequestHideOptions).toHaveBeenCalledTimes(1);
271
+ });
272
+ rerender(/*#__PURE__*/React.createElement(Selectable, {
273
+ isShowingOptions: false,
274
+ onRequestHideOptions: onRequestHideOptions
275
+ }, selectable => getSelectable(selectable)));
276
+ await userEvent.click(input);
277
+ await waitFor(() => {
278
+ expect(onRequestHideOptions).toHaveBeenCalledTimes(1);
279
+ });
280
+ });
281
+ });
282
+ });
283
+ describe('getRootProps()', () => {
284
+ it('should provide prop getter for root element', async () => {
285
+ const renderSpy = vi.fn();
286
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => {
287
+ renderSpy(selectable);
288
+ return null;
289
+ }));
290
+ const selectableProps = renderSpy.mock.calls[0][0];
291
+ expect(selectableProps.getRootProps).toBeDefined();
292
+ });
293
+ it('should allow custom props to pass through', async () => {
294
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", selectable.getRootProps({
295
+ 'data-custom-attr': true,
296
+ className: 'customClass',
297
+ style: {
298
+ color: 'red'
299
+ }
300
+ }), /*#__PURE__*/React.createElement("label", selectable.getLabelProps(), "Selectable"), /*#__PURE__*/React.createElement("input", Object.assign({
301
+ type: "text"
302
+ }, selectable.getTriggerProps(), selectable.getInputProps())))));
303
+ const root = screen.getByText('Selectable').closest('span');
304
+ expect(root).toHaveAttribute('data-custom-attr', 'true');
305
+ expect(root).toHaveClass('customClass');
306
+ expect(root).toHaveStyle('color: rgb(255, 0, 0)');
307
+ });
308
+ it('should allow supplemental onClick behavior', async () => {
309
+ const onClick = vi.fn();
310
+ const onRequestShowOptions = vi.fn();
311
+ render(/*#__PURE__*/React.createElement(Selectable, {
312
+ onRequestShowOptions: onRequestShowOptions
313
+ }, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
314
+ type: "text"
315
+ }, selectable.getTriggerProps({
316
+ onClick
317
+ }), selectable.getInputProps())))));
318
+ const input = screen.getByRole('combobox');
319
+ await userEvent.click(input);
320
+ await waitFor(() => {
321
+ expect(onClick).toHaveBeenCalledTimes(1);
322
+ expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
323
+ });
324
+ });
325
+ });
326
+ describe('getLabelProps()', () => {
327
+ it('should provide prop getter for label element', async () => {
328
+ const renderSpy = vi.fn();
329
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => {
330
+ renderSpy(selectable);
331
+ return null;
332
+ }));
333
+ expect(renderSpy.mock.calls[0].length > 0).toBeTruthy();
334
+ });
335
+ it('should set htmlFor prop', async () => {
336
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("label", selectable.getLabelProps(), "Selectable"), /*#__PURE__*/React.createElement("input", Object.assign({
337
+ type: "text"
338
+ }, selectable.getTriggerProps(), selectable.getInputProps())))));
339
+ const label = screen.getByText('Selectable');
340
+ const input = screen.getByRole('combobox');
341
+ expect(label).toHaveAttribute('for', input.getAttribute('id'));
342
+ });
343
+ it('should set htmlFor prop with custom defined id', async () => {
344
+ const customId = 'CustomSelect';
345
+ render(/*#__PURE__*/React.createElement(Selectable, {
346
+ id: customId
347
+ }, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("label", selectable.getLabelProps(), "Selectable"), /*#__PURE__*/React.createElement("input", Object.assign({
348
+ type: "text"
349
+ }, selectable.getTriggerProps(), selectable.getInputProps())))));
350
+ const label = screen.getByText('Selectable');
351
+ const input = screen.getByRole('combobox');
352
+ expect(input).toHaveAttribute('id', customId);
353
+ expect(label).toHaveAttribute('for', customId);
354
+ });
355
+ it('should allow custom props to pass through', async () => {
356
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("label", selectable.getLabelProps({
357
+ 'data-custom-attr': true,
358
+ className: 'customClass',
359
+ style: {
360
+ color: 'red'
361
+ }
362
+ }), "Selectable"), /*#__PURE__*/React.createElement("input", Object.assign({
363
+ type: "text"
364
+ }, selectable.getTriggerProps(), selectable.getInputProps())))));
365
+ const label = screen.getByText('Selectable');
366
+ expect(label).toHaveAttribute('data-custom-attr', 'true');
367
+ expect(label).toHaveClass('customClass');
368
+ expect(label).toHaveStyle({
369
+ color: 'rgb(255, 0, 0)'
370
+ });
371
+ });
372
+ });
373
+ describe('getTriggerProps()', () => {
374
+ it('should provide prop getter for trigger element', async () => {
375
+ const renderSpy = vi.fn();
376
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => {
377
+ renderSpy(selectable);
378
+ return null;
379
+ }));
380
+ expect(renderSpy.mock.calls[0][0].getTriggerProps).toBeDefined();
381
+ });
382
+ it('should set appropriate prop defaults', async () => {
383
+ const _render6 = render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
384
+ type: "text"
385
+ }, selectable.getTriggerProps())), /*#__PURE__*/React.createElement("span", selectable.getDescriptionProps(), "description")))),
386
+ container = _render6.container;
387
+ const input = container.querySelector('input');
388
+ const desc = screen.getByText('description');
389
+ expect(input).toHaveAttribute('aria-haspopup', 'listbox');
390
+ expect(input).toHaveAttribute('aria-describedby', desc.id);
391
+ expect(input).toHaveAttribute('id');
392
+ });
393
+ it('should set appropriate props based on isShowingOptions', async () => {
394
+ const _render7 = render(/*#__PURE__*/React.createElement(Selectable, {
395
+ isShowingOptions: false
396
+ }, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
397
+ type: "text"
398
+ }, selectable.getTriggerProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps())))),
399
+ container = _render7.container,
400
+ rerender = _render7.rerender;
401
+ const input = container.querySelector('input');
402
+ const list = screen.getByRole('listbox');
403
+ expect(input).toHaveAttribute('aria-expanded', 'false');
404
+ expect(input).not.toHaveAttribute('aria-controls');
405
+
406
+ // Set prop: isShowingOptions true
407
+ rerender(/*#__PURE__*/React.createElement(Selectable, {
408
+ isShowingOptions: true
409
+ }, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
410
+ type: "text"
411
+ }, selectable.getTriggerProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps()))));
412
+ expect(input).toHaveAttribute('aria-expanded', 'true');
413
+ expect(input).toHaveAttribute('aria-controls', list.id);
414
+ });
415
+ it('should set appropriate props based on highlightedOptionId', async () => {
416
+ const _render8 = render(/*#__PURE__*/React.createElement(Selectable, {
417
+ isShowingOptions: true
418
+ }, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
419
+ type: "text"
420
+ }, selectable.getTriggerProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
421
+ key: opt
422
+ }, selectable.getOptionProps({
423
+ id: opt
424
+ })))))))),
425
+ container = _render8.container,
426
+ rerender = _render8.rerender;
427
+ const input = container.querySelector('input');
428
+ const options = screen.getAllByRole('option');
429
+ expect(input).not.toHaveAttribute('aria-activedescendant');
430
+
431
+ // Set prop: highlightedOptionId
432
+ rerender(/*#__PURE__*/React.createElement(Selectable, {
433
+ isShowingOptions: true,
434
+ highlightedOptionId: defaultOptions[0]
435
+ }, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
436
+ type: "text"
437
+ }, selectable.getTriggerProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
438
+ key: opt
439
+ }, selectable.getOptionProps({
440
+ id: opt
441
+ }))))))));
442
+ expect(input).toHaveAttribute('aria-activedescendant', options[0].id);
443
+
444
+ // Set prop: highlightedOptionId
445
+ rerender(/*#__PURE__*/React.createElement(Selectable, {
446
+ isShowingOptions: true,
447
+ highlightedOptionId: defaultOptions[1]
448
+ }, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
449
+ type: "text"
450
+ }, selectable.getTriggerProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
451
+ key: opt
452
+ }, selectable.getOptionProps({
453
+ id: opt
454
+ }))))))));
455
+ expect(input).toHaveAttribute('aria-activedescendant', options[1].id);
456
+
457
+ // Set prop: isShowingOptions
458
+ rerender(/*#__PURE__*/React.createElement(Selectable, {
459
+ isShowingOptions: false,
460
+ highlightedOptionId: defaultOptions[1]
461
+ }, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
462
+ type: "text"
463
+ }, selectable.getTriggerProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
464
+ key: opt
465
+ }, selectable.getOptionProps({
466
+ id: opt
467
+ }))))))));
468
+ expect(input).not.toHaveAttribute('aria-activedescendant');
469
+ });
470
+ it('should allow custom props to pass through', async () => {
471
+ const _render9 = render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
472
+ type: "text"
473
+ }, selectable.getTriggerProps({
474
+ 'data-custom-attr': true,
475
+ placeholder: 'Type to enter text',
476
+ className: 'customClass',
477
+ style: {
478
+ color: 'red'
479
+ }
480
+ })))))),
481
+ container = _render9.container;
482
+ const input = container.querySelector('input');
483
+ expect(input).toHaveAttribute('data-custom-attr', 'true');
484
+ expect(input).toHaveAttribute('style', 'color: red;');
485
+ expect(input).toHaveAttribute('placeholder');
486
+ expect(input).toHaveClass('customClass');
487
+ });
488
+ it('should allow props to be overridden', async () => {
489
+ const _render10 = render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
490
+ type: "text"
491
+ }, selectable.getTriggerProps({
492
+ 'aria-haspopup': 'menu',
493
+ 'aria-controls': 'customId'
494
+ })))))),
495
+ container = _render10.container;
496
+ const input = container.querySelector('input');
497
+ expect(input).toHaveAttribute('aria-haspopup', 'menu');
498
+ expect(input).toHaveAttribute('aria-controls', 'customId');
499
+ });
500
+ it('should provide a ref to the text input', async () => {
501
+ const inputRef = vi.fn();
502
+ const _render11 = render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
503
+ type: "text"
504
+ }, selectable.getTriggerProps({
505
+ ref: inputRef
506
+ })))))),
507
+ container = _render11.container;
508
+ const input = container.querySelector('input');
509
+ expect(inputRef).toHaveBeenCalledWith(input);
510
+ });
511
+ it('should allow role and aria-autocomplete props to be overridden', async () => {
512
+ const _render12 = render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
513
+ type: "text"
514
+ }, selectable.getTriggerProps({
515
+ role: 'textbox',
516
+ 'aria-autocomplete': 'list'
517
+ })))))),
518
+ container = _render12.container;
519
+ const input = container.querySelector('input');
520
+ expect(input).toHaveAttribute('role', 'textbox');
521
+ expect(input).toHaveAttribute('aria-autocomplete', 'list');
522
+ });
523
+ it('should allow supplemental onClick behavior', async () => {
524
+ const onClick = vi.fn();
525
+ const onRequestShowOptions = vi.fn();
526
+ render(/*#__PURE__*/React.createElement(Selectable, {
527
+ onRequestShowOptions: onRequestShowOptions
528
+ }, selectable => /*#__PURE__*/React.createElement("span", selectable.getRootProps(), /*#__PURE__*/React.createElement("input", Object.assign({
529
+ type: "text"
530
+ }, selectable.getTriggerProps({
531
+ onClick
532
+ }), selectable.getInputProps())))));
533
+ const input = screen.getByRole('combobox');
534
+ await input.click();
535
+ expect(onClick).toHaveBeenCalledTimes(1);
536
+ expect(onRequestShowOptions).toHaveBeenCalledTimes(1);
537
+ });
538
+ });
539
+ describe('getInputProps()', () => {
540
+ it('should provide prop getter for trigger element', async () => {
541
+ const renderSpy = vi.fn();
542
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => {
543
+ renderSpy(selectable);
544
+ return null;
545
+ }));
546
+ const args = renderSpy.mock.calls[0][0];
547
+ expect(args.getInputProps).toBeDefined();
548
+ });
549
+ it('should set appropriate prop defaults', async () => {
550
+ const _render13 = render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
551
+ type: "text"
552
+ }, selectable.getInputProps()))))),
553
+ container = _render13.container;
554
+ const input = container.querySelector('input');
555
+ expect(input).toHaveAttribute('role', 'combobox');
556
+ expect(input).toHaveAttribute('aria-autocomplete', 'both');
557
+ expect(input).toHaveAttribute('autocomplete', 'off');
558
+ });
559
+ it('should set appropriate props when readOnly', async () => {
560
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
561
+ type: "text"
562
+ }, selectable.getInputProps({
563
+ readOnly: true
564
+ }))))));
565
+ const input = screen.getByRole('combobox');
566
+ expect(input).toHaveAttribute('aria-autocomplete', 'none');
567
+ expect(input).toHaveAttribute('readOnly');
568
+ expect(input).not.toHaveAttribute('disabled');
569
+ });
570
+ it('should allow props to be overridden', async () => {
571
+ const _render14 = render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
572
+ type: "text"
573
+ }, selectable.getInputProps({
574
+ role: 'textbox',
575
+ 'aria-autocomplete': 'inline'
576
+ })))))),
577
+ container = _render14.container;
578
+ const input = container.querySelector('input');
579
+ expect(input).toHaveAttribute('role', 'textbox');
580
+ expect(input).toHaveAttribute('aria-autocomplete', 'inline');
581
+ });
582
+ it('should allow custom props to pass through', async () => {
583
+ const _render15 = render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
584
+ type: "text"
585
+ }, selectable.getInputProps({
586
+ 'data-custom-attr': true,
587
+ placeholder: 'Type to enter text',
588
+ className: 'customClass',
589
+ style: {
590
+ color: 'red'
591
+ }
592
+ })))))),
593
+ container = _render15.container;
594
+ const input = container.querySelector('input');
595
+ expect(input).toHaveAttribute('data-custom-attr', 'true');
596
+ expect(input).toHaveAttribute('placeholder');
597
+ expect(input).toHaveClass('customClass');
598
+ expect(input).toHaveAttribute('style', 'color: red;');
599
+ });
600
+ });
601
+ describe('getListProps()', () => {
602
+ it('should provide prop getter for list element', async () => {
603
+ const renderSpy = vi.fn();
604
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => {
605
+ renderSpy(selectable);
606
+ return null;
607
+ }));
608
+ const args = renderSpy.mock.calls[0][0];
609
+ expect(args.getListProps).toBeDefined();
610
+ });
611
+ it('should set appropriate prop defaults', async () => {
612
+ const _render16 = render(/*#__PURE__*/React.createElement(Selectable, {
613
+ isShowingOptions: true
614
+ }, selectable => /*#__PURE__*/React.createElement("span", selectable.getRootProps(), /*#__PURE__*/React.createElement("input", Object.assign({
615
+ type: "text"
616
+ }, selectable.getTriggerProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps())))),
617
+ container = _render16.container;
618
+ const list = container.querySelector('ul');
619
+ expect(list).toHaveAttribute('id');
620
+ expect(list).toHaveAttribute('role', 'listbox');
621
+ });
622
+ it('should allow custom props to pass through', async () => {
623
+ const _render17 = render(/*#__PURE__*/React.createElement(Selectable, {
624
+ isShowingOptions: true
625
+ }, selectable => /*#__PURE__*/React.createElement("span", selectable.getRootProps(), /*#__PURE__*/React.createElement("input", Object.assign({
626
+ type: "text"
627
+ }, selectable.getTriggerProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps({
628
+ 'data-custom-attr': true,
629
+ className: 'customClass',
630
+ style: {
631
+ color: 'red'
632
+ }
633
+ }))))),
634
+ container = _render17.container;
635
+ const list = container.querySelector('ul');
636
+ expect(list).toHaveAttribute('data-custom-attr', 'true');
637
+ expect(list).toHaveClass('customClass');
638
+ expect(list).toHaveAttribute('style', 'color: red;');
639
+ });
640
+ });
641
+ describe('getOptionProps()', () => {
642
+ it('should provide prop getter for option element', async () => {
643
+ const renderSpy = vi.fn();
644
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => {
645
+ renderSpy(selectable);
646
+ return null;
647
+ }));
648
+ const args = renderSpy.mock.calls[0][0];
649
+ expect(args.getOptionProps).toBeDefined();
650
+ });
651
+ it('should set appropriate prop defaults', async () => {
652
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
653
+ type: "text"
654
+ }, selectable.getTriggerProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
655
+ key: opt
656
+ }, selectable.getOptionProps({
657
+ id: opt
658
+ })), opt))))));
659
+ const option = screen.getByText(defaultOptions[0]);
660
+ expect(option.tagName).toBe('LI');
661
+ expect(option).toHaveAttribute('role', 'option');
662
+ expect(option).toHaveAttribute('aria-selected', 'false');
663
+ });
664
+ it('should set aria-selected based on selectedOptionId', async () => {
665
+ const _render18 = render(/*#__PURE__*/React.createElement(Selectable, {
666
+ selectedOptionId: defaultOptions[1]
667
+ }, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
668
+ type: "text"
669
+ }, selectable.getTriggerProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
670
+ key: opt
671
+ }, selectable.getOptionProps({
672
+ id: opt
673
+ })), opt)))))),
674
+ container = _render18.container,
675
+ rerender = _render18.rerender;
676
+ const options = container.querySelectorAll('li');
677
+ expect(options[0]).toHaveAttribute('aria-selected', 'false');
678
+ expect(options[1]).toHaveAttribute('aria-selected', 'true');
679
+ expect(options[2]).toHaveAttribute('aria-selected', 'false');
680
+
681
+ // Set prop: selectedOptionId
682
+ rerender(/*#__PURE__*/React.createElement(Selectable, {
683
+ selectedOptionId: [defaultOptions[0], defaultOptions[1]]
684
+ }, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
685
+ type: "text"
686
+ }, selectable.getTriggerProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
687
+ key: opt
688
+ }, selectable.getOptionProps({
689
+ id: opt
690
+ })), opt))))));
691
+ expect(options[0]).toHaveAttribute('aria-selected', 'true');
692
+ expect(options[1]).toHaveAttribute('aria-selected', 'true');
693
+ expect(options[2]).toHaveAttribute('aria-selected', 'false');
694
+ });
695
+ it('should allow custom props to pass through', async () => {
696
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
697
+ type: "text"
698
+ }, selectable.getTriggerProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
699
+ key: opt
700
+ }, selectable.getOptionProps({
701
+ id: opt,
702
+ 'data-custom-attr': true,
703
+ className: 'customClass',
704
+ style: {
705
+ color: 'red'
706
+ }
707
+ })), opt))))));
708
+ const option = screen.getByText(defaultOptions[0]);
709
+ expect(option.tagName).toBe('LI');
710
+ expect(option).toHaveAttribute('data-custom-attr', 'true');
711
+ expect(option).toHaveClass('customClass');
712
+ expect(option).toHaveAttribute('style', 'color: red;');
713
+ });
714
+ it('should allow supplemental onClick behavior', async () => {
715
+ const onClick = vi.fn();
716
+ const onRequestSelectOption = vi.fn();
717
+ render(/*#__PURE__*/React.createElement(Selectable, {
718
+ isShowingOptions: true,
719
+ onRequestSelectOption: onRequestSelectOption
720
+ }, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
721
+ type: "text"
722
+ }, selectable.getInputProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
723
+ key: opt
724
+ }, selectable.getOptionProps({
725
+ id: opt,
726
+ onClick
727
+ })), opt))))));
728
+ const option_0 = screen.getByText(defaultOptions[0]);
729
+ const option_1 = screen.getByText(defaultOptions[1]);
730
+ userEvent.click(option_0);
731
+ userEvent.click(option_1);
732
+ await waitFor(() => {
733
+ expect(onRequestSelectOption).toHaveBeenCalledTimes(2);
734
+ expect(onClick).toHaveBeenCalledTimes(2);
735
+ });
736
+ });
737
+ });
738
+ describe('getDisabledOptionProps()', () => {
739
+ it('should provide prop getter for disabled option element', async () => {
740
+ const renderSpy = vi.fn();
741
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => {
742
+ renderSpy(selectable);
743
+ return null;
744
+ }));
745
+ const args = renderSpy.mock.calls[0][0];
746
+ expect(args.getDisabledOptionProps).toBeDefined();
747
+ });
748
+ it('should set aria-disabled prop', async () => {
749
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", Object.assign({
750
+ type: "text"
751
+ }, selectable.getTriggerProps(), selectable.getInputProps())), /*#__PURE__*/React.createElement("ul", selectable.getListProps(), defaultOptions.map(opt => /*#__PURE__*/React.createElement("li", Object.assign({
752
+ key: opt
753
+ }, selectable.getOptionProps({
754
+ id: opt
755
+ }), selectable.getDisabledOptionProps()), opt))))));
756
+ const option = screen.getByText(defaultOptions[0]);
757
+ expect(option.tagName).toBe('LI');
758
+ expect(option).toHaveAttribute('aria-disabled', 'true');
759
+ });
760
+ });
761
+ describe('getDescriptionProps()', () => {
762
+ it('should provide prop getter for description element', async () => {
763
+ const renderSpy = vi.fn();
764
+ render(/*#__PURE__*/React.createElement(Selectable, null, selectable => {
765
+ renderSpy(selectable);
766
+ return null;
767
+ }));
768
+ const selectableProps = renderSpy.mock.calls[0][0];
769
+ expect(selectableProps.getDescriptionProps).toBeDefined();
770
+ });
771
+ });
772
+ });