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