@tanstack/vue-query 5.59.17 → 5.59.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,433 +0,0 @@
1
- import { describe, expect, test, vi } from 'vitest'
2
- import { ref } from 'vue-demi'
3
- import { QueryClient as QueryClientOrigin } from '@tanstack/query-core'
4
- import { QueryClient } from '../queryClient'
5
- import { infiniteQueryOptions } from '../infiniteQueryOptions'
6
- import { flushPromises } from './test-utils'
7
-
8
- vi.mock('@tanstack/query-core')
9
-
10
- const queryKeyRef = ['foo', ref('bar')]
11
- const queryKeyUnref = ['foo', 'bar']
12
-
13
- const fn = () => 'mock'
14
-
15
- describe('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', () => {
192
- const queryClient = new QueryClient()
193
-
194
- queryClient.invalidateQueries(
195
- {
196
- queryKey: queryKeyRef,
197
- },
198
- { cancelRefetch: ref(false) },
199
- )
200
-
201
- expect(QueryClientOrigin.prototype.invalidateQueries).toBeCalledWith(
202
- {
203
- queryKey: queryKeyUnref,
204
- refetchType: 'none',
205
- },
206
- { cancelRefetch: false },
207
- )
208
- })
209
-
210
- // #7694
211
- test('should call invalidateQueries immediately and refetchQueries after flushPromises', async () => {
212
- const invalidateQueries = vi.spyOn(
213
- QueryClientOrigin.prototype,
214
- 'invalidateQueries',
215
- )
216
- const refetchQueries = vi.spyOn(
217
- QueryClientOrigin.prototype,
218
- 'refetchQueries',
219
- )
220
-
221
- const queryClient = new QueryClient()
222
-
223
- queryClient.invalidateQueries({
224
- queryKey: queryKeyRef,
225
- })
226
-
227
- expect(invalidateQueries).toBeCalled()
228
- expect(refetchQueries).not.toBeCalled()
229
-
230
- await flushPromises()
231
-
232
- expect(refetchQueries).toBeCalled()
233
- })
234
-
235
- test('should call invalidateQueries immediately and not call refetchQueries', async () => {
236
- const invalidateQueries = vi.spyOn(
237
- QueryClientOrigin.prototype,
238
- 'invalidateQueries',
239
- )
240
- const refetchQueries = vi.spyOn(
241
- QueryClientOrigin.prototype,
242
- 'refetchQueries',
243
- )
244
-
245
- const queryClient = new QueryClient()
246
-
247
- queryClient.invalidateQueries({
248
- queryKey: queryKeyRef,
249
- refetchType: 'none',
250
- })
251
-
252
- expect(invalidateQueries).toBeCalled()
253
- expect(refetchQueries).not.toBeCalled()
254
-
255
- await flushPromises()
256
-
257
- expect(refetchQueries).not.toBeCalled()
258
- })
259
- })
260
-
261
- describe('refetchQueries', () => {
262
- test('should properly unwrap 2 parameter', async () => {
263
- const queryClient = new QueryClient()
264
-
265
- queryClient.refetchQueries(
266
- {
267
- queryKey: queryKeyRef,
268
- },
269
- { cancelRefetch: ref(false) },
270
- )
271
-
272
- expect(QueryClientOrigin.prototype.refetchQueries).toBeCalledWith(
273
- {
274
- queryKey: queryKeyUnref,
275
- },
276
- { cancelRefetch: false },
277
- )
278
- })
279
- })
280
-
281
- describe('fetchQuery', () => {
282
- test('should properly unwrap parameter', async () => {
283
- const queryClient = new QueryClient()
284
-
285
- queryClient.fetchQuery({
286
- queryKey: queryKeyRef,
287
- })
288
-
289
- expect(QueryClientOrigin.prototype.fetchQuery).toBeCalledWith({
290
- queryKey: queryKeyUnref,
291
- })
292
- })
293
- })
294
-
295
- describe('prefetchQuery', () => {
296
- test('should properly unwrap parameters', async () => {
297
- const queryClient = new QueryClient()
298
-
299
- queryClient.prefetchQuery({ queryKey: queryKeyRef, queryFn: fn })
300
-
301
- expect(QueryClientOrigin.prototype.prefetchQuery).toBeCalledWith({
302
- queryKey: queryKeyUnref,
303
- queryFn: fn,
304
- })
305
- })
306
- })
307
-
308
- describe('fetchInfiniteQuery', () => {
309
- test('should properly unwrap parameter', async () => {
310
- const queryClient = new QueryClient()
311
-
312
- queryClient.fetchInfiniteQuery({
313
- queryKey: queryKeyRef,
314
- initialPageParam: 0,
315
- })
316
-
317
- expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith({
318
- initialPageParam: 0,
319
- queryKey: queryKeyUnref,
320
- })
321
- })
322
- test('should properly unwrap parameter using infiniteQueryOptions with unref', async () => {
323
- const queryClient = new QueryClient()
324
-
325
- const options = infiniteQueryOptions({
326
- queryKey: queryKeyUnref,
327
- initialPageParam: 0,
328
- getNextPageParam: () => 12,
329
- })
330
-
331
- queryClient.fetchInfiniteQuery(options)
332
-
333
- expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith({
334
- initialPageParam: 0,
335
- queryKey: queryKeyUnref,
336
- })
337
- })
338
- })
339
-
340
- describe('prefetchInfiniteQuery', () => {
341
- test('should properly unwrap parameters', async () => {
342
- const queryClient = new QueryClient()
343
-
344
- queryClient.prefetchInfiniteQuery({
345
- queryKey: queryKeyRef,
346
- queryFn: fn,
347
- initialPageParam: 0,
348
- })
349
-
350
- expect(QueryClientOrigin.prototype.prefetchInfiniteQuery).toBeCalledWith({
351
- initialPageParam: 0,
352
- queryKey: queryKeyUnref,
353
- queryFn: fn,
354
- })
355
- })
356
- })
357
-
358
- describe('setDefaultOptions', () => {
359
- test('should properly unwrap parameters', async () => {
360
- const queryClient = new QueryClient()
361
-
362
- queryClient.setDefaultOptions({
363
- queries: {
364
- enabled: ref(false),
365
- },
366
- })
367
-
368
- expect(QueryClientOrigin.prototype.setDefaultOptions).toBeCalledWith({
369
- queries: {
370
- enabled: false,
371
- },
372
- })
373
- })
374
- })
375
-
376
- describe('setQueryDefaults', () => {
377
- test('should properly unwrap parameters', async () => {
378
- const queryClient = new QueryClient()
379
-
380
- queryClient.setQueryDefaults(queryKeyRef, {
381
- enabled: ref(false),
382
- })
383
-
384
- expect(QueryClientOrigin.prototype.setQueryDefaults).toBeCalledWith(
385
- queryKeyUnref,
386
- {
387
- enabled: false,
388
- },
389
- )
390
- })
391
- })
392
-
393
- describe('getQueryDefaults', () => {
394
- test('should properly unwrap parameters', async () => {
395
- const queryClient = new QueryClient()
396
-
397
- queryClient.getQueryDefaults(queryKeyRef)
398
-
399
- expect(QueryClientOrigin.prototype.getQueryDefaults).toBeCalledWith(
400
- queryKeyUnref,
401
- )
402
- })
403
- })
404
-
405
- describe('setMutationDefaults', () => {
406
- test('should properly unwrap parameters', async () => {
407
- const queryClient = new QueryClient()
408
-
409
- queryClient.setMutationDefaults(queryKeyRef, {
410
- mutationKey: queryKeyRef,
411
- })
412
-
413
- expect(QueryClientOrigin.prototype.setMutationDefaults).toBeCalledWith(
414
- queryKeyUnref,
415
- {
416
- mutationKey: queryKeyUnref,
417
- },
418
- )
419
- })
420
- })
421
-
422
- describe('getMutationDefaults', () => {
423
- test('should properly unwrap parameters', async () => {
424
- const queryClient = new QueryClient()
425
-
426
- queryClient.getMutationDefaults(queryKeyRef)
427
-
428
- expect(QueryClientOrigin.prototype.getMutationDefaults).toBeCalledWith(
429
- queryKeyUnref,
430
- )
431
- })
432
- })
433
- })
@@ -1,125 +0,0 @@
1
- import { describe, expectTypeOf, it } from 'vitest'
2
- import { reactive, ref } from 'vue-demi'
3
- import { dataTagSymbol } from '@tanstack/query-core'
4
- import { QueryClient } from '../queryClient'
5
- import { queryOptions } from '../queryOptions'
6
- import { useQuery } from '../useQuery'
7
-
8
- describe('queryOptions', () => {
9
- it('should not allow excess properties', () => {
10
- queryOptions({
11
- queryKey: ['key'],
12
- queryFn: () => Promise.resolve(5),
13
- // @ts-expect-error this is a good error, because stallTime does not exist!
14
- stallTime: 1000,
15
- })
16
- })
17
- it('should infer types for callbacks', () => {
18
- queryOptions({
19
- queryKey: ['key'],
20
- queryFn: () => Promise.resolve(5),
21
- staleTime: 1000,
22
- select: (data) => {
23
- expectTypeOf(data).toEqualTypeOf<number>()
24
- },
25
- })
26
- })
27
- it('should work when passed to useQuery', () => {
28
- const options = queryOptions({
29
- queryKey: ['key'],
30
- queryFn: () => Promise.resolve(5),
31
- })
32
-
33
- const { data } = reactive(useQuery(options))
34
- expectTypeOf(data).toEqualTypeOf<number | undefined>()
35
- })
36
- it('should tag the queryKey with the result type of the QueryFn', () => {
37
- const { queryKey } = queryOptions({
38
- queryKey: ['key'],
39
- queryFn: () => Promise.resolve(5),
40
- })
41
-
42
- expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
43
- })
44
- it('should tag the queryKey even if no promise is returned', () => {
45
- const { queryKey } = queryOptions({
46
- queryKey: ['key'],
47
- queryFn: () => 5,
48
- })
49
-
50
- expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
51
- })
52
- it('should tag the queryKey with unknown if there is no queryFn', () => {
53
- const { queryKey } = queryOptions({
54
- queryKey: ['key'],
55
- })
56
-
57
- expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<unknown>()
58
- })
59
- it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
60
- const { queryKey } = queryOptions({
61
- queryKey: ['key'],
62
- queryFn: () => Promise.resolve(5),
63
- select: (data) => data.toString(),
64
- })
65
-
66
- expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
67
- })
68
- it('should return the proper type when passed to getQueryData', () => {
69
- const { queryKey } = queryOptions({
70
- queryKey: ['key'],
71
- queryFn: () => Promise.resolve(5),
72
- })
73
-
74
- const queryClient = new QueryClient()
75
- const data = queryClient.getQueryData(queryKey)
76
-
77
- expectTypeOf(data).toEqualTypeOf<number | undefined>()
78
- })
79
- it('should properly type updaterFn when passed to setQueryData', () => {
80
- const { queryKey } = queryOptions({
81
- queryKey: ['key'],
82
- queryFn: () => Promise.resolve(5),
83
- })
84
-
85
- const queryClient = new QueryClient()
86
- const data = queryClient.setQueryData(queryKey, (prev) => {
87
- expectTypeOf(prev).toEqualTypeOf<number | undefined>()
88
- return prev
89
- })
90
- expectTypeOf(data).toEqualTypeOf<number | undefined>()
91
- })
92
- it('should properly type value when passed to setQueryData', () => {
93
- const { queryKey } = queryOptions({
94
- queryKey: ['key'],
95
- queryFn: () => Promise.resolve(5),
96
- })
97
-
98
- const queryClient = new QueryClient()
99
-
100
- // @ts-expect-error value should be a number
101
- queryClient.setQueryData(queryKey, '5')
102
- // @ts-expect-error value should be a number
103
- queryClient.setQueryData(queryKey, () => '5')
104
-
105
- const data = queryClient.setQueryData(queryKey, 5)
106
-
107
- expectTypeOf(data).toEqualTypeOf<number | undefined>()
108
- })
109
- it('should allow to be passed to QueryClient methods while containing ref in queryKey', () => {
110
- const options = queryOptions({
111
- queryKey: ['key', ref(1), { nested: ref(2) }],
112
- queryFn: () => Promise.resolve(5),
113
- })
114
-
115
- const queryClient = new QueryClient()
116
-
117
- // Should not error
118
- const data = queryClient.invalidateQueries(options)
119
- // Should not error
120
- const data2 = queryClient.fetchQuery(options)
121
-
122
- expectTypeOf(data).toEqualTypeOf<Promise<void>>()
123
- expectTypeOf(data2).toEqualTypeOf<Promise<number>>()
124
- })
125
- })
@@ -1,52 +0,0 @@
1
- /* istanbul ignore file */
2
-
3
- export function flushPromises(timeout = 0): Promise<unknown> {
4
- return new Promise(function (resolve) {
5
- setTimeout(resolve, timeout)
6
- })
7
- }
8
-
9
- export function simpleFetcher(): Promise<string> {
10
- return new Promise((resolve) => {
11
- setTimeout(() => {
12
- return resolve('Some data')
13
- }, 0)
14
- })
15
- }
16
-
17
- export function getSimpleFetcherWithReturnData(returnData: unknown) {
18
- return () =>
19
- new Promise((resolve) => setTimeout(() => resolve(returnData), 0))
20
- }
21
-
22
- export function infiniteFetcher({
23
- pageParam,
24
- }: {
25
- pageParam?: number
26
- }): Promise<string> {
27
- return new Promise((resolve) => {
28
- setTimeout(() => {
29
- return resolve('data on page ' + pageParam)
30
- }, 0)
31
- })
32
- }
33
-
34
- export function rejectFetcher(): Promise<Error> {
35
- return new Promise((_, reject) => {
36
- setTimeout(() => {
37
- return reject(new Error('Some error'))
38
- }, 0)
39
- })
40
- }
41
-
42
- export function successMutator<T>(param: T): Promise<T> {
43
- return new Promise((resolve) => {
44
- setTimeout(() => {
45
- return resolve(param)
46
- }, 0)
47
- })
48
- }
49
-
50
- export function errorMutator<T>(_: T): Promise<Error> {
51
- return rejectFetcher()
52
- }
@@ -1,84 +0,0 @@
1
- import { describe, expectTypeOf, it } from 'vitest'
2
- import { reactive } from 'vue-demi'
3
- import { useInfiniteQuery } from '../useInfiniteQuery'
4
- import { simpleFetcher } from './test-utils'
5
- import type { InfiniteData } from '@tanstack/query-core'
6
-
7
- describe('Discriminated union return type', () => {
8
- it('data should be possibly undefined by default', () => {
9
- const query = reactive(
10
- useInfiniteQuery({
11
- queryKey: ['infiniteQuery'],
12
- queryFn: simpleFetcher,
13
- getNextPageParam: () => undefined,
14
- initialPageParam: 0,
15
- }),
16
- )
17
-
18
- // TODO: Order of generics prevents pageParams to be typed correctly. Using `unknown` for now
19
- expectTypeOf(query.data).toEqualTypeOf<
20
- InfiniteData<string, unknown> | undefined
21
- >()
22
- })
23
-
24
- it('data should be defined when query is success', () => {
25
- const query = reactive(
26
- useInfiniteQuery({
27
- queryKey: ['infiniteQuery'],
28
- queryFn: simpleFetcher,
29
- getNextPageParam: () => undefined,
30
- initialPageParam: 0,
31
- }),
32
- )
33
-
34
- if (query.isSuccess) {
35
- // TODO: Order of generics prevents pageParams to be typed correctly. Using `unknown` for now
36
- expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
37
- }
38
- })
39
-
40
- it('error should be null when query is success', () => {
41
- const query = reactive(
42
- useInfiniteQuery({
43
- queryKey: ['infiniteQuery'],
44
- queryFn: simpleFetcher,
45
- getNextPageParam: () => undefined,
46
- initialPageParam: 0,
47
- }),
48
- )
49
-
50
- if (query.isSuccess) {
51
- expectTypeOf(query.error).toEqualTypeOf<null>()
52
- }
53
- })
54
-
55
- it('data should be undefined when query is pending', () => {
56
- const query = reactive(
57
- useInfiniteQuery({
58
- queryKey: ['infiniteQuery'],
59
- queryFn: simpleFetcher,
60
- getNextPageParam: () => undefined,
61
- initialPageParam: 0,
62
- }),
63
- )
64
-
65
- if (query.isPending) {
66
- expectTypeOf(query.data).toEqualTypeOf<undefined>()
67
- }
68
- })
69
-
70
- it('error should be defined when query is error', () => {
71
- const query = reactive(
72
- useInfiniteQuery({
73
- queryKey: ['infiniteQuery'],
74
- queryFn: simpleFetcher,
75
- getNextPageParam: () => undefined,
76
- initialPageParam: 0,
77
- }),
78
- )
79
-
80
- if (query.isError) {
81
- expectTypeOf(query.error).toEqualTypeOf<Error>()
82
- }
83
- })
84
- })