Open History to see the last commit
1import { cleanup, render, screen } from '@solidjs/testing-library'
2import { QueryClient, useQuery } from '@tanstack/solid-query'
3import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
4import { BroadcastQueryClientProvider } from '../BroadcastQueryClientProvider'
5β
6const mockState = vi.hoisted(() => {
7 const state: { resolveRestore?: () => void } = {}
8 const cleanupSession = vi.fn()
9 const restore = vi.fn(() => {
10 const restorePromise = new Promise<void>((resolve) => {
11 state.resolveRestore = resolve
12 })
13 return [cleanupSession, restorePromise] as const
14 })
15 return { cleanupSession, restore, state }
16})
17β
18vi.mock('@tanstack/query-broadcast-client-experimental', () => ({
19 broadcastQueryClientRestore: mockState.restore,
20}))
21β
22describe('BroadcastQueryClientProvider (solid)', () => {
23 beforeEach(() => {
24 mockState.cleanupSession.mockReset()
25 mockState.restore.mockClear()
26 mockState.state.resolveRestore = undefined
27 })
28β
29 afterEach(() => {
30 cleanup()
31 vi.restoreAllMocks()
32 })
33β
34 it('holds query observers until bootstrap completes', async () => {
35 const queryKey = ['solid-bootstrap']
36 const queryFn = vi.fn().mockResolvedValue('network')
37 const queryClient = new QueryClient()
38 queryClient.setQueryData(queryKey, 'cache', { updatedAt: Date.now() })
39β
40 function Page() {
41 const query = useQuery(() => ({
42 queryKey,
43 queryFn,
44 staleTime: Infinity,
45 }))
46 return <div>{query.data ?? query.fetchStatus}</div>
47 }
48β
49 render(() => (
50 <BroadcastQueryClientProvider
51 client={queryClient}
52 broadcastOptions={{ broadcastChannel: 'test-channel' }}
53 >
54 <Page />
55 </BroadcastQueryClientProvider>
56 ))
57β
58 expect(screen.getByText('cache')).toBeInTheDocument()
59 expect(queryFn).not.toHaveBeenCalled()
60β
61 await mockState.state.resolveRestore?.()
62 expect(queryFn).not.toHaveBeenCalled()
63 expect(mockState.cleanupSession).not.toHaveBeenCalled()
64 cleanup()
65 expect(mockState.cleanupSession).toHaveBeenCalledOnce()
66 })
67β
68 it('refetches a stale query after bootstrap completes', async () => {
69 const queryKey = ['solid-stale-bootstrap']
70 const queryFn = vi.fn().mockResolvedValue('network')
71 const queryClient = new QueryClient()
72 queryClient.setQueryData(queryKey, 'stale', { updatedAt: 0 })
73β
74 function Page() {
75 const query = useQuery(() => ({ queryKey, queryFn, staleTime: 0 }))
76 return <div>{query.data ?? query.fetchStatus}</div>
77 }
78β
79 render(() => (
80 <BroadcastQueryClientProvider
81 client={queryClient}
82 broadcastOptions={{ broadcastChannel: 'test-channel' }}
83 >
84 <Page />
85 </BroadcastQueryClientProvider>
86 ))
87β
88 await mockState.state.resolveRestore?.()
89 await Promise.resolve()
90β
91 expect(queryFn).toHaveBeenCalledOnce()
92 cleanup()
93 })
94})