Open History to see the last commit
1/** @jsxRuntime automatic */
2/** @jsxImportSource preact */
3import { act, cleanup, render } from '@testing-library/preact'
4import { QueryClient, useQuery } from '@tanstack/preact-query'
5import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
6import { BroadcastQueryClientProvider } from '../BroadcastQueryClientProvider'
7β
8const mockState = vi.hoisted(() => {
9 const state: {
10 resolveRestore?: () => void
11 restoreResolvers: Array<() => void>
12 } = { restoreResolvers: [] }
13 const cleanupSession = vi.fn()
14 const restore = vi.fn(() => {
15 const restorePromise = new Promise<void>((resolve) => {
16 state.restoreResolvers.push(resolve)
17 state.resolveRestore = resolve
18 })
19 return [cleanupSession, restorePromise] as const
20 })
21 return { cleanupSession, restore, state }
22})
23β
24vi.mock('@tanstack/query-broadcast-client-experimental', () => ({
25 broadcastQueryClientRestore: mockState.restore,
26}))
27β
28describe('BroadcastQueryClientProvider (preact)', () => {
29 beforeEach(() => {
30 mockState.cleanupSession.mockReset()
31 mockState.restore.mockClear()
32 mockState.state.resolveRestore = undefined
33 mockState.state.restoreResolvers.length = 0
34 })
35β
36 afterEach(() => {
37 cleanup()
38 })
39β
40 it('holds query observers until bootstrap completes', async () => {
41 const queryKey = ['preact-bootstrap']
42 const queryFn = vi.fn().mockResolvedValue('network')
43 const queryClient = new QueryClient()
44 queryClient.setQueryData(queryKey, 'cache', { updatedAt: Date.now() })
45β
46 function Page() {
47 const query = useQuery({
48 queryKey,
49 queryFn,
50 staleTime: Infinity,
51 })
52 return <div>{query.data ?? query.fetchStatus}</div>
53 }
54β
55 const rendered = render(
56 <BroadcastQueryClientProvider
57 client={queryClient}
58 broadcastOptions={{ broadcastChannel: 'test-channel' }}
59 >
60 <Page />
61 </BroadcastQueryClientProvider>,
62 )
63β
64 expect(rendered.getByText('cache')).toBeInTheDocument()
65 expect(queryFn).not.toHaveBeenCalled()
66β
67 await act(() => {
68 mockState.state.resolveRestore?.()
69 })
70β
71 expect(queryFn).not.toHaveBeenCalled()
72 rendered.unmount()
73 expect(mockState.cleanupSession).toHaveBeenCalledOnce()
74 })
75β
76 it('refetches a stale query after bootstrap completes', async () => {
77 const queryKey = ['preact-stale-bootstrap']
78 const queryFn = vi.fn().mockResolvedValue('network')
79 const queryClient = new QueryClient()
80 queryClient.setQueryData(queryKey, 'stale', { updatedAt: 0 })
81β
82 function Page() {
83 const query = useQuery({ queryKey, queryFn, staleTime: 0 })
84 return <div>{query.data ?? query.fetchStatus}</div>
85 }
86β
87 const rendered = render(
88 <BroadcastQueryClientProvider
89 client={queryClient}
90 broadcastOptions={{ broadcastChannel: 'test-channel' }}
91 >
92 <Page />
93 </BroadcastQueryClientProvider>,
94 )
95β
96 await act(() => {
97 mockState.state.resolveRestore?.()
98 })
99 await act(async () => {
100 await Promise.resolve()
101 })
102β
103 expect(queryFn).toHaveBeenCalledOnce()
104 rendered.unmount()
105 })
106β
107 it('gates a replacement QueryClient before its restore effect runs', async () => {
108 const queryKey = ['replacement-client-bootstrap']
109 const queryFn = vi.fn().mockResolvedValue('network')
110 const firstClient = new QueryClient()
111 const replacementClient = new QueryClient()
112 firstClient.setQueryData(queryKey, 'first-cache', {
113 updatedAt: Date.now(),
114 })
115β
116 function Page() {
117 const query = useQuery({ queryKey, queryFn, staleTime: Infinity })
118 return <div>{query.data ?? query.fetchStatus}</div>
119 }
120β
121 const rendered = render(
122 <BroadcastQueryClientProvider
123 client={firstClient}
124 broadcastOptions={{ broadcastChannel: 'test-channel' }}
125 >
126 <Page key="first" />
127 </BroadcastQueryClientProvider>,
128 )
129β
130 await act(() => {
131 mockState.state.resolveRestore?.()
132 })
133β
134 rendered.rerender(
135 <BroadcastQueryClientProvider
136 client={replacementClient}
137 broadcastOptions={{ broadcastChannel: 'test-channel' }}
138 >
139 <Page key="replacement" />
140 </BroadcastQueryClientProvider>,
141 )
142β
143 expect(queryFn).not.toHaveBeenCalled()
144 expect(mockState.restore).toHaveBeenCalledTimes(2)
145 expect(mockState.cleanupSession).toHaveBeenCalledOnce()
146β
147 await act(() => {
148 mockState.state.resolveRestore?.()
149 })
150 await act(async () => {
151 await Promise.resolve()
152 })
153β
154 expect(queryFn).toHaveBeenCalledOnce()
155 rendered.unmount()
156 expect(mockState.cleanupSession).toHaveBeenCalledTimes(2)
157 })
158β
159 it('does not let old restore promises open a rapidly replaced client gate', async () => {
160 const queryFn = vi.fn().mockResolvedValue('network')
161 const firstClient = new QueryClient()
162 const replacementClient = new QueryClient()
163 const finalClient = new QueryClient()
164β
165 function Page() {
166 const query = useQuery({
167 queryKey: ['stale-restore-promise'],
168 queryFn,
169 })
170 return <div>{query.fetchStatus}</div>
171 }
172β
173 const rendered = render(
174 <BroadcastQueryClientProvider
175 client={firstClient}
176 broadcastOptions={{ broadcastChannel: 'test-channel' }}
177 >
178 <Page key="first" />
179 </BroadcastQueryClientProvider>,
180 )
181β
182 rendered.rerender(
183 <BroadcastQueryClientProvider
184 client={replacementClient}
185 broadcastOptions={{ broadcastChannel: 'test-channel' }}
186 >
187 <Page key="replacement" />
188 </BroadcastQueryClientProvider>,
189 )
190β
191 rendered.rerender(
192 <BroadcastQueryClientProvider
193 client={finalClient}
194 broadcastOptions={{ broadcastChannel: 'test-channel' }}
195 >
196 <Page key="final" />
197 </BroadcastQueryClientProvider>,
198 )
199β
200 const firstRestore = mockState.state.restoreResolvers[0]
201 const replacementRestore = mockState.state.restoreResolvers[1]
202 const finalRestore = mockState.state.restoreResolvers[2]
203 expect(firstRestore).toEqual(expect.any(Function))
204 expect(replacementRestore).toEqual(expect.any(Function))
205 expect(finalRestore).toEqual(expect.any(Function))
206β
207 await act(async () => {
208 firstRestore?.()
209 await Promise.resolve()
210 })
211 expect(queryFn).not.toHaveBeenCalled()
212β
213 await act(async () => {
214 replacementRestore?.()
215 await Promise.resolve()
216 })
217 expect(queryFn).not.toHaveBeenCalled()
218β
219 await act(async () => {
220 finalRestore?.()
221 await Promise.resolve()
222 })
223 expect(queryFn).toHaveBeenCalledOnce()
224 rendered.unmount()
225 })
226β
227 it('does not restart restore when the QueryClient is unchanged', () => {
228 const queryClient = new QueryClient()
229 const rendered = render(
230 <BroadcastQueryClientProvider
231 client={queryClient}
232 broadcastOptions={{ broadcastChannel: 'test-channel' }}
233 />,
234 )
235β
236 rendered.rerender(
237 <BroadcastQueryClientProvider
238 client={queryClient}
239 broadcastOptions={{ broadcastChannel: 'test-channel' }}
240 />,
241 )
242β
243 expect(mockState.restore).toHaveBeenCalledOnce()
244 rendered.unmount()
245 })
246})