-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathuse-swr-promise.test.tsx
182 lines (153 loc) · 4.7 KB
/
use-swr-promise.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
import { act, screen } from '@testing-library/react'
import useSWR, { SWRConfig } from 'swr'
import { createKey, createResponse, renderWithConfig, sleep } from './utils'
import { Suspense } from 'react'
import { ErrorBoundary } from 'react-error-boundary'
describe('useSWR - promise', () => {
it('should allow passing promises as fallback', async () => {
const key = createKey()
const firstRender = [false, undefined] as [boolean, string | undefined]
function Page() {
const { data } = useSWR(key, () => {
return createResponse('new data', { delay: 100 })
})
if (!firstRender[0]) {
firstRender[0] = true
firstRender[1] = data
}
return <div>data:{data}</div>
}
const fetchData = createResponse('initial data', { delay: 100 })
renderWithConfig(
<SWRConfig
value={{
fallback: {
[key]: fetchData
}
}}
>
<Page />
</SWRConfig>
)
await screen.findByText('data:initial data')
await act(() => sleep(100)) // wait 100ms until the request inside finishes
await screen.findByText('data:new data')
expect(firstRender[1]).toEqual('initial data')
})
it('should allow passing promises as fallbackData', async () => {
const key = createKey()
const fetchData = createResponse('initial data', { delay: 100 })
const firstRender = [false, undefined] as [boolean, string | undefined]
function Page() {
const { data } = useSWR(
key,
() => {
return createResponse('new data', { delay: 100 })
},
{
fallbackData: fetchData
}
)
if (!firstRender[0]) {
firstRender[0] = true
firstRender[1] = data
}
return <div>data:{data}</div>
}
renderWithConfig(<Page />)
await screen.findByText('data:initial data')
await act(() => sleep(100)) // wait 100ms until the request inside finishes
await screen.findByText('data:new data')
expect(firstRender[1]).toEqual('initial data')
})
it('should suspend when resolving the fallback promise', async () => {
const key = createKey()
const firstRender = [false, undefined] as [boolean, string | undefined]
function Page() {
const { data } = useSWR(key, () => {
return createResponse('new data', { delay: 100 })
})
if (!firstRender[0]) {
firstRender[0] = true
firstRender[1] = data
}
return <div>data:{data}</div>
}
const fetchData = createResponse('initial data', { delay: 100 })
renderWithConfig(
<SWRConfig
value={{
fallback: {
[key]: fetchData
}
}}
>
<Suspense fallback={<div>loading</div>}>
<Page />
</Suspense>
</SWRConfig>
)
await screen.findByText('loading')
await screen.findByText('data:initial data')
await act(() => sleep(100)) // wait 100ms until the request inside finishes
await screen.findByText('data:new data')
expect(firstRender[1]).toEqual('initial data')
})
it('should handle errors with fallback promises', async () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
jest.spyOn(console, 'error').mockImplementation(() => {})
const key = createKey()
function Page() {
const { data } = useSWR(key)
return <div>data:{data}</div>
}
const fetchDataError = createResponse(new Error('error'), {
delay: 100
})
renderWithConfig(
<ErrorBoundary fallback={<div>error boundary</div>}>
<SWRConfig
value={{
fallback: {
[key]: fetchDataError
}
}}
>
<Suspense fallback={<div>loading</div>}>
<Page />
</Suspense>
</SWRConfig>
</ErrorBoundary>
)
await screen.findByText('loading')
await act(() => sleep(100)) // wait 100ms until the request inside throws
await screen.findByText('error boundary')
})
it('should handle same fallback promise that is already pending', async () => {
const key = createKey()
function Comp() {
const { data } = useSWR(key)
return <>data:{data},</>
}
const fetchDataError = createResponse('value', {
delay: 100
})
renderWithConfig(
<SWRConfig
value={{
fallback: {
[key]: fetchDataError
}
}}
>
<Suspense fallback={<div>loading</div>}>
<Comp />
<Comp />
</Suspense>
</SWRConfig>
)
await screen.findByText('loading')
await act(() => sleep(100)) // wait 100ms until the request inside resolves
await screen.findByText('data:value,data:value,')
})
})