-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
Expand file tree
/
Copy pathuseAutocomplete.test.js
More file actions
378 lines (333 loc) · 10.5 KB
/
useAutocomplete.test.js
File metadata and controls
378 lines (333 loc) · 10.5 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
import * as React from 'react';
import { expect } from 'chai';
import { createRenderer, screen, ErrorBoundary, fireEvent } from '@mui/internal-test-utils';
import { spy } from 'sinon';
import { useAutocomplete, createFilterOptions } from '@mui/base/useAutocomplete';
describe('useAutocomplete', () => {
const { render } = createRenderer();
it('should preserve DOM nodes of options when re-ordering', () => {
function Test(props) {
const { options } = props;
const {
groupedOptions,
getRootProps,
getInputLabelProps,
getInputProps,
getListboxProps,
getOptionProps,
} = useAutocomplete({
options,
open: true,
});
return (
<div>
<div {...getRootProps()}>
<label {...getInputLabelProps()}>useAutocomplete</label>
<input {...getInputProps()} />
</div>
{groupedOptions.length > 0 ? (
<ul {...getListboxProps()}>
{groupedOptions.map((option, index) => {
const { key, ...optionProps } = getOptionProps({ option, index });
return (
<li key={key} {...optionProps}>
{option}
</li>
);
})}
</ul>
) : null}
</div>
);
}
const { rerender } = render(<Test options={['foo', 'bar']} />);
const [fooOptionAsFirst, barOptionAsSecond] = screen.getAllByRole('option');
rerender(<Test options={['bar', 'foo']} />);
const [barOptionAsFirst, fooOptionAsSecond] = screen.getAllByRole('option');
// If the DOM nodes are not preserved VO will not read the first option again since it thinks it didn't change.
expect(fooOptionAsFirst).to.equal(fooOptionAsSecond);
expect(barOptionAsFirst).to.equal(barOptionAsSecond);
});
describe('createFilterOptions', () => {
it('defaults to getOptionLabel for text filtering', () => {
const filterOptions = createFilterOptions();
const getOptionLabel = (option) => option.name;
const options = [
{
id: '1234',
name: 'cat',
},
{
id: '5678',
name: 'dog',
},
{
id: '9abc',
name: 'emu',
},
];
expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal([
options[0],
]);
});
it('filters without error with empty option set', () => {
const filterOptions = createFilterOptions();
const getOptionLabel = (option) => option.name;
const options = [];
expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal([]);
});
describe('option: limit', () => {
it('limits the number of suggested options to be shown', () => {
const filterOptions = createFilterOptions({ limit: 2 });
const getOptionLabel = (option) => option.name;
const options = [
{
id: '1234',
name: 'a1',
},
{
id: '5678',
name: 'a2',
},
{
id: '9abc',
name: 'a3',
},
{
id: '9abc',
name: 'a4',
},
];
expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal([
options[0],
options[1],
]);
});
});
describe('option: matchFrom', () => {
let filterOptions;
let getOptionLabel;
let options;
beforeEach(() => {
filterOptions = createFilterOptions({ matchFrom: 'any' });
getOptionLabel = (option) => option.name;
options = [
{
id: '1234',
name: 'ab',
},
{
id: '5678',
name: 'ba',
},
{
id: '9abc',
name: 'ca',
},
];
});
describe('any', () => {
it('show all results that match', () => {
expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal(
options,
);
});
});
describe('empty', () => {
it('does not call getOptionLabel if filter is empty', () => {
const getOptionLabelSpy = spy(getOptionLabel);
expect(
filterOptions(options, { inputValue: '', getOptionLabel: getOptionLabelSpy }),
).to.deep.equal(options);
expect(getOptionLabelSpy.callCount).to.equal(0);
});
});
describe('start', () => {
it('show only results that start with search', () => {
expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal(
options,
);
});
});
});
describe('option: ignoreAccents', () => {
it('does not ignore accents', () => {
const filterOptions = createFilterOptions({ ignoreAccents: false });
const getOptionLabel = (option) => option.name;
const options = [
{
id: '1234',
name: 'áb',
},
{
id: '5678',
name: 'ab',
},
{
id: '9abc',
name: 'áe',
},
{
id: '9abc',
name: 'ae',
},
];
expect(filterOptions(options, { inputValue: 'á', getOptionLabel })).to.deep.equal([
options[0],
options[2],
]);
});
});
describe('option: ignoreCase', () => {
it('matches results with case insensitive', () => {
const filterOptions = createFilterOptions({ ignoreCase: false });
const getOptionLabel = (option) => option.name;
const options = [
{
id: '1234',
name: 'Ab',
},
{
id: '5678',
name: 'ab',
},
{
id: '9abc',
name: 'Ae',
},
{
id: '9abc',
name: 'ae',
},
];
expect(filterOptions(options, { inputValue: 'A', getOptionLabel })).to.deep.equal([
options[0],
options[2],
]);
});
});
});
it('should warn if the input is not binded', function test() {
// TODO is this fixed?
if (!/jsdom/.test(window.navigator.userAgent)) {
// can't catch render errors in the browser for unknown reason
// tried try-catch + error boundary + window onError preventDefault
this.skip();
}
function Test(props) {
const { options } = props;
const {
groupedOptions,
getRootProps,
getInputLabelProps,
// getInputProps,
getListboxProps,
getOptionProps,
} = useAutocomplete({
options,
open: true,
});
return (
<div>
<div {...getRootProps()}>
<label {...getInputLabelProps()}>useAutocomplete</label>
</div>
{groupedOptions.length > 0 ? (
<ul {...getListboxProps()}>
{groupedOptions.map((option, index) => {
const { key, ...optionProps } = getOptionProps({ option, index });
return (
<li key={key} {...optionProps}>
{option}
</li>
);
})}
</ul>
) : null}
</div>
);
}
const node16ErrorMessage =
"Error: Uncaught [TypeError: Cannot read properties of null (reading 'removeAttribute')]";
const olderNodeErrorMessage =
"Error: Uncaught [TypeError: Cannot read property 'removeAttribute' of null]";
const nodeVersion = Number(process.versions.node.split('.')[0]);
const errorMessage = nodeVersion >= 16 ? node16ErrorMessage : olderNodeErrorMessage;
const devErrorMessages = [
errorMessage,
'MUI: Unable to find the input element.',
errorMessage,
// strict effects runs effects twice
React.version.startsWith('18') && 'MUI: Unable to find the input element.',
React.version.startsWith('18') && errorMessage,
'The above error occurred in the <ul> component',
React.version.startsWith('16') && 'The above error occurred in the <ul> component',
'The above error occurred in the <Test> component',
// strict effects runs effects twice
React.version.startsWith('18') && 'The above error occurred in the <Test> component',
React.version.startsWith('16') && 'The above error occurred in the <Test> component',
];
expect(() => {
render(
<ErrorBoundary>
<Test options={['foo', 'bar']} />
</ErrorBoundary>,
);
}).toErrorDev(devErrorMessages);
});
describe('prop: freeSolo', () => {
it('should not reset if the component value does not change on blur', () => {
function Test(props) {
const { options } = props;
const { getInputProps } = useAutocomplete({ options, open: true, freeSolo: true });
return <input {...getInputProps()} />;
}
render(<Test options={['foo', 'bar']} />);
const input = screen.getByRole('combobox');
fireEvent.change(input, { target: { value: 'free' } });
fireEvent.blur(input);
expect(input.value).to.equal('free');
});
});
describe('getInputProps', () => {
it('should disable input element', () => {
function Test(props) {
const { options } = props;
const { getInputProps } = useAutocomplete({ options, disabled: true });
return <input {...getInputProps()} />;
}
render(<Test options={['foo', 'bar']} />);
const input = screen.getByRole('combobox');
expect(input).to.have.attribute('disabled');
});
});
it('should allow tuples or arrays as value when multiple=false', () => {
function Test() {
const defaultValue = ['bar'];
const { getClearProps, getInputProps } = useAutocomplete({
defaultValue,
disableClearable: false,
getOptionLabel: ([val]) => val,
isOptionEqualToValue: (option, value) => {
if (option === value) {
return true;
}
return option[0] === value[0];
},
multiple: false,
options: [['foo'], defaultValue, ['baz']],
});
return (
<div>
<input {...getInputProps()} />
<button data-testid="button" {...getClearProps()} />;
</div>
);
}
const { getByTestId } = render(<Test />);
const button = getByTestId('button');
expect(() => {
fireEvent.click(button);
}).not.to.throw();
});
});