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

queryClient.test.ts

384 lines9.5 KB

1import { describe, expect, test, vi } from 'vitest'
2import { ref } from 'vue-demi'
3import { QueryClient as QueryClientOrigin } from '@tanstack/query-core'
4import { QueryClient } from '../queryClient'
5import { infiniteQueryOptions } from '../infiniteQueryOptions'
6import { flushPromises } from './test-utils'
7​
8vi.mock('@tanstack/query-core')
9​
10const queryKeyRef = ['foo', ref('bar')]
11const queryKeyUnref = ['foo', 'bar']
12​
13const fn = () => 'mock'
14​
15describe('QueryCache', () => {
16 describe('isFetching', () => {
17 test('should properly unwrap 1 parameter', async () => {
18 const queryClient = new QueryClient()
19​
20 queryClient.isFetching({
21 queryKey: queryKeyRef,
22 })
23​
24 expect(QueryClientOrigin.prototype.isFetching).toBeCalledWith({
25 queryKey: queryKeyUnref,
26 })
27 })
28 })
29​
30 describe('isMutating', () => {
31 test('should properly unwrap 1 parameter', async () => {
32 const queryClient = new QueryClient()
33​
34 queryClient.isMutating({
35 mutationKey: queryKeyRef,
36 })
37​
38 expect(QueryClientOrigin.prototype.isMutating).toBeCalledWith({
39 mutationKey: queryKeyUnref,
40 })
41 })
42 })
43​
44 describe('getQueryData', () => {
45 test('should properly unwrap 1 parameter', async () => {
46 const queryClient = new QueryClient()
47​
48 queryClient.getQueryData(queryKeyRef)
49​
50 expect(QueryClientOrigin.prototype.getQueryData).toBeCalledWith(
51 queryKeyUnref,
52 )
53 })
54 })
55​
56 describe('getQueriesData', () => {
57 test('should properly unwrap queryKey param', async () => {
58 const queryClient = new QueryClient()
59​
60 queryClient.getQueriesData({ queryKey: queryKeyRef })
61​
62 expect(QueryClientOrigin.prototype.getQueriesData).toBeCalledWith({
63 queryKey: queryKeyUnref,
64 })
65 })
66​
67 test('should properly unwrap filters param', async () => {
68 const queryClient = new QueryClient()
69​
70 queryClient.getQueriesData({ queryKey: queryKeyRef })
71​
72 expect(QueryClientOrigin.prototype.getQueriesData).toBeCalledWith({
73 queryKey: queryKeyUnref,
74 })
75 })
76 })
77​
78 describe('setQueryData', () => {
79 test('should properly unwrap 3 parameter', async () => {
80 const queryClient = new QueryClient()
81​
82 queryClient.setQueryData(queryKeyRef, fn, {
83 updatedAt: ref(3),
84 })
85​
86 expect(QueryClientOrigin.prototype.setQueryData).toBeCalledWith(
87 queryKeyUnref,
88 fn,
89 { updatedAt: 3 },
90 )
91 })
92 })
93​
94 describe('setQueriesData', () => {
95 test('should properly unwrap params with queryKey', async () => {
96 const queryClient = new QueryClient()
97​
98 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
99 updatedAt: ref(3),
100 })
101​
102 expect(QueryClientOrigin.prototype.setQueriesData).toBeCalledWith(
103 { queryKey: queryKeyUnref },
104 fn,
105 { updatedAt: 3 },
106 )
107 })
108​
109 test('should properly unwrap params with filters', async () => {
110 const queryClient = new QueryClient()
111​
112 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
113 updatedAt: ref(3),
114 })
115​
116 expect(QueryClientOrigin.prototype.setQueriesData).toBeCalledWith(
117 { queryKey: queryKeyUnref },
118 fn,
119 { updatedAt: 3 },
120 )
121 })
122 })
123​
124 describe('getQueryState', () => {
125 test('should properly unwrap 1 parameter', async () => {
126 const queryClient = new QueryClient()
127​
128 queryClient.getQueryState(queryKeyRef)
129​
130 expect(QueryClientOrigin.prototype.getQueryState).toBeCalledWith(
131 queryKeyUnref,
132 )
133 })
134 })
135​
136 describe('removeQueries', () => {
137 test('should properly unwrap 1 parameter', async () => {
138 const queryClient = new QueryClient()
139​
140 queryClient.removeQueries({
141 queryKey: queryKeyRef,
142 })
143​
144 expect(QueryClientOrigin.prototype.removeQueries).toBeCalledWith({
145 queryKey: queryKeyUnref,
146 })
147 })
148 })
149​
150 describe('resetQueries', () => {
151 test('should properly unwrap 2 parameter', async () => {
152 const queryClient = new QueryClient()
153​
154 queryClient.resetQueries(
155 {
156 queryKey: queryKeyRef,
157 },
158 { cancelRefetch: ref(false) },
159 )
160​
161 expect(QueryClientOrigin.prototype.resetQueries).toBeCalledWith(
162 {
163 queryKey: queryKeyUnref,
164 },
165 { cancelRefetch: false },
166 )
167 })
168 })
169​
170 describe('cancelQueries', () => {
171 test('should properly unwrap 2 parameter', async () => {
172 const queryClient = new QueryClient()
173​
174 queryClient.cancelQueries(
175 {
176 queryKey: queryKeyRef,
177 },
178 { revert: ref(false) },
179 )
180​
181 expect(QueryClientOrigin.prototype.cancelQueries).toBeCalledWith(
182 {
183 queryKey: queryKeyUnref,
184 },
185 { revert: false },
186 )
187 })
188 })
189​
190 describe('invalidateQueries', () => {
191 test('should properly unwrap 2 parameter', async () => {
192 const queryClient = new QueryClient()
193​
194 queryClient.invalidateQueries(
195 {
196 queryKey: queryKeyRef,
197 },
198 { cancelRefetch: ref(false) },
199 )
200​
201 await flushPromises()
202​
203 expect(QueryClientOrigin.prototype.invalidateQueries).toBeCalledWith(
204 {
205 queryKey: queryKeyUnref,
206 },
207 { cancelRefetch: false },
208 )
209 })
210 })
211​
212 describe('refetchQueries', () => {
213 test('should properly unwrap 2 parameter', async () => {
214 const queryClient = new QueryClient()
215​
216 queryClient.refetchQueries(
217 {
218 queryKey: queryKeyRef,
219 },
220 { cancelRefetch: ref(false) },
221 )
222​
223 expect(QueryClientOrigin.prototype.refetchQueries).toBeCalledWith(
224 {
225 queryKey: queryKeyUnref,
226 },
227 { cancelRefetch: false },
228 )
229 })
230 })
231​
232 describe('fetchQuery', () => {
233 test('should properly unwrap parameter', async () => {
234 const queryClient = new QueryClient()
235​
236 queryClient.fetchQuery({
237 queryKey: queryKeyRef,
238 })
239​
240 expect(QueryClientOrigin.prototype.fetchQuery).toBeCalledWith({
241 queryKey: queryKeyUnref,
242 })
243 })
244 })
245​
246 describe('prefetchQuery', () => {
247 test('should properly unwrap parameters', async () => {
248 const queryClient = new QueryClient()
249​
250 queryClient.prefetchQuery({ queryKey: queryKeyRef, queryFn: fn })
251​
252 expect(QueryClientOrigin.prototype.prefetchQuery).toBeCalledWith({
253 queryKey: queryKeyUnref,
254 queryFn: fn,
255 })
256 })
257 })
258​
259 describe('fetchInfiniteQuery', () => {
260 test('should properly unwrap parameter', async () => {
261 const queryClient = new QueryClient()
262​
263 queryClient.fetchInfiniteQuery({
264 queryKey: queryKeyRef,
265 initialPageParam: 0,
266 })
267​
268 expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith({
269 initialPageParam: 0,
270 queryKey: queryKeyUnref,
271 })
272 })
273 test('should properly unwrap parameter using infiniteQueryOptions with unref', async () => {
274 const queryClient = new QueryClient()
275​
276 const options = infiniteQueryOptions({
277 queryKey: queryKeyUnref,
278 initialPageParam: 0,
279 getNextPageParam: () => 12,
280 })
281​
282 queryClient.fetchInfiniteQuery(options)
283​
284 expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith({
285 initialPageParam: 0,
286 queryKey: queryKeyUnref,
287 })
288 })
289 })
290​
291 describe('prefetchInfiniteQuery', () => {
292 test('should properly unwrap parameters', async () => {
293 const queryClient = new QueryClient()
294​
295 queryClient.prefetchInfiniteQuery({
296 queryKey: queryKeyRef,
297 queryFn: fn,
298 initialPageParam: 0,
299 })
300​
301 expect(QueryClientOrigin.prototype.prefetchInfiniteQuery).toBeCalledWith({
302 initialPageParam: 0,
303 queryKey: queryKeyUnref,
304 queryFn: fn,
305 })
306 })
307 })
308​
309 describe('setDefaultOptions', () => {
310 test('should properly unwrap parameters', async () => {
311 const queryClient = new QueryClient()
312​
313 queryClient.setDefaultOptions({
314 queries: {
315 enabled: ref(false),
316 },
317 })
318​
319 expect(QueryClientOrigin.prototype.setDefaultOptions).toBeCalledWith({
320 queries: {
321 enabled: false,
322 },
323 })
324 })
325 })
326​
327 describe('setQueryDefaults', () => {
328 test('should properly unwrap parameters', async () => {
329 const queryClient = new QueryClient()
330​
331 queryClient.setQueryDefaults(queryKeyRef, {
332 enabled: ref(false),
333 })
334​
335 expect(QueryClientOrigin.prototype.setQueryDefaults).toBeCalledWith(
336 queryKeyUnref,
337 {
338 enabled: false,
339 },
340 )
341 })
342 })
343​
344 describe('getQueryDefaults', () => {
345 test('should properly unwrap parameters', async () => {
346 const queryClient = new QueryClient()
347​
348 queryClient.getQueryDefaults(queryKeyRef)
349​
350 expect(QueryClientOrigin.prototype.getQueryDefaults).toBeCalledWith(
351 queryKeyUnref,
352 )
353 })
354 })
355​
356 describe('setMutationDefaults', () => {
357 test('should properly unwrap parameters', async () => {
358 const queryClient = new QueryClient()
359​
360 queryClient.setMutationDefaults(queryKeyRef, {
361 mutationKey: queryKeyRef,
362 })
363​
364 expect(QueryClientOrigin.prototype.setMutationDefaults).toBeCalledWith(
365 queryKeyUnref,
366 {
367 mutationKey: queryKeyUnref,
368 },
369 )
370 })
371 })
372​
373 describe('getMutationDefaults', () => {
374 test('should properly unwrap parameters', async () => {
375 const queryClient = new QueryClient()
376​
377 queryClient.getMutationDefaults(queryKeyRef)
378​
379 expect(QueryClientOrigin.prototype.getMutationDefaults).toBeCalledWith(
380 queryKeyUnref,
381 )
382 })
383 })
384})