-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathuse-swr-subscription.test.tsx
325 lines (289 loc) · 8.38 KB
/
use-swr-subscription.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
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
import { act, fireEvent, screen } from '@testing-library/react'
import { sleep, renderWithConfig, createKey } from './utils'
import useSWRSubscription from 'swr/subscription'
import useSWR from 'swr'
import { useEffect, useState } from 'react'
import { ErrorBoundary } from 'react-error-boundary'
describe('useSWRSubscription', () => {
it('should update the state', async () => {
const swrKey = createKey()
let intervalId
let res = 0
function subscribe(key, { next }) {
intervalId = setInterval(() => {
if (res === 3) {
const err = new Error(key)
next(err)
} else {
next(undefined, key + res)
}
res++
}, 100)
return () => {}
}
function Page() {
const { data, error } = useSWRSubscription(swrKey, subscribe, {
fallbackData: 'fallback'
})
return (
<>
<div data-testid="data">{'data:' + data}</div>
<div data-testid="error">
{'error:' + (error ? error.message : '')}
</div>
</>
)
}
renderWithConfig(<Page />)
await act(() => sleep(10))
screen.getByText(`data:fallback`)
screen.getByText('error:')
await act(() => sleep(100))
screen.getByText(`data:${swrKey}0`)
screen.getByText('error:')
await act(() => sleep(100))
screen.getByText(`data:${swrKey}1`)
screen.getByText('error:')
await act(() => sleep(100))
screen.getByText(`data:${swrKey}2`)
screen.getByText('error:')
await act(() => sleep(100))
// error occurred, error arrives instead of data 3
screen.getByText(`data:${swrKey}2`)
screen.getByText(`error:${swrKey}`)
await act(() => sleep(100))
screen.getByText(`data:${swrKey}4`)
screen.getByText('error:')
clearInterval(intervalId)
await sleep(100)
screen.getByText(`error:`)
})
it('should pass the origin keys', async () => {
const swrKey = createKey()
let intervalId
let res = 0
function subscribe(key, { next }) {
intervalId = setInterval(() => {
if (res === 3) {
const err = new Error(key)
next(err)
} else {
next(undefined, key[0] + res)
}
res++
}, 100)
return () => {}
}
function Page() {
const { data, error } = useSWRSubscription(() => [swrKey], subscribe, {
fallbackData: 'fallback'
})
return (
<>
<div data-testid="data">{'data:' + data}</div>
<div data-testid="error">
{'error:' + (error ? error.message : '')}
</div>
</>
)
}
renderWithConfig(<Page />)
await act(() => sleep(10))
screen.getByText(`data:fallback`)
screen.getByText('error:')
await act(() => sleep(100))
screen.getByText(`data:${swrKey}0`)
screen.getByText('error:')
await act(() => sleep(100))
screen.getByText(`data:${swrKey}1`)
screen.getByText('error:')
await act(() => sleep(100))
screen.getByText(`data:${swrKey}2`)
screen.getByText('error:')
await act(() => sleep(100))
// error occurred, error arrives instead of data 3
screen.getByText(`data:${swrKey}2`)
screen.getByText(`error:${swrKey}`)
await act(() => sleep(100))
screen.getByText(`data:${swrKey}4`)
screen.getByText('error:')
clearInterval(intervalId)
await sleep(100)
screen.getByText(`error:`)
})
it('should support updating keys', async () => {
const swrKey = createKey()
const subscriptions: string[] = []
function subscribe(key, { next }) {
subscriptions.push(key)
next(undefined, key)
return () => {}
}
function Page() {
const [key, setKey] = useState(undefined)
const { data } = useSWRSubscription(key, subscribe, {
fallbackData: 'fallback'
})
useEffect(() => {
const timeout = setTimeout(() => {
setKey(swrKey)
}, 100)
return () => clearTimeout(timeout)
}, [])
return <div data-testid="data">{'data:' + data}</div>
}
renderWithConfig(<Page />)
await screen.findByText(`data:fallback`)
await act(() => sleep(100))
await screen.findByText(`data:` + swrKey)
// `undefined` should not trigger a subscription.
expect(subscriptions).toEqual([swrKey])
})
it('should deduplicate subscriptions', async () => {
const swrKey = createKey()
let subscriptionCount = 0
function subscribe(key, { next }) {
++subscriptionCount
let res = 0
const intervalId = setInterval(() => {
if (res === 3) {
const err = new Error(key + 'error')
next(err)
} else {
next(undefined, key + res)
}
res++
}, 100)
return () => {
clearInterval(intervalId)
}
}
function Page() {
const { data, error } = useSWRSubscription(swrKey, subscribe, {
fallbackData: 'fallback'
})
useSWRSubscription(swrKey, subscribe)
useSWRSubscription(swrKey, subscribe)
return <div>{error ? error.message : data}</div>
}
renderWithConfig(<Page />)
await act(() => sleep(10))
screen.getByText(`fallback`)
await act(() => sleep(100))
screen.getByText(`${swrKey}0`)
await act(() => sleep(100))
screen.getByText(`${swrKey}1`)
await act(() => sleep(100))
screen.getByText(`${swrKey}2`)
expect(subscriptionCount).toBe(1)
})
it('should not conflict with useSWR state', async () => {
const swrKey = createKey()
function subscribe(key, { next }) {
let res = 0
const intervalId = setInterval(() => {
if (res === 3) {
const err = new Error(key + 'error')
next(err)
} else {
next(undefined, key + res)
}
res++
}, 100)
return () => {
clearInterval(intervalId)
}
}
function Page() {
const { data, error } = useSWRSubscription(swrKey, subscribe, {
fallbackData: 'fallback'
})
const { data: swrData } = useSWR(swrKey, () => 'swr')
return (
<div>
{swrData}:{error ? error.message : data}
</div>
)
}
renderWithConfig(<Page />)
await act(() => sleep(10))
screen.getByText(`swr:fallback`)
await act(() => sleep(100))
screen.getByText(`swr:${swrKey}0`)
await act(() => sleep(100))
screen.getByText(`swr:${swrKey}1`)
await act(() => sleep(100))
screen.getByText(`swr:${swrKey}2`)
})
it('should support singleton subscription', async () => {
const placeholderFn: (data: number) => void = () => {}
let callback: (data: number) => void = placeholderFn
const sub = (fn: (data: number) => void) => {
callback = fn
return () => {
callback = placeholderFn
}
}
const emit = (data: number) => {
callback(data)
}
const useSubData = (key: number) =>
useSWRSubscription(key.toString(), (_, { next }) =>
sub(data => next(null, data + key))
)
const App = () => {
const [key, setKey] = useState(0)
const { data } = useSubData(key)
useEffect(() => {
callback(1)
}, [])
return (
<div className="App">
<p>key: {key}</p>
<p className="read-the-docs">data: {data}</p>
<button
onClick={() => {
setKey(value => value + 1)
setTimeout(() => {
emit(2)
}, 100)
}}
>
add
</button>
</div>
)
}
renderWithConfig(<App />)
await screen.findByText(`key: 0`)
await screen.findByText(`data: 1`)
fireEvent.click(screen.getByText('add'))
await act(() => sleep(100))
await screen.findByText(`key: 1`)
await screen.findByText(`data: 3`)
})
it('should require a dispose function', async () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
jest.spyOn(console, 'error').mockImplementation(() => {})
const swrKey = createKey()
function subscribe() {
return 'no-dispose'
}
function Page() {
useSWRSubscription(swrKey, subscribe)
return null
}
renderWithConfig(
<ErrorBoundary
fallbackRender={({ error }) => {
return <div>{error.message}</div>
}}
>
<Page />
</ErrorBoundary>
)
await screen.findByText(
'The `subscribe` function must return a function to unsubscribe.'
)
})
})