-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathuseCascadeSearch.test.js
47 lines (46 loc) · 1.26 KB
/
useCascadeSearch.test.js
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
import { renderHook } from '@testing-library/react-hooks';
import { useCascadeSearch } from '../src/useCascadeSearch';
test('useCascadeSearch', async () => {
const config = {
list: [
() => {
return [
{
label: '1',
value: '1',
},
];
},
async (lastValue, value) => {
await new Promise(r => setTimeout(r, 200));
return value === '1'
? [
{
label: '2',
value: '2',
},
]
: [];
},
],
autoFirstSearch: false,
};
const { result, waitForNextUpdate } = renderHook(() =>
useCascadeSearch(config),
);
result.current.search(0);
expect(result.current.loadingList).toEqual([true, false]);
await waitForNextUpdate();
expect(result.current.loadingList).toEqual([false, false]);
expect(result.current.responseDataList).toEqual([
[{ label: '1', value: '1' }],
]);
result.current.search(1, '1');
expect(result.current.loadingList).toEqual([false, true]);
await waitForNextUpdate();
expect(result.current.loadingList).toEqual([false, false]);
expect(result.current.responseDataList).toEqual([
[{ label: '1', value: '1' }],
[{ label: '2', value: '2' }],
]);
});