-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathuseComboBoxState.test.js
More file actions
388 lines (325 loc) · 16.9 KB
/
useComboBoxState.test.js
File metadata and controls
388 lines (325 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {actHook as act, renderHook} from '@react-spectrum/test-utils-internal';
import {Item} from '../../src/collections/Item';
import React from 'react';
import {useComboBoxState} from '../../src/combobox/useComboBoxState';
import {useFilter} from 'react-aria/useFilter';
describe('useComboBoxState tests', function () {
describe('open state', function () {
let onOpenChange;
let defaultProps;
beforeEach(() => {
onOpenChange = jest.fn();
let collator = {compare: jest.fn().mockReturnValue(true)};
defaultProps = {isFocused: true, items: [{id: 1, name: 'one'}], children: (props) => <Item>{props.name}</Item>, onOpenChange, collator};
});
it('should be closed by default', function () {
let initialProps = defaultProps;
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
expect(result.current.isOpen).toBe(false);
act(() => {
result.current.open();
});
expect(result.current.isOpen).toBe(true);
expect(onOpenChange).toHaveBeenCalledWith(true, undefined);
act(() => result.current.close());
expect(result.current.isOpen).toBe(false);
expect(onOpenChange).toHaveBeenCalledWith(false, undefined);
});
it('onOpenChange should return the reason that open was called', function () {
let initialProps = defaultProps;
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
act(() => {
result.current.open(undefined, 'focus');
});
expect(result.current.isOpen).toBe(true);
expect(onOpenChange).toHaveBeenCalledWith(true, 'focus');
act(() => result.current.close());
expect(result.current.isOpen).toBe(false);
expect(onOpenChange).toHaveBeenCalledWith(false, undefined);
act(() => {
result.current.open(undefined, 'input');
});
expect(result.current.isOpen).toBe(true);
expect(onOpenChange).toHaveBeenCalledWith(true, 'input');
act(() => result.current.close());
act(() => {
result.current.open(undefined, 'manual');
});
expect(result.current.isOpen).toBe(true);
expect(onOpenChange).toHaveBeenCalledWith(true, 'manual');
});
it('onOpenChange should return the reason that toggle was called when opening', function () {
let initialProps = defaultProps;
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
act(() => {
result.current.toggle(undefined, 'focus');
});
expect(result.current.isOpen).toBe(true);
expect(onOpenChange).toHaveBeenCalledWith(true, 'focus');
act(() => result.current.toggle(undefined, 'focus'));
expect(result.current.isOpen).toBe(false);
expect(onOpenChange).toHaveBeenCalledWith(false, undefined);
act(() => {
result.current.toggle(undefined, 'input');
});
expect(result.current.isOpen).toBe(true);
expect(onOpenChange).toHaveBeenCalledWith(true, 'input');
act(() => result.current.close());
act(() => {
result.current.toggle(undefined, 'manual');
});
expect(result.current.isOpen).toBe(true);
expect(onOpenChange).toHaveBeenCalledWith(true, 'manual');
});
});
describe('values', function () {
let onInputChange;
let defaultProps;
beforeEach(() => {
onInputChange = jest.fn();
let collator = {compare: jest.fn().mockReturnValue(true)};
defaultProps = {items: [{id: 1, name: 'one'}], children: (props) => <Item>{props.name}</Item>, onInputChange, collator};
});
it('can have a default value', function () {
let initialProps = {...defaultProps, defaultInputValue: 'hello'};
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
expect(result.current.inputValue).toBe('hello');
});
it('fires an event when the value is changed and updates if uncontrolled', function () {
let initialProps = {...defaultProps, defaultInputValue: 'hello'};
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
expect(result.current.inputValue).toBe('hello');
expect(onInputChange).not.toHaveBeenCalled();
act(() => result.current.setInputValue('hellow'));
expect(result.current.inputValue).toBe('hellow');
expect(onInputChange).toHaveBeenCalledWith('hellow');
});
it('does not change selection on close', () => {
let initialProps = defaultProps;
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
act(() => {
result.current.open();
});
act(() => {
result.current.selectionManager.setFocusedKey(1);
});
act(() => {
result.current.close();
});
expect(result.current.selectedKey).not.toBe(1);
});
it('starts blank if no (default) value', function () {
let initialProps = {...defaultProps};
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
expect(result.current.inputValue).toBe('');
expect(onInputChange).not.toHaveBeenCalled();
act(() => result.current.setInputValue('h'));
expect(result.current.inputValue).toBe('h');
expect(onInputChange).toHaveBeenCalledWith('h');
});
it('can be controlled', function () {
let initialProps = {...defaultProps, inputValue: 'hello'};
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
expect(result.current.inputValue).toBe('hello');
expect(onInputChange).not.toHaveBeenCalled();
act(() => result.current.setInputValue('hellow'));
expect(result.current.inputValue).toBe('hello');
expect(onInputChange).toHaveBeenCalledWith('hellow');
});
});
describe('collection', function () {
let onSelectionChange;
let defaultProps;
beforeEach(() => {
onSelectionChange = jest.fn();
let collator = {compare: jest.fn().mockReturnValue(true)};
defaultProps = {items: [{id: '0', name: 'one'}, {id: '1', name: 'onomatopoeia'}], children: (props) => <Item {...props}>{props.name}</Item>, onSelectionChange, collator};
});
it('support selectedKey', function () {
let initialProps = {...defaultProps, selectedKey: '0'};
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
expect(result.current.selectionManager.selectionMode).toBe('single');
expect(result.current.selectionManager.selectedKeys).toContain('0');
expect(result.current.selectionManager.selectedKeys).not.toContain('1');
act(() => result.current.selectionManager.replaceSelection('1'));
expect(result.current.selectionManager.selectedKeys).toContain('0');
expect(result.current.selectionManager.selectedKeys).not.toContain('1');
expect(onSelectionChange).toHaveBeenCalledWith('1');
});
it('support defaultSelectedKey', function () {
let initialProps = {...defaultProps, defaultSelectedKey: '0'};
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
expect(result.current.selectionManager.selectionMode).toBe('single');
expect(result.current.selectionManager.selectedKeys).toContain('0');
expect(result.current.selectionManager.selectedKeys).not.toContain('1');
act(() => result.current.selectionManager.replaceSelection('1'));
expect(result.current.selectionManager.selectedKeys).toContain('1');
expect(result.current.selectionManager.selectedKeys).not.toContain('0');
expect(onSelectionChange).toHaveBeenCalledWith('1');
});
it('supports default no selection', function () {
let initialProps = {...defaultProps};
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
expect(result.current.selectionManager.selectionMode).toBe('single');
expect(result.current.selectionManager.selectedKeys.size).toBe(0);
act(() => result.current.selectionManager.replaceSelection('1'));
expect(result.current.selectionManager.selectedKeys).toContain('1');
expect(result.current.selectionManager.selectedKeys).not.toContain('0');
expect(onSelectionChange).toHaveBeenCalledWith('1');
});
it('does not fire onSelectionChange on blur when fully controlled values are already synced', function () {
let initialProps = {...defaultProps, selectedKey: '1', inputValue: 'onomatopoeia'};
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
act(() => {result.current.setFocused(false);});
expect(onSelectionChange).not.toHaveBeenCalled();
});
it('fires onSelectionChange on blur when fully controlled inputValue is out of sync', function () {
let initialProps = {...defaultProps, selectedKey: '1', inputValue: 'onom'};
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
act(() => {result.current.setFocused(false);});
expect(onSelectionChange).toHaveBeenCalledTimes(1);
expect(onSelectionChange).toHaveBeenCalledWith('1');
});
it('won\'t update the returned collection if the combobox is closed (uncontrolled items)', function () {
let filter = renderHook((props) => useFilter(props), {sensitivity: 'base'});
let initialProps = {...defaultProps, items: null, defaultItems: [{id: '0', name: 'one'}, {id: '1', name: 'onomatopoeia'}], defaultInputValue: '', defaultFilter: filter.result.current.contains};
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
expect(result.current.collection.size).toEqual(2);
expect(result.current.inputValue).toBe('');
act(() => {result.current.open();});
act(() => result.current.setInputValue('onom'));
expect(result.current.inputValue).toBe('onom');
expect(result.current.collection.size).toEqual(1);
expect(result.current.collection.getItem('1').rendered).toBe('onomatopoeia');
// The input value updates, but the returned collection still only contains onomatopoeia
act(() => {result.current.setFocused(false);});
expect(result.current.collection.size).toEqual(1);
expect(result.current.inputValue).toBe('');
expect(result.current.collection.getItem('1').rendered).toBe('onomatopoeia');
// Subsequent calls that would close the menu don't update the tracked lastCollection
act(() => {result.current.commit();});
expect(result.current.collection.size).toEqual(1);
expect(result.current.inputValue).toBe('');
expect(result.current.collection.getItem('1').rendered).toBe('onomatopoeia');
act(() => {result.current.close();});
expect(result.current.collection.size).toEqual(1);
expect(result.current.inputValue).toBe('');
expect(result.current.collection.getItem('1').rendered).toBe('onomatopoeia');
act(() => {result.current.revert();});
expect(result.current.collection.size).toEqual(1);
expect(result.current.inputValue).toBe('');
expect(result.current.collection.getItem('1').rendered).toBe('onomatopoeia');
act(() => {result.current.open();});
expect(result.current.collection.size).toEqual(2);
});
it('won\'t update the returned collection if the combobox is closed (controlled items)', function () {
let initialProps = {...defaultProps};
let {result, rerender} = renderHook((props) => useComboBoxState(props), {initialProps});
expect(result.current.collection.size).toEqual(2);
act(() => {result.current.open();});
rerender({...initialProps, items: [{id: '1', name: 'onomatopoeia'}]});
// Returned collection reflects the items provided by the user since the combobox is open
expect(result.current.collection.size).toEqual(1);
expect(result.current.collection.getItem('1').rendered).toBe('onomatopoeia');
act(() => {result.current.setFocused(false);});
// Returned collection reflects the old items provided by the user when the combobox is closed
expect(result.current.collection.size).toEqual(1);
expect(result.current.collection.getItem('1').rendered).toBe('onomatopoeia');
rerender(initialProps);
expect(result.current.collection.size).toEqual(1);
expect(result.current.collection.getItem('1').rendered).toBe('onomatopoeia');
// Subsequent calls that would close the menu don't update the tracked lastCollection
act(() => {result.current.commit();});
expect(result.current.collection.size).toEqual(1);
expect(result.current.inputValue).toBe('');
expect(result.current.collection.getItem('1').rendered).toBe('onomatopoeia');
act(() => {result.current.close();});
expect(result.current.collection.size).toEqual(1);
expect(result.current.inputValue).toBe('');
expect(result.current.collection.getItem('1').rendered).toBe('onomatopoeia');
act(() => {result.current.revert();});
expect(result.current.collection.size).toEqual(1);
expect(result.current.inputValue).toBe('');
expect(result.current.collection.getItem('1').rendered).toBe('onomatopoeia');
// When the combobox is opened again, the returned collection of items updates to reflect the items provided by the user
act(() => {result.current.open();});
expect(result.current.collection.size).toEqual(2);
});
});
describe('controlled items (async loading)', function () {
it('should re-open the menu when controlled items go from empty to non-empty', function () {
let onOpenChange = jest.fn();
let initialProps = {
items: [{id: 1, name: 'Luke Skywalker'}],
children: (props) => <Item>{props.name}</Item>,
onOpenChange
};
let {result, rerender} = renderHook((props) => useComboBoxState(props), {initialProps});
// Focus and open the menu by setting input value
act(() => {result.current.setFocused(true);});
act(() => {result.current.open(null, 'input');});
expect(result.current.isOpen).toBe(true);
// Simulate async load returning empty results (e.g. user typed "luka")
rerender({...initialProps, items: []});
// Menu closes on empty collection
expect(result.current.isOpen).toBe(false);
// Simulate async load returning results again (e.g. user backspaced to "luk")
rerender({...initialProps, items: [{id: 1, name: 'Luke Skywalker'}]});
// Menu should re-open because items were controlled and the close was due to empty collection
expect(result.current.isOpen).toBe(true);
expect(result.current.collection.size).toEqual(1);
expect(onOpenChange).toHaveBeenLastCalledWith(true, 'input');
});
it('should not re-open after user dismisses with Escape (revert)', function () {
let onOpenChange = jest.fn();
let initialProps = {
items: [{id: 1, name: 'Luke Skywalker'}],
children: (props) => <Item>{props.name}</Item>,
onOpenChange
};
let {result, rerender} = renderHook((props) => useComboBoxState(props), {initialProps});
// Focus and open
act(() => {result.current.setFocused(true);});
act(() => {result.current.open(null, 'input');});
expect(result.current.isOpen).toBe(true);
// Async returns empty, menu auto-closes
rerender({...initialProps, items: []});
expect(result.current.isOpen).toBe(false);
// User presses Escape (revert) while menu is closed
act(() => {result.current.revert();});
// Async returns items — menu should NOT re-open because user explicitly dismissed
rerender({...initialProps, items: [{id: 1, name: 'Luke Skywalker'}]});
expect(result.current.isOpen).toBe(false);
});
it('should still close the menu when uncontrolled items are empty', function () {
let onOpenChange = jest.fn();
let contains = (a, b) => a.toLowerCase().includes(b.toLowerCase());
let initialProps = {
defaultItems: [{id: 1, name: 'Luke Skywalker'}],
children: (props) => <Item>{props.name}</Item>,
onOpenChange,
defaultFilter: contains
};
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
// Focus and open
act(() => {result.current.setFocused(true);});
act(() => {result.current.open(null, 'input');});
expect(result.current.isOpen).toBe(true);
// Type something that filters to zero results
act(() => {result.current.setInputValue('zzz');});
// Menu should close because items are uncontrolled and filtered to empty
expect(result.current.isOpen).toBe(false);
});
});
});