-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathuseList.test.ts
195 lines (163 loc) · 5.99 KB
/
useList.test.ts
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
import { act, waitFor } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
import useList from '..';
const awaitTimers = async () => {
for (let i = 0; i < 10; i++) {
await Promise.resolve();
}
};
describe('Test useList hook', () => {
afterEach(() => jest.useRealTimers());
it('Should get initial data with default params', async () => {
const fetcher = jest.fn().mockResolvedValue({
total: 1,
data: [{ uuid: 1 }],
});
const { result } = renderHook(() => useList(fetcher, { current: 1, pageSize: 20 }));
expect(fetcher).toBeCalledTimes(1);
await waitFor(() => {
expect(result.current.data.length).toBe(1);
});
expect(result.current.params).toEqual(
expect.objectContaining({ current: 1, pageSize: 20, total: 1 })
);
});
it('Should get data after mutating', () => {
const fetcher = jest.fn().mockResolvedValue({
total: 1,
data: [{ uuid: 1 }],
});
const { result } = renderHook(() =>
useList(fetcher, { current: 1, pageSize: 20, search: '' })
);
expect(fetcher).toBeCalledTimes(1);
act(() => {
result.current.mutate({ search: 'test' });
});
expect(fetcher).toBeCalledTimes(2);
expect(result.current.params).toEqual(expect.objectContaining({ search: 'test' }));
expect(fetcher.mock.calls[1][0]).toEqual(expect.objectContaining({ search: 'test' }));
});
it('Should support revalidate after mutating', () => {
const fetcher = jest.fn().mockResolvedValue({
total: 1,
data: [{ uuid: 1 }],
});
const { result } = renderHook(() =>
useList(fetcher, { current: 1, pageSize: 20, search: '' })
);
expect(fetcher).toBeCalledTimes(1);
act(() => {
result.current.mutate({ search: 'test' }, { revalidate: false });
});
expect(result.current.params).toEqual(expect.objectContaining({ search: 'test' }));
});
it('Should support get data with current params', () => {
const fetcher = jest.fn().mockResolvedValue({
total: 1,
data: [{ uuid: 1 }],
});
const { result } = renderHook(() =>
useList(fetcher, { current: 1, pageSize: 20, search: '' })
);
expect(fetcher).toBeCalledTimes(1);
act(() => {
result.current.mutate();
});
expect(fetcher).toBeCalledTimes(2);
});
it('Should support immediate option', () => {
const fetcher = jest.fn();
renderHook(() => useList(fetcher, {}, { immediate: false }));
expect(fetcher).not.toBeCalled();
});
it('Should hide loading after the last fetching done', async () => {
jest.useFakeTimers({ advanceTimers: true });
const fetcher = jest
.fn()
.mockReturnValueOnce(
new Promise((resolve) => {
setTimeout(() => {
resolve({
total: 0,
data: [],
});
}, 100);
})
)
.mockReturnValueOnce(
new Promise((resolve) => {
setTimeout(() => {
resolve({
total: 0,
data: [],
});
}, 200);
})
);
const { result } = renderHook(() => useList(fetcher, {}, { immediate: false }));
act(() => {
result.current.mutate();
result.current.mutate();
});
jest.advanceTimersByTime(100);
await awaitTimers();
expect(result.current.loading).toBe(true);
jest.advanceTimersByTime(100);
await awaitTimers();
expect(result.current.loading).toBe(false);
});
it('Should support clear data before mutate', async () => {
const fetcher = jest.fn().mockResolvedValue({
total: 1,
data: [{ uuid: 1 }],
error: new Error('testError'),
});
const { result } = renderHook(() =>
useList(fetcher, { current: 1, pageSize: 20, search: '' })
);
expect(fetcher).toBeCalledTimes(1);
await waitFor(() => {
expect(result.current.data.length).toBe(1);
});
act(() => {
result.current.mutate({ search: 'test' }, { clearData: true });
});
expect(result.current.data).toStrictEqual([]);
expect(result.current.params.total).toBe(0);
expect(result.current.error).toBe(undefined);
});
it('Should ONLY clear data while not reset loading', async () => {
const fetcher = jest.fn().mockImplementation(
() =>
new Promise((resolve) => {
setTimeout(() => {
resolve({
total: 1,
data: [{ uuid: 1 }],
});
}, 1000);
})
);
const { result } = renderHook(() =>
useList(fetcher, { current: 1, pageSize: 20, search: '' }, { immediate: false })
);
act(() => {
result.current.mutate({ current: 2 });
});
expect(result.current.loading).toBe(true);
expect(result.current.params.current).toBe(2);
act(() => {
result.current.clearData();
});
// clearData won't reset params and loading status
expect(result.current.loading).toBe(true);
expect(result.current.params.current).toBe(2);
act(() => {
result.current.clear();
});
// clear method will clear all data including params and loading status
expect(result.current.loading).toBe(false);
expect(result.current.params.current).toBe(1);
});
});