@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.
- package/package.json +4 -3
- package/src/__tests__/infiniteQueryOptions.test-d.ts +0 -111
- package/src/__tests__/mutationCache.test.ts +0 -39
- package/src/__tests__/queryCache.test.ts +0 -47
- package/src/__tests__/queryClient.test-d.ts +0 -132
- package/src/__tests__/queryClient.test.ts +0 -433
- package/src/__tests__/queryOptions.test-d.ts +0 -125
- package/src/__tests__/test-utils.ts +0 -52
- package/src/__tests__/useInfiniteQuery.test-d.tsx +0 -84
- package/src/__tests__/useInfiniteQuery.test.ts +0 -69
- package/src/__tests__/useIsFetching.test.ts +0 -76
- package/src/__tests__/useIsMutating.test.ts +0 -122
- package/src/__tests__/useMutation.test-d.tsx +0 -74
- package/src/__tests__/useMutation.test.ts +0 -355
- package/src/__tests__/useQueries.test-d.ts +0 -222
- package/src/__tests__/useQueries.test.ts +0 -370
- package/src/__tests__/useQuery.test-d.ts +0 -271
- package/src/__tests__/useQuery.test.ts +0 -497
- package/src/__tests__/useQueryClient.test.ts +0 -51
- package/src/__tests__/utils.test.ts +0 -155
- package/src/__tests__/vueQueryPlugin.test.ts +0 -393
|
@@ -1,497 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test, vi } from 'vitest'
|
|
2
|
-
import {
|
|
3
|
-
computed,
|
|
4
|
-
getCurrentInstance,
|
|
5
|
-
onScopeDispose,
|
|
6
|
-
reactive,
|
|
7
|
-
ref,
|
|
8
|
-
} from 'vue-demi'
|
|
9
|
-
import { QueryObserver } from '@tanstack/query-core'
|
|
10
|
-
import { useQuery } from '../useQuery'
|
|
11
|
-
import { useBaseQuery } from '../useBaseQuery'
|
|
12
|
-
import {
|
|
13
|
-
flushPromises,
|
|
14
|
-
getSimpleFetcherWithReturnData,
|
|
15
|
-
rejectFetcher,
|
|
16
|
-
simpleFetcher,
|
|
17
|
-
} from './test-utils'
|
|
18
|
-
import type { Mock, MockedFunction } from 'vitest'
|
|
19
|
-
|
|
20
|
-
vi.mock('../useQueryClient')
|
|
21
|
-
vi.mock('../useBaseQuery')
|
|
22
|
-
|
|
23
|
-
describe('useQuery', () => {
|
|
24
|
-
test('should properly execute query', () => {
|
|
25
|
-
useQuery({ queryKey: ['key0'], queryFn: simpleFetcher, staleTime: 1000 })
|
|
26
|
-
|
|
27
|
-
expect(useBaseQuery).toBeCalledWith(
|
|
28
|
-
QueryObserver,
|
|
29
|
-
{
|
|
30
|
-
queryKey: ['key0'],
|
|
31
|
-
queryFn: simpleFetcher,
|
|
32
|
-
staleTime: 1000,
|
|
33
|
-
},
|
|
34
|
-
undefined,
|
|
35
|
-
)
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
test('should return pending status initially', () => {
|
|
39
|
-
const query = useQuery({ queryKey: ['key1'], queryFn: simpleFetcher })
|
|
40
|
-
|
|
41
|
-
expect(query).toMatchObject({
|
|
42
|
-
status: { value: 'pending' },
|
|
43
|
-
isPending: { value: true },
|
|
44
|
-
isFetching: { value: true },
|
|
45
|
-
isStale: { value: true },
|
|
46
|
-
})
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
test('should resolve to success and update reactive state: useQuery(key, dataFn)', async () => {
|
|
50
|
-
const query = useQuery({
|
|
51
|
-
queryKey: ['key2'],
|
|
52
|
-
queryFn: getSimpleFetcherWithReturnData('result2'),
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
await flushPromises()
|
|
56
|
-
|
|
57
|
-
expect(query).toMatchObject({
|
|
58
|
-
status: { value: 'success' },
|
|
59
|
-
data: { value: 'result2' },
|
|
60
|
-
isPending: { value: false },
|
|
61
|
-
isFetching: { value: false },
|
|
62
|
-
isFetched: { value: true },
|
|
63
|
-
isSuccess: { value: true },
|
|
64
|
-
})
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
test('should resolve to success and update reactive state: useQuery(optionsObj)', async () => {
|
|
68
|
-
const query = useQuery({
|
|
69
|
-
queryKey: ['key31'],
|
|
70
|
-
queryFn: getSimpleFetcherWithReturnData('result31'),
|
|
71
|
-
enabled: true,
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
await flushPromises()
|
|
75
|
-
|
|
76
|
-
expect(query).toMatchObject({
|
|
77
|
-
status: { value: 'success' },
|
|
78
|
-
data: { value: 'result31' },
|
|
79
|
-
isPending: { value: false },
|
|
80
|
-
isFetching: { value: false },
|
|
81
|
-
isFetched: { value: true },
|
|
82
|
-
isSuccess: { value: true },
|
|
83
|
-
})
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
test('should resolve to success and update reactive state: useQuery(key, optionsObj)', async () => {
|
|
87
|
-
const query = useQuery({
|
|
88
|
-
queryKey: ['key32'],
|
|
89
|
-
queryFn: getSimpleFetcherWithReturnData('result32'),
|
|
90
|
-
enabled: true,
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
await flushPromises()
|
|
94
|
-
|
|
95
|
-
expect(query).toMatchObject({
|
|
96
|
-
status: { value: 'success' },
|
|
97
|
-
data: { value: 'result32' },
|
|
98
|
-
isPending: { value: false },
|
|
99
|
-
isFetching: { value: false },
|
|
100
|
-
isFetched: { value: true },
|
|
101
|
-
isSuccess: { value: true },
|
|
102
|
-
})
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
test('should reject and update reactive state', async () => {
|
|
106
|
-
const query = useQuery({
|
|
107
|
-
queryKey: ['key3'],
|
|
108
|
-
queryFn: rejectFetcher,
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
await flushPromises()
|
|
112
|
-
|
|
113
|
-
expect(query).toMatchObject({
|
|
114
|
-
status: { value: 'error' },
|
|
115
|
-
data: { value: undefined },
|
|
116
|
-
error: { value: { message: 'Some error' } },
|
|
117
|
-
isPending: { value: false },
|
|
118
|
-
isFetching: { value: false },
|
|
119
|
-
isFetched: { value: true },
|
|
120
|
-
isError: { value: true },
|
|
121
|
-
failureCount: { value: 1 },
|
|
122
|
-
failureReason: { value: { message: 'Some error' } },
|
|
123
|
-
})
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
test('should update query on reactive (Ref) key change', async () => {
|
|
127
|
-
const secondKeyRef = ref('key7')
|
|
128
|
-
const query = useQuery({
|
|
129
|
-
queryKey: ['key6', secondKeyRef],
|
|
130
|
-
queryFn: simpleFetcher,
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
await flushPromises()
|
|
134
|
-
|
|
135
|
-
expect(query).toMatchObject({
|
|
136
|
-
status: { value: 'success' },
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
secondKeyRef.value = 'key8'
|
|
140
|
-
await flushPromises()
|
|
141
|
-
|
|
142
|
-
expect(query).toMatchObject({
|
|
143
|
-
status: { value: 'pending' },
|
|
144
|
-
data: { value: undefined },
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
await flushPromises()
|
|
148
|
-
|
|
149
|
-
expect(query).toMatchObject({
|
|
150
|
-
status: { value: 'success' },
|
|
151
|
-
})
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
test("should update query when an option is passed as Ref and it's changed", async () => {
|
|
155
|
-
const enabled = ref(false)
|
|
156
|
-
const query = useQuery({
|
|
157
|
-
queryKey: ['key9'],
|
|
158
|
-
queryFn: simpleFetcher,
|
|
159
|
-
enabled,
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
await flushPromises()
|
|
163
|
-
|
|
164
|
-
expect(query).toMatchObject({
|
|
165
|
-
fetchStatus: { value: 'idle' },
|
|
166
|
-
data: { value: undefined },
|
|
167
|
-
})
|
|
168
|
-
|
|
169
|
-
enabled.value = true
|
|
170
|
-
|
|
171
|
-
await flushPromises()
|
|
172
|
-
|
|
173
|
-
expect(query).toMatchObject({
|
|
174
|
-
fetchStatus: { value: 'fetching' },
|
|
175
|
-
data: { value: undefined },
|
|
176
|
-
})
|
|
177
|
-
|
|
178
|
-
await flushPromises()
|
|
179
|
-
|
|
180
|
-
expect(query).toMatchObject({
|
|
181
|
-
status: { value: 'success' },
|
|
182
|
-
})
|
|
183
|
-
})
|
|
184
|
-
|
|
185
|
-
test('should properly execute dependant queries', async () => {
|
|
186
|
-
const { data } = useQuery({
|
|
187
|
-
queryKey: ['dependant1'],
|
|
188
|
-
queryFn: simpleFetcher,
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
const enabled = computed(() => !!data.value)
|
|
192
|
-
|
|
193
|
-
const dependentQueryFn = vi.fn().mockImplementation(simpleFetcher)
|
|
194
|
-
const { fetchStatus, status } = useQuery(
|
|
195
|
-
reactive({
|
|
196
|
-
queryKey: ['dependant2'],
|
|
197
|
-
queryFn: dependentQueryFn,
|
|
198
|
-
enabled,
|
|
199
|
-
}),
|
|
200
|
-
)
|
|
201
|
-
|
|
202
|
-
expect(data.value).toStrictEqual(undefined)
|
|
203
|
-
expect(fetchStatus.value).toStrictEqual('idle')
|
|
204
|
-
expect(dependentQueryFn).not.toHaveBeenCalled()
|
|
205
|
-
|
|
206
|
-
await flushPromises()
|
|
207
|
-
|
|
208
|
-
expect(data.value).toStrictEqual('Some data')
|
|
209
|
-
expect(fetchStatus.value).toStrictEqual('fetching')
|
|
210
|
-
|
|
211
|
-
await flushPromises()
|
|
212
|
-
|
|
213
|
-
expect(fetchStatus.value).toStrictEqual('idle')
|
|
214
|
-
expect(status.value).toStrictEqual('success')
|
|
215
|
-
expect(dependentQueryFn).toHaveBeenCalledTimes(1)
|
|
216
|
-
expect(dependentQueryFn).toHaveBeenCalledWith(
|
|
217
|
-
expect.objectContaining({ queryKey: ['dependant2'] }),
|
|
218
|
-
)
|
|
219
|
-
})
|
|
220
|
-
|
|
221
|
-
test('should stop listening to changes on onScopeDispose', async () => {
|
|
222
|
-
const onScopeDisposeMock = onScopeDispose as MockedFunction<
|
|
223
|
-
typeof onScopeDispose
|
|
224
|
-
>
|
|
225
|
-
onScopeDisposeMock.mockImplementationOnce((fn) => fn())
|
|
226
|
-
|
|
227
|
-
const { status } = useQuery({
|
|
228
|
-
queryKey: ['onScopeDispose'],
|
|
229
|
-
queryFn: simpleFetcher,
|
|
230
|
-
})
|
|
231
|
-
|
|
232
|
-
expect(status.value).toStrictEqual('pending')
|
|
233
|
-
|
|
234
|
-
await flushPromises()
|
|
235
|
-
|
|
236
|
-
expect(status.value).toStrictEqual('pending')
|
|
237
|
-
|
|
238
|
-
await flushPromises()
|
|
239
|
-
|
|
240
|
-
expect(status.value).toStrictEqual('pending')
|
|
241
|
-
})
|
|
242
|
-
|
|
243
|
-
test('should use the current value for the queryKey when refetch is called', async () => {
|
|
244
|
-
const fetchFn = vi.fn()
|
|
245
|
-
const keyRef = ref('key11')
|
|
246
|
-
const query = useQuery({
|
|
247
|
-
queryKey: ['key10', keyRef],
|
|
248
|
-
queryFn: fetchFn,
|
|
249
|
-
enabled: false,
|
|
250
|
-
})
|
|
251
|
-
|
|
252
|
-
expect(fetchFn).not.toHaveBeenCalled()
|
|
253
|
-
await query.refetch()
|
|
254
|
-
expect(fetchFn).toHaveBeenCalledTimes(1)
|
|
255
|
-
expect(fetchFn).toHaveBeenCalledWith(
|
|
256
|
-
expect.objectContaining({
|
|
257
|
-
queryKey: ['key10', 'key11'],
|
|
258
|
-
}),
|
|
259
|
-
)
|
|
260
|
-
|
|
261
|
-
keyRef.value = 'key12'
|
|
262
|
-
await query.refetch()
|
|
263
|
-
expect(fetchFn).toHaveBeenCalledTimes(2)
|
|
264
|
-
expect(fetchFn).toHaveBeenCalledWith(
|
|
265
|
-
expect.objectContaining({
|
|
266
|
-
queryKey: ['key10', 'key12'],
|
|
267
|
-
}),
|
|
268
|
-
)
|
|
269
|
-
})
|
|
270
|
-
|
|
271
|
-
test('should be `enabled` to accept getter function', async () => {
|
|
272
|
-
const fetchFn = vi.fn()
|
|
273
|
-
const checked = ref(false)
|
|
274
|
-
|
|
275
|
-
useQuery({
|
|
276
|
-
queryKey: ['enabled'],
|
|
277
|
-
queryFn: fetchFn,
|
|
278
|
-
enabled: () => checked.value,
|
|
279
|
-
})
|
|
280
|
-
|
|
281
|
-
expect(fetchFn).not.toHaveBeenCalled()
|
|
282
|
-
|
|
283
|
-
checked.value = true
|
|
284
|
-
|
|
285
|
-
await flushPromises()
|
|
286
|
-
|
|
287
|
-
expect(fetchFn).toHaveBeenCalled()
|
|
288
|
-
})
|
|
289
|
-
|
|
290
|
-
test('should allow getters for query keys', async () => {
|
|
291
|
-
const fetchFn = vi.fn()
|
|
292
|
-
const key1 = ref('key1')
|
|
293
|
-
const key2 = ref('key2')
|
|
294
|
-
|
|
295
|
-
useQuery({
|
|
296
|
-
queryKey: ['key', () => key1.value, () => key2.value],
|
|
297
|
-
queryFn: fetchFn,
|
|
298
|
-
})
|
|
299
|
-
|
|
300
|
-
expect(fetchFn).toHaveBeenCalledTimes(1)
|
|
301
|
-
|
|
302
|
-
key1.value = 'key3'
|
|
303
|
-
|
|
304
|
-
await flushPromises()
|
|
305
|
-
|
|
306
|
-
expect(fetchFn).toHaveBeenCalledTimes(2)
|
|
307
|
-
|
|
308
|
-
key2.value = 'key4'
|
|
309
|
-
|
|
310
|
-
await flushPromises()
|
|
311
|
-
|
|
312
|
-
expect(fetchFn).toHaveBeenCalledTimes(3)
|
|
313
|
-
})
|
|
314
|
-
|
|
315
|
-
test('should allow arbitrarily nested getters for query keys', async () => {
|
|
316
|
-
const fetchFn = vi.fn()
|
|
317
|
-
const key1 = ref('key1')
|
|
318
|
-
const key2 = ref('key2')
|
|
319
|
-
const key3 = ref('key3')
|
|
320
|
-
const key4 = ref('key4')
|
|
321
|
-
const key5 = ref('key5')
|
|
322
|
-
|
|
323
|
-
useQuery({
|
|
324
|
-
queryKey: [
|
|
325
|
-
'key',
|
|
326
|
-
key1,
|
|
327
|
-
() => key2.value,
|
|
328
|
-
{ key: () => key3.value },
|
|
329
|
-
[{ foo: { bar: () => key4.value } }],
|
|
330
|
-
() => ({
|
|
331
|
-
foo: {
|
|
332
|
-
bar: {
|
|
333
|
-
baz: () => key5.value,
|
|
334
|
-
},
|
|
335
|
-
},
|
|
336
|
-
}),
|
|
337
|
-
],
|
|
338
|
-
queryFn: fetchFn,
|
|
339
|
-
})
|
|
340
|
-
|
|
341
|
-
expect(fetchFn).toHaveBeenCalledTimes(1)
|
|
342
|
-
|
|
343
|
-
key1.value = 'key1-updated'
|
|
344
|
-
|
|
345
|
-
await flushPromises()
|
|
346
|
-
|
|
347
|
-
expect(fetchFn).toHaveBeenCalledTimes(2)
|
|
348
|
-
|
|
349
|
-
key2.value = 'key2-updated'
|
|
350
|
-
|
|
351
|
-
await flushPromises()
|
|
352
|
-
|
|
353
|
-
expect(fetchFn).toHaveBeenCalledTimes(3)
|
|
354
|
-
|
|
355
|
-
key3.value = 'key3-updated'
|
|
356
|
-
|
|
357
|
-
await flushPromises()
|
|
358
|
-
|
|
359
|
-
expect(fetchFn).toHaveBeenCalledTimes(4)
|
|
360
|
-
|
|
361
|
-
key4.value = 'key4-updated'
|
|
362
|
-
|
|
363
|
-
await flushPromises()
|
|
364
|
-
|
|
365
|
-
expect(fetchFn).toHaveBeenCalledTimes(5)
|
|
366
|
-
|
|
367
|
-
key5.value = 'key5-updated'
|
|
368
|
-
|
|
369
|
-
await flushPromises()
|
|
370
|
-
|
|
371
|
-
expect(fetchFn).toHaveBeenCalledTimes(6)
|
|
372
|
-
})
|
|
373
|
-
|
|
374
|
-
describe('throwOnError', () => {
|
|
375
|
-
test('should evaluate throwOnError when query is expected to throw', async () => {
|
|
376
|
-
const boundaryFn = vi.fn()
|
|
377
|
-
useQuery({
|
|
378
|
-
queryKey: ['key0'],
|
|
379
|
-
queryFn: rejectFetcher,
|
|
380
|
-
retry: false,
|
|
381
|
-
throwOnError: boundaryFn,
|
|
382
|
-
})
|
|
383
|
-
|
|
384
|
-
await flushPromises()
|
|
385
|
-
|
|
386
|
-
expect(boundaryFn).toHaveBeenCalledTimes(1)
|
|
387
|
-
expect(boundaryFn).toHaveBeenCalledWith(
|
|
388
|
-
Error('Some error'),
|
|
389
|
-
expect.objectContaining({
|
|
390
|
-
state: expect.objectContaining({ status: 'error' }),
|
|
391
|
-
}),
|
|
392
|
-
)
|
|
393
|
-
})
|
|
394
|
-
})
|
|
395
|
-
|
|
396
|
-
describe('suspense', () => {
|
|
397
|
-
test('should return a Promise', () => {
|
|
398
|
-
const getCurrentInstanceSpy = getCurrentInstance as Mock
|
|
399
|
-
getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }))
|
|
400
|
-
|
|
401
|
-
const query = useQuery({ queryKey: ['suspense'], queryFn: simpleFetcher })
|
|
402
|
-
const result = query.suspense()
|
|
403
|
-
|
|
404
|
-
expect(result).toBeInstanceOf(Promise)
|
|
405
|
-
})
|
|
406
|
-
|
|
407
|
-
test('should resolve after being enabled', () => {
|
|
408
|
-
const getCurrentInstanceSpy = getCurrentInstance as Mock
|
|
409
|
-
getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }))
|
|
410
|
-
|
|
411
|
-
let afterTimeout = false
|
|
412
|
-
const isEnabled = ref(false)
|
|
413
|
-
const query = useQuery({
|
|
414
|
-
queryKey: ['suspense2'],
|
|
415
|
-
queryFn: simpleFetcher,
|
|
416
|
-
enabled: isEnabled,
|
|
417
|
-
})
|
|
418
|
-
|
|
419
|
-
setTimeout(() => {
|
|
420
|
-
afterTimeout = true
|
|
421
|
-
isEnabled.value = true
|
|
422
|
-
}, 200)
|
|
423
|
-
|
|
424
|
-
return query.suspense().then(() => {
|
|
425
|
-
expect(afterTimeout).toBe(true)
|
|
426
|
-
})
|
|
427
|
-
})
|
|
428
|
-
|
|
429
|
-
test('should resolve immediately when stale without refetching', () => {
|
|
430
|
-
const getCurrentInstanceSpy = getCurrentInstance as Mock
|
|
431
|
-
getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }))
|
|
432
|
-
|
|
433
|
-
const fetcherSpy = vi.fn(() => simpleFetcher())
|
|
434
|
-
|
|
435
|
-
// let afterTimeout = false;
|
|
436
|
-
const query = useQuery({
|
|
437
|
-
queryKey: ['suspense3'],
|
|
438
|
-
queryFn: simpleFetcher,
|
|
439
|
-
staleTime: 10000,
|
|
440
|
-
initialData: 'foo',
|
|
441
|
-
})
|
|
442
|
-
|
|
443
|
-
return query.suspense().then(() => {
|
|
444
|
-
expect(fetcherSpy).toHaveBeenCalledTimes(0)
|
|
445
|
-
})
|
|
446
|
-
})
|
|
447
|
-
|
|
448
|
-
test('should not throw from suspense by default', async () => {
|
|
449
|
-
const getCurrentInstanceSpy = getCurrentInstance as Mock
|
|
450
|
-
getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }))
|
|
451
|
-
|
|
452
|
-
const query = useQuery({
|
|
453
|
-
queryKey: ['suspense4'],
|
|
454
|
-
queryFn: rejectFetcher,
|
|
455
|
-
staleTime: 10000,
|
|
456
|
-
})
|
|
457
|
-
|
|
458
|
-
await flushPromises()
|
|
459
|
-
|
|
460
|
-
expect(query).toMatchObject({
|
|
461
|
-
status: { value: 'error' },
|
|
462
|
-
isError: { value: true },
|
|
463
|
-
})
|
|
464
|
-
})
|
|
465
|
-
|
|
466
|
-
test('should throw from suspense when throwOnError is true', async () => {
|
|
467
|
-
const getCurrentInstanceSpy = getCurrentInstance as Mock
|
|
468
|
-
getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }))
|
|
469
|
-
|
|
470
|
-
const boundaryFn = vi.fn()
|
|
471
|
-
const query = useQuery({
|
|
472
|
-
queryKey: ['suspense5'],
|
|
473
|
-
queryFn: rejectFetcher,
|
|
474
|
-
staleTime: 10000,
|
|
475
|
-
throwOnError: boundaryFn,
|
|
476
|
-
})
|
|
477
|
-
|
|
478
|
-
await query.suspense()
|
|
479
|
-
|
|
480
|
-
expect(boundaryFn).toHaveBeenCalledTimes(2)
|
|
481
|
-
expect(boundaryFn).toHaveBeenNthCalledWith(
|
|
482
|
-
1,
|
|
483
|
-
Error('Some error'),
|
|
484
|
-
expect.objectContaining({
|
|
485
|
-
state: expect.objectContaining({ status: 'error' }),
|
|
486
|
-
}),
|
|
487
|
-
)
|
|
488
|
-
expect(boundaryFn).toHaveBeenNthCalledWith(
|
|
489
|
-
2,
|
|
490
|
-
Error('Some error'),
|
|
491
|
-
expect.objectContaining({
|
|
492
|
-
state: expect.objectContaining({ status: 'error' }),
|
|
493
|
-
}),
|
|
494
|
-
)
|
|
495
|
-
})
|
|
496
|
-
})
|
|
497
|
-
})
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, test, vi } from 'vitest'
|
|
2
|
-
import { hasInjectionContext, inject } from 'vue-demi'
|
|
3
|
-
import { useQueryClient } from '../useQueryClient'
|
|
4
|
-
import { VUE_QUERY_CLIENT } from '../utils'
|
|
5
|
-
import type { Mock } from 'vitest'
|
|
6
|
-
|
|
7
|
-
describe('useQueryClient', () => {
|
|
8
|
-
const injectSpy = inject as Mock
|
|
9
|
-
const hasInjectionContextSpy = hasInjectionContext as Mock
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
vi.restoreAllMocks()
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
test('should return queryClient when it is provided in the context', () => {
|
|
16
|
-
const queryClientMock = { name: 'Mocked client' }
|
|
17
|
-
injectSpy.mockReturnValueOnce(queryClientMock)
|
|
18
|
-
|
|
19
|
-
const queryClient = useQueryClient()
|
|
20
|
-
|
|
21
|
-
expect(queryClient).toStrictEqual(queryClientMock)
|
|
22
|
-
expect(injectSpy).toHaveBeenCalledTimes(1)
|
|
23
|
-
expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT)
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
test('should throw an error when queryClient does not exist in the context', () => {
|
|
27
|
-
injectSpy.mockReturnValueOnce(undefined)
|
|
28
|
-
|
|
29
|
-
expect(useQueryClient).toThrowError()
|
|
30
|
-
expect(injectSpy).toHaveBeenCalledTimes(1)
|
|
31
|
-
expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT)
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
test('should throw an error when used outside of setup function', () => {
|
|
35
|
-
hasInjectionContextSpy.mockReturnValueOnce(false)
|
|
36
|
-
|
|
37
|
-
expect(useQueryClient).toThrowError()
|
|
38
|
-
expect(hasInjectionContextSpy).toHaveBeenCalledTimes(1)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
test('should call inject with a custom key as a suffix', () => {
|
|
42
|
-
const queryClientKey = 'foo'
|
|
43
|
-
const expectedKeyParameter = `${VUE_QUERY_CLIENT}:${queryClientKey}`
|
|
44
|
-
const queryClientMock = { name: 'Mocked client' }
|
|
45
|
-
injectSpy.mockReturnValueOnce(queryClientMock)
|
|
46
|
-
|
|
47
|
-
useQueryClient(queryClientKey)
|
|
48
|
-
|
|
49
|
-
expect(injectSpy).toHaveBeenCalledWith(expectedKeyParameter)
|
|
50
|
-
})
|
|
51
|
-
})
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'vitest'
|
|
2
|
-
import { reactive, ref } from 'vue-demi'
|
|
3
|
-
import { cloneDeep, cloneDeepUnref, updateState } from '../utils'
|
|
4
|
-
|
|
5
|
-
describe('utils', () => {
|
|
6
|
-
describe('updateState', () => {
|
|
7
|
-
test('should update first object with values from the second one', () => {
|
|
8
|
-
const origin = { option1: 'a', option2: 'b', option3: 'c' }
|
|
9
|
-
const update = { option1: 'x', option2: 'y', option3: 'z' }
|
|
10
|
-
const expected = { option1: 'x', option2: 'y', option3: 'z' }
|
|
11
|
-
|
|
12
|
-
updateState(origin, update)
|
|
13
|
-
expect(origin).toEqual(expected)
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
test('should update only existing keys', () => {
|
|
17
|
-
const origin = { option1: 'a', option2: 'b' }
|
|
18
|
-
const update = { option1: 'x', option2: 'y', option3: 'z' }
|
|
19
|
-
const expected = { option1: 'x', option2: 'y' }
|
|
20
|
-
|
|
21
|
-
updateState(origin, update)
|
|
22
|
-
expect(origin).toEqual(expected)
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
test('should remove non existing keys', () => {
|
|
26
|
-
const origin = { option1: 'a', option2: 'b', option3: 'c' }
|
|
27
|
-
const update = { option1: 'x', option2: 'y' }
|
|
28
|
-
const expected = { option1: 'x', option2: 'y' }
|
|
29
|
-
|
|
30
|
-
updateState(origin, update)
|
|
31
|
-
expect(origin).toEqual(expected)
|
|
32
|
-
})
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
describe('cloneDeep', () => {
|
|
36
|
-
test('should copy primitives and functions AS-IS', () => {
|
|
37
|
-
expect(cloneDeep(3456)).toBe(3456)
|
|
38
|
-
expect(cloneDeep('theString')).toBe('theString')
|
|
39
|
-
expect(cloneDeep(null)).toBe(null)
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
test('should copy Maps and Sets AS-IS', () => {
|
|
43
|
-
const setVal = new Set([3, 4, 5])
|
|
44
|
-
const setValCopy = cloneDeep(setVal)
|
|
45
|
-
expect(setValCopy).toBe(setVal)
|
|
46
|
-
expect(setValCopy).toStrictEqual(new Set([3, 4, 5]))
|
|
47
|
-
|
|
48
|
-
const mapVal = new Map([
|
|
49
|
-
['a', 'aVal'],
|
|
50
|
-
['b', 'bVal'],
|
|
51
|
-
])
|
|
52
|
-
const mapValCopy = cloneDeep(mapVal)
|
|
53
|
-
expect(mapValCopy).toBe(mapVal)
|
|
54
|
-
expect(mapValCopy).toStrictEqual(
|
|
55
|
-
new Map([
|
|
56
|
-
['a', 'aVal'],
|
|
57
|
-
['b', 'bVal'],
|
|
58
|
-
]),
|
|
59
|
-
)
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
test('should deeply copy arrays', () => {
|
|
63
|
-
const val = [
|
|
64
|
-
25,
|
|
65
|
-
'str',
|
|
66
|
-
null,
|
|
67
|
-
new Set([3, 4]),
|
|
68
|
-
[5, 6, { a: 1 }],
|
|
69
|
-
undefined,
|
|
70
|
-
]
|
|
71
|
-
const cp = cloneDeep(val)
|
|
72
|
-
expect(cp).toStrictEqual([
|
|
73
|
-
25,
|
|
74
|
-
'str',
|
|
75
|
-
null,
|
|
76
|
-
new Set([3, 4]),
|
|
77
|
-
[5, 6, { a: 1 }],
|
|
78
|
-
undefined,
|
|
79
|
-
])
|
|
80
|
-
expect(cp).not.toBe(val)
|
|
81
|
-
expect(cp[3]).toBe(val[3]) // Set([3, 4])
|
|
82
|
-
expect(cp[4]).not.toBe(val[4]) // [5, 6, { a: 1 }]
|
|
83
|
-
expect((cp[4] as Array<number>)[2]).not.toBe((val[4] as Array<number>)[2]) // { a : 1 }
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
test('should deeply copy object', () => {
|
|
87
|
-
const val = reactive({
|
|
88
|
-
a: 25,
|
|
89
|
-
b: 'str',
|
|
90
|
-
c: null,
|
|
91
|
-
d: undefined,
|
|
92
|
-
e: new Set([5, 6]),
|
|
93
|
-
f: [3, 4],
|
|
94
|
-
g: { fa: 26 },
|
|
95
|
-
})
|
|
96
|
-
const cp = cloneDeep(val)
|
|
97
|
-
|
|
98
|
-
expect(cp).toStrictEqual({
|
|
99
|
-
a: 25,
|
|
100
|
-
b: 'str',
|
|
101
|
-
c: null,
|
|
102
|
-
d: undefined,
|
|
103
|
-
e: new Set([5, 6]),
|
|
104
|
-
f: [3, 4],
|
|
105
|
-
g: { fa: 26 },
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
expect(cp.e).toBe(val.e) // Set
|
|
109
|
-
expect(cp.f).not.toBe(val.f) // []
|
|
110
|
-
expect(cp.g).not.toBe(val.g) // {}
|
|
111
|
-
})
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
describe('cloneDeepUnref', () => {
|
|
115
|
-
test('should unref primitives', () => {
|
|
116
|
-
expect(cloneDeepUnref(ref(34))).toBe(34)
|
|
117
|
-
expect(cloneDeepUnref(ref('myStr'))).toBe('myStr')
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
test('should deeply unref arrays', () => {
|
|
121
|
-
const val = ref([2, 3, ref(4), ref('5'), { a: ref(6) }, [ref(7)]])
|
|
122
|
-
const cp = cloneDeepUnref(val)
|
|
123
|
-
expect(cp).toStrictEqual([2, 3, 4, '5', { a: 6 }, [7]])
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
test('should deeply unref objects', () => {
|
|
127
|
-
const val = ref({
|
|
128
|
-
a: 1,
|
|
129
|
-
b: ref(2),
|
|
130
|
-
c: [ref('c1'), ref(['c2'])],
|
|
131
|
-
d: {
|
|
132
|
-
e: ref('e'),
|
|
133
|
-
},
|
|
134
|
-
})
|
|
135
|
-
const cp = cloneDeepUnref(val)
|
|
136
|
-
|
|
137
|
-
expect(cp).toEqual({
|
|
138
|
-
a: 1,
|
|
139
|
-
b: 2,
|
|
140
|
-
c: ['c1', ['c2']],
|
|
141
|
-
d: { e: 'e' },
|
|
142
|
-
})
|
|
143
|
-
})
|
|
144
|
-
|
|
145
|
-
test('should clone getters returning values in queryKey', () => {
|
|
146
|
-
const val = ref({ queryKey: [1, 2, () => '3'] })
|
|
147
|
-
const cp = cloneDeepUnref(val)
|
|
148
|
-
expect(cp).toStrictEqual({ queryKey: [1, 2, '3'] })
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
test('should unref undefined', () => {
|
|
152
|
-
expect(cloneDeepUnref(ref(undefined))).toBe(undefined)
|
|
153
|
-
})
|
|
154
|
-
})
|
|
155
|
-
})
|