Skip to content

Commit 9376835

Browse files
sukvvonmanudeli
andauthored
test(react-query/HydrationBoundary): use precise time in 'advanceTimersByTimeAsync', add 'expect' using 'toBeInTheDocument', and simplify 'queryFn' (#9472)
Co-authored-by: Jonghyeon Ko <manudeli.ko@gmail.com>
1 parent cb9ab1e commit 9376835

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

packages/react-query/src/__tests__/HydrationBoundary.test.tsx

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ describe('React hydration', () => {
5454
</HydrationBoundary>
5555
</QueryClientProvider>,
5656
)
57-
await vi.advanceTimersByTimeAsync(1)
57+
5858
expect(rendered.getByText('stringCached')).toBeInTheDocument()
59-
await vi.advanceTimersByTimeAsync(20)
59+
await vi.advanceTimersByTimeAsync(21)
6060
expect(rendered.getByText('string')).toBeInTheDocument()
6161
queryClient.clear()
6262
})
@@ -89,9 +89,8 @@ describe('React hydration', () => {
8989
</QueryClientProvider>,
9090
)
9191

92-
await vi.advanceTimersByTimeAsync(1)
9392
expect(rendered.getByText('stringCached')).toBeInTheDocument()
94-
await vi.advanceTimersByTimeAsync(20)
93+
await vi.advanceTimersByTimeAsync(21)
9594
expect(rendered.getByText('string')).toBeInTheDocument()
9695

9796
queryClientInner.clear()
@@ -123,9 +122,8 @@ describe('React hydration', () => {
123122
</QueryClientProvider>,
124123
)
125124

126-
await vi.advanceTimersByTimeAsync(1)
127125
expect(rendered.getByText('stringCached')).toBeInTheDocument()
128-
await vi.advanceTimersByTimeAsync(20)
126+
await vi.advanceTimersByTimeAsync(21)
129127
expect(rendered.getByText('string')).toBeInTheDocument()
130128

131129
const intermediateClient = new QueryClient()
@@ -134,7 +132,6 @@ describe('React hydration', () => {
134132
queryKey: ['string'],
135133
queryFn: () => sleep(20).then(() => ['should change']),
136134
})
137-
await vi.advanceTimersByTimeAsync(20)
138135
intermediateClient.prefetchQuery({
139136
queryKey: ['added'],
140137
queryFn: () => sleep(20).then(() => ['added']),
@@ -158,9 +155,9 @@ describe('React hydration', () => {
158155
// New query data should be available immediately
159156
expect(rendered.getByText('added')).toBeInTheDocument()
160157

161-
await vi.advanceTimersByTimeAsync(20)
158+
await vi.advanceTimersByTimeAsync(0)
162159
// After effects phase has had time to run, the observer should have updated
163-
expect(rendered.queryByText('string')).toBeNull()
160+
expect(rendered.queryByText('string')).not.toBeInTheDocument()
164161
expect(rendered.getByText('should change')).toBeInTheDocument()
165162

166163
queryClient.clear()
@@ -196,17 +193,15 @@ describe('React hydration', () => {
196193
</QueryClientProvider>,
197194
)
198195

199-
await vi.advanceTimersByTimeAsync(1)
200196
expect(rendered.getByText('stringCached')).toBeInTheDocument()
201-
await vi.advanceTimersByTimeAsync(20)
197+
await vi.advanceTimersByTimeAsync(21)
202198
expect(rendered.getByText('string')).toBeInTheDocument()
203199

204200
const intermediateClient = new QueryClient()
205201
intermediateClient.prefetchQuery({
206202
queryKey: ['string'],
207203
queryFn: () => sleep(20).then(() => ['should not change']),
208204
})
209-
await vi.advanceTimersByTimeAsync(20)
210205
intermediateClient.prefetchQuery({
211206
queryKey: ['added'],
212207
queryFn: () => sleep(20).then(() => ['added']),
@@ -238,7 +233,7 @@ describe('React hydration', () => {
238233
</React.Suspense>,
239234
)
240235

241-
rendered.getByText('loading')
236+
expect(rendered.getByText('loading')).toBeInTheDocument()
242237
})
243238

244239
React.startTransition(() => {
@@ -253,7 +248,9 @@ describe('React hydration', () => {
253248

254249
// This query existed before the transition so it should stay the same
255250
expect(rendered.getByText('string')).toBeInTheDocument()
256-
expect(rendered.queryByText('should not change')).toBeNull()
251+
expect(
252+
rendered.queryByText('should not change'),
253+
).not.toBeInTheDocument()
257254
// New query data should be available immediately because it was
258255
// hydrated in the previous transition, even though the new dehydrated
259256
// state did not contain it
@@ -263,7 +260,7 @@ describe('React hydration', () => {
263260
await vi.advanceTimersByTimeAsync(20)
264261
// It should stay the same even after effects have had a chance to run
265262
expect(rendered.getByText('string')).toBeInTheDocument()
266-
expect(rendered.queryByText('should not change')).toBeNull()
263+
expect(rendered.queryByText('should not change')).not.toBeInTheDocument()
267264

268265
queryClient.clear()
269266
})
@@ -292,9 +289,8 @@ describe('React hydration', () => {
292289
</QueryClientProvider>,
293290
)
294291

295-
await vi.advanceTimersByTimeAsync(1)
296292
expect(rendered.getByText('stringCached')).toBeInTheDocument()
297-
await vi.advanceTimersByTimeAsync(20)
293+
await vi.advanceTimersByTimeAsync(21)
298294
expect(rendered.getByText('string')).toBeInTheDocument()
299295
const newClientQueryClient = new QueryClient()
300296

@@ -359,7 +355,7 @@ describe('React hydration', () => {
359355
</QueryClientProvider>,
360356
)
361357

362-
await vi.runAllTimersAsync()
358+
await vi.advanceTimersByTimeAsync(0)
363359
expect(hydrateSpy).toHaveBeenCalledTimes(0)
364360

365361
hydrateSpy.mockRestore()
@@ -386,12 +382,11 @@ describe('React hydration', () => {
386382
// For the bug to trigger, there needs to already be a query in the cache,
387383
// with a dataUpdatedAt earlier than the dehydratedAt of the next query
388384
const clientQueryClient = new QueryClient()
389-
await clientQueryClient.prefetchQuery({
385+
clientQueryClient.prefetchQuery({
390386
queryKey: ['promise'],
391-
queryFn: () => 'existing',
387+
queryFn: () => sleep(20).then(() => 'existing'),
392388
})
393-
394-
await vi.advanceTimersByTimeAsync(100)
389+
await vi.advanceTimersByTimeAsync(20)
395390

396391
const prefetchQueryClient = new QueryClient({
397392
defaultOptions: {
@@ -402,10 +397,8 @@ describe('React hydration', () => {
402397
})
403398
prefetchQueryClient.prefetchQuery({
404399
queryKey: ['promise'],
405-
queryFn: async () => {
406-
await sleep(10)
407-
throw new Error('Query failed')
408-
},
400+
queryFn: () =>
401+
sleep(10).then(() => Promise.reject(new Error('Query failed'))),
409402
})
410403

411404
const dehydratedState = dehydrate(prefetchQueryClient)
@@ -422,7 +415,7 @@ describe('React hydration', () => {
422415
function Page() {
423416
const { data } = useQuery({
424417
queryKey: ['promise'],
425-
queryFn: () => sleep(10).then(() => ['new']),
418+
queryFn: () => sleep(20).then(() => ['new']),
426419
})
427420
return (
428421
<div>
@@ -438,9 +431,9 @@ describe('React hydration', () => {
438431
</HydrationBoundary>
439432
</QueryClientProvider>,
440433
)
441-
await vi.advanceTimersByTimeAsync(1)
434+
442435
expect(rendered.getByText('existing')).toBeInTheDocument()
443-
await vi.advanceTimersByTimeAsync(10)
436+
await vi.advanceTimersByTimeAsync(21)
444437
expect(rendered.getByText('new')).toBeInTheDocument()
445438

446439
process.removeListener('unhandledRejection', ignore)

0 commit comments

Comments
 (0)