Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

QueryClientProvider.test.tsx

177 lines4.4 KB

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 })).toBeDefined()
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 })).toBeDefined()
98 expect(queryCache1.find({ queryKey: key2 })).not.toBeDefined()
99 expect(queryCache2.find({ queryKey: key1 })).not.toBeDefined()
100 expect(queryCache2.find({ queryKey: key2 })).toBeDefined()
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 })).toBeDefined()
139 expect(queryCache.find({ queryKey: key })?.options.gcTime).toBe(Infinity)
140 })
141​
142 describe('useQueryClient', () => {
143 it('should throw an error if no query client has been set', () => {
144 const consoleMock = vi
145 .spyOn(console, 'error')
146 .mockImplementation(() => undefined)
147​
148 function Page() {
149 useQueryClient()
150 return null
151 }
152​
153 expect(() => render(() => <Page />)).toThrow(
154 'No QueryClient set, use QueryClientProvider to set one',
155 )
156​
157 consoleMock.mockRestore()
158 })
159 })
160​
161 it('should not throw an error if user provides custom query client', () => {
162 const consoleMock = vi
163 .spyOn(console, 'error')
164 .mockImplementation(() => undefined)
165​
166 function Page() {
167 const client = new QueryClient()
168 useQueryClient(client)
169 return null
170 }
171​
172 render(() => <Page />)
173 expect(consoleMock).not.toHaveBeenCalled()
174​
175 consoleMock.mockRestore()
176 })
177})