Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { render } from '@solidjs/testing-library'
3import { QueryCache } from '@tanstack/query-core'
4import { queryKey, sleep } from '@tanstack/query-test-utils'
5import { QueryClient, QueryClientProvider, useQuery, useQueryClient } from '..'
6β
7describe('QueryClientProvider', () => {
8 beforeEach(() => {
9 vi.useFakeTimers()
10 })
11β
12 afterEach(() => {
13 vi.useRealTimers()
14 })
15β
16 it('sets a specific cache for all queries to use', async () => {
17 const key = queryKey()
18β
19 const queryCache = new QueryCache()
20 const queryClient = new QueryClient({ queryCache })
21β
22 function Page() {
23 const query = useQuery(() => ({
24 queryKey: key,
25 queryFn: () => sleep(10).then(() => 'test'),
26 }))
27β
28 return (
29 <div>
30 <h1>{query.data}</h1>
31 </div>
32 )
33 }
34β
35 const rendered = render(() => (
36 <QueryClientProvider client={queryClient}>
37 <Page />
38 </QueryClientProvider>
39 ))
40β
41 await vi.advanceTimersByTimeAsync(10)
42 expect(rendered.getByText('test')).toBeInTheDocument()
43β
44 expect(queryCache.find({ queryKey: key })?.state.data).toBe('test')
45 })
46β
47 it('allows multiple caches to be partitioned', async () => {
48 const key1 = queryKey()
49 const key2 = queryKey()
50β
51 const queryCache1 = new QueryCache()
52 const queryCache2 = new QueryCache()
53β
54 const queryClient1 = new QueryClient({ queryCache: queryCache1 })
55 const queryClient2 = new QueryClient({ queryCache: queryCache2 })
56β
57 function Page1() {
58 const query = useQuery(() => ({
59 queryKey: key1,
60 queryFn: () => sleep(10).then(() => 'test1'),
61 }))
62β
63 return (
64 <div>
65 <h1>{query.data}</h1>
66 </div>
67 )
68 }
69 function Page2() {
70 const query = useQuery(() => ({
71 queryKey: key2,
72 queryFn: () => sleep(10).then(() => 'test2'),
73 }))
74β
75 return (
76 <div>
77 <h1>{query.data}</h1>
78 </div>
79 )
80 }
81β
82 const rendered = render(() => (
83 <>
84 <QueryClientProvider client={queryClient1}>
85 <Page1 />
86 </QueryClientProvider>
87 <QueryClientProvider client={queryClient2}>
88 <Page2 />
89 </QueryClientProvider>
90 </>
91 ))
92β
93 await vi.advanceTimersByTimeAsync(10)
94 expect(rendered.getByText('test1')).toBeInTheDocument()
95 expect(rendered.getByText('test2')).toBeInTheDocument()
96β
97 expect(queryCache1.find({ queryKey: key1 })?.state.data).toBe('test1')
98 expect(queryCache1.find({ queryKey: key2 })).toBeUndefined()
99 expect(queryCache2.find({ queryKey: key1 })).toBeUndefined()
100 expect(queryCache2.find({ queryKey: key2 })?.state.data).toBe('test2')
101 })
102β
103 it("uses defaultOptions for queries when they don't provide their own config", async () => {
104 const key = queryKey()
105β
106 const queryCache = new QueryCache()
107 const queryClient = new QueryClient({
108 queryCache,
109 defaultOptions: {
110 queries: {
111 gcTime: Infinity,
112 },
113 },
114 })
115β
116 function Page() {
117 const query = useQuery(() => ({
118 queryKey: key,
119 queryFn: () => sleep(10).then(() => 'test'),
120 }))
121β
122 return (
123 <div>
124 <h1>{query.data}</h1>
125 </div>
126 )
127 }
128β
129 const rendered = render(() => (
130 <QueryClientProvider client={queryClient}>
131 <Page />
132 </QueryClientProvider>
133 ))
134β
135 await vi.advanceTimersByTimeAsync(10)
136 expect(rendered.getByText('test')).toBeInTheDocument()
137β
138 expect(queryCache.find({ queryKey: key })?.options.gcTime).toBe(Infinity)
139 })
140β
141 describe('useQueryClient', () => {
142 it('should throw an error if no query client has been set', () => {
143 const consoleMock = vi
144 .spyOn(console, 'error')
145 .mockImplementation(() => undefined)
146β
147 function Page() {
148 useQueryClient()
149 return null
150 }
151β
152 expect(() => render(() => <Page />)).toThrow(
153 'No QueryClient set, use QueryClientProvider to set one',
154 )
155β
156 consoleMock.mockRestore()
157 })
158 })
159β
160 it('should not throw an error if user provides custom query client', () => {
161 const consoleMock = vi
162 .spyOn(console, 'error')
163 .mockImplementation(() => undefined)
164β
165 function Page() {
166 const client = new QueryClient()
167 useQueryClient(client)
168 return null
169 }
170β
171 render(() => <Page />)
172 expect(consoleMock).not.toHaveBeenCalled()
173β
174 consoleMock.mockRestore()
175 })
176})