-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathuse-swr-preload.test.tsx
227 lines (186 loc) · 5.55 KB
/
use-swr-preload.test.tsx
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
import { act, fireEvent, screen } from '@testing-library/react'
import { Suspense, useEffect, useState, Profiler } from 'react'
import useSWR, { preload, useSWRConfig } from 'swr'
import {
createKey,
createResponse,
renderWithGlobalCache,
sleep
} from './utils'
describe('useSWR - preload', () => {
it('preload the fetcher function', async () => {
const key = createKey()
const fetcher = jest.fn(() => createResponse('foo'))
function Page() {
const { data } = useSWR(key, fetcher)
return <div>data:{data}</div>
}
preload(key, fetcher)
expect(fetcher).toBeCalledTimes(1)
renderWithGlobalCache(<Page />)
await screen.findByText('data:foo')
expect(fetcher).toBeCalledTimes(1)
})
it('should avoid preloading the resource multiple times', async () => {
const key = createKey()
const fetcher = jest.fn(() => createResponse('foo'))
function Page() {
const { data } = useSWR(key, fetcher)
return <div>data:{data}</div>
}
preload(key, fetcher)
preload(key, fetcher)
preload(key, fetcher)
expect(fetcher).toBeCalledTimes(1)
renderWithGlobalCache(<Page />)
await screen.findByText('data:foo')
expect(fetcher).toBeCalledTimes(1)
})
it('should be able to prealod resources in effects', async () => {
const key = createKey()
const fetcher = jest.fn(() => createResponse('foo'))
function Comp() {
const { data } = useSWR(key, fetcher)
return <div>data:{data}</div>
}
function Page() {
const [show, setShow] = useState(false)
useEffect(() => {
preload(key, fetcher)
}, [])
return show ? (
<Comp />
) : (
<button onClick={() => setShow(true)}>click</button>
)
}
renderWithGlobalCache(<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 fetcher = jest.fn(() => createResponse('foo'))
const onRender = jest.fn()
function Page() {
const { data } = useSWR(key, fetcher, { suspense: true })
return <div>data:{data}</div>
}
preload(key, fetcher)
expect(fetcher).toBeCalledTimes(1)
renderWithGlobalCache(
<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 key2 = createKey()
const response1 = createResponse('foo', { delay: 50 })
const response2 = createResponse('bar', { delay: 50 })
const fetcher1 = () => response1
const fetcher2 = () => response2
function Page() {
const { data: data1 } = useSWR(key1, fetcher1, { suspense: true })
const { data: data2 } = useSWR(key2, fetcher2, { suspense: true })
return (
<div>
data:{data1}:{data2}
</div>
)
}
preload(key1, fetcher1)
preload(key2, fetcher2)
renderWithGlobalCache(
<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()
let count = 0
const fetcher = () => {
++count
const res = count === 1 ? new Error('err') : 'foo'
return createResponse(res)
}
let mutate
function Page() {
mutate = useSWRConfig().mutate
const { data, error } = useSWR<any>(key, fetcher)
if (error) {
return <div>error:{error.message}</div>
}
return <div>data:{data}</div>
}
try {
// error
await preload(key, fetcher)
} catch (e) {
// noop
}
renderWithGlobalCache(<Page />)
screen.getByText('data:')
// use the preloaded result
await screen.findByText('error:err')
expect(count).toBe(1)
// revalidate
await act(() => mutate(key))
// should not use the preload data
await screen.findByText('data:foo')
})
it('dedupe requests during preloading', async () => {
const key = createKey()
const fetcher = jest.fn(() =>
createResponse('foo', {
delay: 50
})
)
const onRender = jest.fn()
function Page() {
const { data } = useSWR(key, fetcher, { dedupingInterval: 0 })
return (
<Profiler id={key} onRender={onRender}>
data:{data}
</Profiler>
)
}
preload(key, fetcher)
expect(fetcher).toBeCalledTimes(1)
const { rerender } = renderWithGlobalCache(<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()
let calledWith: string
const fetcher = (args: string) => {
calledWith = args
}
preload(() => key, fetcher)
expect(calledWith).toBe(key)
})
})