forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hydrate.test.tsx
208 lines (173 loc) · 5.52 KB
/
Hydrate.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
import React from 'react'
import { render } from '@testing-library/react'
import {
QueryClient,
QueryClientProvider,
QueryCache,
useQuery,
dehydrate,
useHydrate,
Hydrate,
} from '../..'
import { sleep } from './utils'
import * as coreModule from '../../core/index'
describe('React hydration', () => {
const fetchData: (value: string) => Promise<string> = value =>
new Promise(res => setTimeout(() => res(value), 10))
const dataQuery: (key: string) => Promise<string> = key => fetchData(key)
let stringifiedState: string
beforeAll(async () => {
const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })
await queryClient.prefetchQuery('string', () => dataQuery('string'))
const dehydrated = dehydrate(queryClient)
stringifiedState = JSON.stringify(dehydrated)
queryClient.clear()
})
describe('useHydrate', () => {
test('should hydrate queries to the cache on context', async () => {
const dehydratedState = JSON.parse(stringifiedState)
const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })
function Page() {
useHydrate(dehydratedState)
const { data } = useQuery('string', () => dataQuery('string'))
return (
<div>
<h1>{data}</h1>
</div>
)
}
const rendered = render(
<QueryClientProvider client={queryClient}>
<Page />
</QueryClientProvider>
)
await sleep(10)
rendered.getByText('string')
queryClient.clear()
})
})
describe('ReactQueryCacheProvider with hydration support', () => {
test('should hydrate new queries if queries change', async () => {
const dehydratedState = JSON.parse(stringifiedState)
const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })
function Page({ queryKey }: { queryKey: string }) {
const { data } = useQuery(queryKey, () => dataQuery(queryKey))
return (
<div>
<h1>{data}</h1>
</div>
)
}
const rendered = render(
<QueryClientProvider client={queryClient}>
<Hydrate state={dehydratedState}>
<Page queryKey={'string'} />
</Hydrate>
</QueryClientProvider>
)
await sleep(10)
rendered.getByText('string')
const intermediateCache = new QueryCache()
const intermediateClient = new QueryClient({
queryCache: intermediateCache,
})
await intermediateClient.prefetchQuery('string', () =>
dataQuery('should change')
)
await intermediateClient.prefetchQuery('added string', () =>
dataQuery('added string')
)
const dehydrated = dehydrate(intermediateClient)
intermediateClient.clear()
rendered.rerender(
<QueryClientProvider client={queryClient}>
<Hydrate state={dehydrated}>
<Page queryKey={'string'} />
<Page queryKey={'added string'} />
</Hydrate>
</QueryClientProvider>
)
// Existing query data should be overwritten if older,
// so this should have changed
await sleep(10)
rendered.getByText('should change')
// New query data should be available immediately
rendered.getByText('added string')
queryClient.clear()
})
test('should hydrate queries to new cache if cache changes', async () => {
const dehydratedState = JSON.parse(stringifiedState)
const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })
function Page() {
const { data } = useQuery('string', () => dataQuery('string'))
return (
<div>
<h1>{data}</h1>
</div>
)
}
const rendered = render(
<QueryClientProvider client={queryClient}>
<Hydrate state={dehydratedState}>
<Page />
</Hydrate>
</QueryClientProvider>
)
await sleep(10)
rendered.getByText('string')
const newClientQueryCache = new QueryCache()
const newClientQueryClient = new QueryClient({
queryCache: newClientQueryCache,
})
rendered.rerender(
<QueryClientProvider client={newClientQueryClient}>
<Hydrate state={dehydratedState}>
<Page />
</Hydrate>
</QueryClientProvider>
)
await sleep(10)
rendered.getByText('string')
queryClient.clear()
newClientQueryClient.clear()
})
})
test('should not hydrate queries if state is null', async () => {
const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })
const hydrateSpy = jest.spyOn(coreModule, 'hydrate')
function Page() {
useHydrate(null)
return null
}
render(
<QueryClientProvider client={queryClient}>
<Page />
</QueryClientProvider>
)
expect(hydrateSpy).toHaveBeenCalledTimes(0)
hydrateSpy.mockRestore()
queryClient.clear()
})
test('should not hydrate queries if state is undefined', async () => {
const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })
const hydrateSpy = jest.spyOn(coreModule, 'hydrate')
function Page() {
useHydrate(undefined)
return null
}
render(
<QueryClientProvider client={queryClient}>
<Page />
</QueryClientProvider>
)
expect(hydrateSpy).toHaveBeenCalledTimes(0)
hydrateSpy.mockRestore()
queryClient.clear()
})
})