forked from vercel/swr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-swr-infinite-preload.test.tsx
More file actions
299 lines (250 loc) · 7.98 KB
/
use-swr-infinite-preload.test.tsx
File metadata and controls
299 lines (250 loc) · 7.98 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
import { act, fireEvent, screen } from '@testing-library/react'
import { Suspense, useEffect, useState, Profiler } from 'react'
import { preload } from 'swr'
import useSWRInfinite from 'swr/infinite'
import { createKey, createResponse, renderWithConfig, sleep } from './utils'
describe('useSWRInfinite - preload', () => {
const getKeyFunction = (key: string) => (index: number) =>
`page-${index}-${key}`
it('preloading useSWRInfinite should produce the same result', async () => {
const key = createKey()
const getKey = getKeyFunction(key)
const fetcher = jest.fn(() => createResponse('foo'))
function Page() {
const { data } = useSWRInfinite(getKey, fetcher)
return <div>data:{Array.isArray(data) ? 'true' : 'false'}</div>
}
preload(getKey(0), fetcher)
renderWithConfig(<Page />)
await screen.findByText('data:true')
})
it('preload the fetcher function', async () => {
const key = createKey()
const getKey = getKeyFunction(key)
const fetcher = jest.fn(() => createResponse('foo'))
function Page() {
const { data } = useSWRInfinite(getKey, fetcher)
return <div>data:{data}</div>
}
preload(getKey(0), fetcher)
expect(fetcher).toBeCalledTimes(1)
renderWithConfig(<Page />)
await screen.findByText('data:foo')
expect(fetcher).toBeCalledTimes(1)
})
it('should avoid preloading the resource multiple times', async () => {
const key = createKey()
const getKey = getKeyFunction(key)
const fetcher = jest.fn(() => createResponse('foo'))
function Page() {
const { data } = useSWRInfinite(getKey, fetcher)
return <div>data:{data}</div>
}
preload(getKey(0), fetcher)
preload(getKey(0), fetcher)
preload(getKey(0), fetcher)
expect(fetcher).toBeCalledTimes(1)
renderWithConfig(<Page />)
await screen.findByText('data:foo')
expect(fetcher).toBeCalledTimes(1)
})
it('should be able to prealod resources in effects', async () => {
const key = createKey()
const getKey = getKeyFunction(key)
const fetcher = jest.fn(() => createResponse('foo'))
function Comp() {
const { data } = useSWRInfinite(getKey, fetcher)
return <div>data:{data}</div>
}
function Page() {
const [show, setShow] = useState(false)
useEffect(() => {
preload(getKey(0), fetcher)
}, [])
return show ? (
<Comp />
) : (
<button onClick={() => setShow(true)}>click</button>
)
}
renderWithConfig(<Page />)
expect(fetcher).toBeCalledTimes(1)
fireEvent.click(screen.getByText('click'))
await screen.findByText('data:foo')
expect(fetcher).toBeCalledTimes(1)
})
it('preload the fetcher function with the suspense mode', async () => {
const key = createKey()
const getKey = getKeyFunction(key)
const fetcher = jest.fn(() => createResponse('foo'))
const onRender = jest.fn()
function Page() {
const { data } = useSWRInfinite(getKey, fetcher, { suspense: true })
return <div>data:{data}</div>
}
preload(getKey(0), fetcher)
expect(fetcher).toBeCalledTimes(1)
renderWithConfig(
<Suspense
fallback={
<Profiler id={key} onRender={onRender}>
loading
</Profiler>
}
>
<Page />
</Suspense>
)
await screen.findByText('data:foo')
expect(onRender).toBeCalledTimes(1)
expect(fetcher).toBeCalledTimes(1)
})
it('avoid suspense waterfall by prefetching the resources', async () => {
const key1 = createKey()
const getKey1 = getKeyFunction(key1)
const key2 = createKey()
const getKey2 = getKeyFunction(key2)
const response1 = createResponse('foo', { delay: 50 })
const response2 = createResponse('bar', { delay: 50 })
const fetcher1 = () => response1
const fetcher2 = () => response2
function Page() {
const { data: data1 } = useSWRInfinite(getKey1, fetcher1, {
suspense: true
})
const { data: data2 } = useSWRInfinite(getKey2, fetcher2, {
suspense: true
})
return (
<div>
data:{data1}:{data2}
</div>
)
}
preload(getKey1(0), fetcher1)
preload(getKey1(0), fetcher2)
renderWithConfig(
<Suspense fallback="loading">
<Page />
</Suspense>
)
screen.getByText('loading')
// Should avoid waterfall(50ms + 50ms)
await act(() => sleep(80))
screen.getByText('data:foo:bar')
})
it('reset the preload result when the preload function gets an error', async () => {
const key = createKey()
const getKey = getKeyFunction(key)
let count = 0
const fetcher = () => {
++count
const res = count === 1 ? new Error('err') : 'foo'
return createResponse(res)
}
let mutate
function Page() {
const { data, error, ...swr } = useSWRInfinite<any>(getKey, fetcher)
mutate = swr.mutate
if (error) {
return <div>error:{error.message}</div>
}
return <div>data:{data}</div>
}
try {
// error
await preload(getKey(0), fetcher)
} catch (e) {
// noop
}
renderWithConfig(<Page />)
screen.getByText('data:')
// use the preloaded result
await screen.findByText('error:err')
expect(count).toBe(1)
// revalidate
await act(() => mutate(getKey(0)))
// should not use the preload data
await screen.findByText('data:foo')
})
it('dedupe requests during preloading', async () => {
const key = createKey()
const getKey = getKeyFunction(key)
const fetcher = jest.fn(() =>
createResponse('foo', {
delay: 50
})
)
const onRender = jest.fn()
function Page() {
const { data } = useSWRInfinite(getKey, fetcher, { dedupingInterval: 0 })
return (
<Profiler id={key} onRender={onRender}>
data:{data}
</Profiler>
)
}
preload(getKey(0), fetcher)
expect(fetcher).toBeCalledTimes(1)
const { rerender } = renderWithConfig(<Page />)
expect(onRender).toBeCalledTimes(1)
// rerender when the preloading is in-flight, and the deduping interval is over
await act(() => sleep(10))
rerender(<Page />)
expect(onRender).toBeCalledTimes(2)
await screen.findByText('data:foo')
expect(fetcher).toBeCalledTimes(1)
expect(onRender).toBeCalledTimes(3)
})
it('should pass serialize key to fetcher', async () => {
const key = createKey()
const getKey = getKeyFunction(key)
let calledWith: string
const fetcher = (args: string) => {
calledWith = args
}
preload(() => getKey(0), fetcher)
expect(calledWith).toBe(getKey(0))
})
it('should not break parallel option', async () => {
// mock api
const pageData = ['apple', 'banana', 'pineapple']
const key = createKey()
const fetcher = ([_, index]) =>
createResponse(`${pageData[index]}, `, { delay: index === 0 ? 50 : 200 })
function Page() {
const { data } = useSWRInfinite(index => [key, index], fetcher, {
initialSize: 3,
parallel: true
})
return <div>data:{data}</div>
}
preload([key, 0], fetcher)
renderWithConfig(<Page />)
screen.getByText('data:')
// If SWR sends parallel requests, it should only take 200ms
await act(() => sleep(200))
screen.getByText('data:apple, banana, pineapple,')
})
it('should be able to preload multiple page', async () => {
// mock api
const pageData = ['apple', 'banana', 'pineapple']
const key = createKey()
const fetcher = ([_, index]) =>
createResponse(`${pageData[index]}, `, { delay: 50 })
function Page() {
const { data } = useSWRInfinite(index => [key, index], fetcher, {
initialSize: 3,
parallel: true
})
return <div>data:{data}</div>
}
preload([key, 0], fetcher)
preload([key, 1], fetcher)
preload([key, 2], fetcher)
renderWithConfig(<Page />)
screen.getByText('data:')
await act(() => sleep(50))
screen.getByText('data:apple, banana, pineapple,')
})
})