Open History to see the last commit
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})