Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { QueryClient } from '../QueryClient'
3import { dehydrateSettled } from '../dehydrateSettled'
4β
5const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
6β
7describe('dehydrateSettled', () => {
8 it('waits for in-flight fetches instead of snapshotting mid-fetch', async () => {
9 const client = new QueryClient()
10 // Fire-and-forget, the way loaders hint prefetches.
11 void client.prefetchQuery({
12 queryKey: ['slow'],
13 queryFn: async () => {
14 await sleep(20)
15 return 'slow-data'
16 },
17 })
18β
19 const state = await dehydrateSettled(client)
20β
21 const query = state.queries.find((q) => q.queryHash === '["slow"]')
22 expect(query?.state.data).toBe('slow-data')
23 expect(query?.state.status).toBe('success')
24 })
25β
26 it('chases fetches dispatched by earlier settlements to quiescence', async () => {
27 const client = new QueryClient()
28 void client.prefetchQuery({
29 queryKey: ['first'],
30 queryFn: async () => {
31 await sleep(10)
32 // A dependent fetch that only exists once the first one lands.
33 void client.prefetchQuery({
34 queryKey: ['second'],
35 queryFn: async () => {
36 await sleep(10)
37 return 'second-data'
38 },
39 })
40 return 'first-data'
41 },
42 })
43β
44 const state = await dehydrateSettled(client)
45β
46 expect(state.queries.map((q) => q.queryHash).sort()).toEqual([
47 '["first"]',
48 '["second"]',
49 ])
50 expect(
51 state.queries.find((q) => q.queryHash === '["second"]')?.state.data,
52 ).toBe('second-data')
53 })
54β
55 it('settles failures without rejecting and forwards dehydrate options', async () => {
56 const client = new QueryClient()
57 void client.prefetchQuery({
58 queryKey: ['ok'],
59 queryFn: async () => {
60 await sleep(5)
61 return 'ok-data'
62 },
63 })
64 void client.prefetchQuery({
65 queryKey: ['boom'],
66 retry: false,
67 queryFn: async () => {
68 await sleep(5)
69 throw new Error('nope')
70 },
71 })
72β
73 const state = await dehydrateSettled(client, {
74 shouldDehydrateQuery: (query) => query.state.status === 'success',
75 })
76β
77 expect(state.queries.map((q) => q.queryHash)).toEqual(['["ok"]'])
78 })
79})