@tanstack/vue-query 5.0.0-alpha.6 → 5.0.0-alpha.61
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/README.md +18 -14
- package/build/lib/__mocks__/useBaseQuery.d.ts +2 -1
- package/build/lib/__mocks__/useBaseQuery.d.ts.map +1 -0
- package/build/lib/__mocks__/useQueryClient.d.ts +2 -1
- package/build/lib/__mocks__/useQueryClient.d.ts.map +1 -0
- package/build/lib/__tests__/mutationCache.test.d.ts +1 -0
- package/build/lib/__tests__/mutationCache.test.d.ts.map +1 -0
- package/build/lib/__tests__/queryCache.test.d.ts +1 -0
- package/build/lib/__tests__/queryCache.test.d.ts.map +1 -0
- package/build/lib/__tests__/queryClient.test.d.ts +1 -0
- package/build/lib/__tests__/queryClient.test.d.ts.map +1 -0
- package/build/lib/__tests__/test-utils.d.ts +3 -2
- package/build/lib/__tests__/test-utils.d.ts.map +1 -0
- package/build/lib/__tests__/useInfiniteQuery.test.d.ts +1 -0
- package/build/lib/__tests__/useInfiniteQuery.test.d.ts.map +1 -0
- package/build/lib/__tests__/useInfiniteQuery.types.test.d.ts +1 -0
- package/build/lib/__tests__/useInfiniteQuery.types.test.d.ts.map +1 -0
- package/build/lib/__tests__/useIsFetching.test.d.ts +1 -0
- package/build/lib/__tests__/useIsFetching.test.d.ts.map +1 -0
- package/build/lib/__tests__/useIsMutating.test.d.ts +1 -0
- package/build/lib/__tests__/useIsMutating.test.d.ts.map +1 -0
- package/build/lib/__tests__/useMutation.test.d.ts +1 -0
- package/build/lib/__tests__/useMutation.test.d.ts.map +1 -0
- package/build/lib/__tests__/useMutation.types.test.d.ts +1 -0
- package/build/lib/__tests__/useMutation.types.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQueries.test.d.ts +1 -0
- package/build/lib/__tests__/useQueries.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQuery.test.d.ts +1 -0
- package/build/lib/__tests__/useQuery.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQuery.types.test.d.ts +1 -0
- package/build/lib/__tests__/useQuery.types.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQueryClient.test.d.ts +1 -0
- package/build/lib/__tests__/useQueryClient.test.d.ts.map +1 -0
- package/build/lib/__tests__/utils.test.d.ts +1 -0
- package/build/lib/__tests__/utils.test.d.ts.map +1 -0
- package/build/lib/__tests__/vueQueryPlugin.test.d.ts +1 -0
- package/build/lib/__tests__/vueQueryPlugin.test.d.ts.map +1 -0
- package/build/lib/devtools/{devtools.mjs → devtools.cjs} +62 -21
- package/build/lib/devtools/devtools.cjs.map +1 -0
- package/build/lib/devtools/devtools.d.ts +1 -0
- package/build/lib/devtools/devtools.d.ts.map +1 -0
- package/build/lib/devtools/devtools.js +61 -26
- package/build/lib/devtools/devtools.js.map +1 -1
- package/build/lib/devtools/{utils.esm.js → utils.cjs} +11 -6
- package/build/lib/devtools/utils.cjs.map +1 -0
- package/build/lib/devtools/utils.d.ts +2 -1
- package/build/lib/devtools/utils.d.ts.map +1 -0
- package/build/lib/devtools/utils.js +4 -11
- package/build/lib/devtools/utils.js.map +1 -1
- package/build/lib/index.cjs +38 -0
- package/build/lib/index.cjs.map +1 -0
- package/build/lib/index.d.ts +3 -2
- package/build/lib/index.d.ts.map +1 -0
- package/build/lib/index.js +13 -36
- package/build/lib/index.js.map +1 -1
- package/build/lib/mutationCache.cjs +16 -0
- package/build/lib/{mutationCache.mjs.map → mutationCache.cjs.map} +1 -1
- package/build/lib/mutationCache.d.ts +1 -0
- package/build/lib/mutationCache.d.ts.map +1 -0
- package/build/lib/mutationCache.js +6 -8
- package/build/lib/mutationCache.js.map +1 -1
- package/build/lib/queryCache.cjs +16 -0
- package/build/lib/{queryCache.mjs.map → queryCache.cjs.map} +1 -1
- package/build/lib/queryCache.d.ts +1 -0
- package/build/lib/queryCache.d.ts.map +1 -0
- package/build/lib/queryCache.js +6 -8
- package/build/lib/queryCache.js.map +1 -1
- package/build/lib/queryClient.cjs +86 -0
- package/build/lib/{queryClient.mjs.map → queryClient.cjs.map} +1 -1
- package/build/lib/queryClient.d.ts +1 -0
- package/build/lib/queryClient.d.ts.map +1 -0
- package/build/lib/queryClient.js +32 -34
- package/build/lib/queryClient.js.map +1 -1
- package/build/lib/types.d.ts +4 -3
- package/build/lib/types.d.ts.map +1 -0
- package/build/lib/{useBaseQuery.esm.js → useBaseQuery.cjs} +19 -17
- package/build/lib/{useBaseQuery.mjs.map → useBaseQuery.cjs.map} +1 -1
- package/build/lib/useBaseQuery.d.ts +3 -2
- package/build/lib/useBaseQuery.d.ts.map +1 -0
- package/build/lib/useBaseQuery.js +16 -18
- package/build/lib/useBaseQuery.js.map +1 -1
- package/build/lib/{useInfiniteQuery.esm.js → useInfiniteQuery.cjs} +8 -6
- package/build/lib/{useInfiniteQuery.mjs.map → useInfiniteQuery.cjs.map} +1 -1
- package/build/lib/useInfiniteQuery.d.ts +4 -3
- package/build/lib/useInfiniteQuery.d.ts.map +1 -0
- package/build/lib/useInfiniteQuery.js +5 -7
- package/build/lib/useInfiniteQuery.js.map +1 -1
- package/build/lib/useIsFetching.cjs +26 -0
- package/build/lib/{useIsFetching.mjs.map → useIsFetching.cjs.map} +1 -1
- package/build/lib/useIsFetching.d.ts +2 -1
- package/build/lib/useIsFetching.d.ts.map +1 -0
- package/build/lib/useIsFetching.js +9 -11
- package/build/lib/useIsFetching.js.map +1 -1
- package/build/lib/useMutation.cjs +41 -0
- package/build/lib/{useMutation.mjs.map → useMutation.cjs.map} +1 -1
- package/build/lib/useMutation.d.ts +5 -4
- package/build/lib/useMutation.d.ts.map +1 -0
- package/build/lib/useMutation.js +14 -16
- package/build/lib/useMutation.js.map +1 -1
- package/build/lib/useMutationState.cjs +36 -0
- package/build/lib/useMutationState.cjs.map +1 -0
- package/build/lib/useMutationState.d.ts +12 -0
- package/build/lib/useMutationState.d.ts.map +1 -0
- package/build/lib/useMutationState.js +33 -0
- package/build/lib/useMutationState.js.map +1 -0
- package/build/lib/useQueries.cjs +62 -0
- package/build/lib/useQueries.cjs.map +1 -0
- package/build/lib/useQueries.d.ts +12 -11
- package/build/lib/useQueries.d.ts.map +1 -0
- package/build/lib/useQueries.js +35 -23
- package/build/lib/useQueries.js.map +1 -1
- package/build/lib/useQuery.cjs +15 -0
- package/build/lib/{useQuery.mjs.map → useQuery.cjs.map} +1 -1
- package/build/lib/useQuery.d.ts +4 -3
- package/build/lib/useQuery.d.ts.map +1 -0
- package/build/lib/useQuery.js +4 -6
- package/build/lib/useQuery.js.map +1 -1
- package/build/lib/{useQueryClient.mjs → useQueryClient.cjs} +9 -7
- package/build/lib/{useQueryClient.mjs.map → useQueryClient.cjs.map} +1 -1
- package/build/lib/useQueryClient.d.ts +1 -0
- package/build/lib/useQueryClient.d.ts.map +1 -0
- package/build/lib/useQueryClient.js +6 -9
- package/build/lib/useQueryClient.js.map +1 -1
- package/build/lib/{utils.mjs → utils.cjs} +12 -6
- package/build/lib/{utils.mjs.map → utils.cjs.map} +1 -1
- package/build/lib/utils.d.ts +1 -0
- package/build/lib/utils.d.ts.map +1 -0
- package/build/lib/utils.js +5 -11
- package/build/lib/utils.js.map +1 -1
- package/build/lib/{vueQueryPlugin.mjs → vueQueryPlugin.cjs} +15 -15
- package/build/lib/vueQueryPlugin.cjs.map +1 -0
- package/build/lib/vueQueryPlugin.d.ts +3 -2
- package/build/lib/vueQueryPlugin.d.ts.map +1 -0
- package/build/lib/vueQueryPlugin.js +12 -16
- package/build/lib/vueQueryPlugin.js.map +1 -1
- package/package.json +21 -16
- package/src/__mocks__/useBaseQuery.ts +8 -0
- package/src/__mocks__/useQueryClient.ts +10 -0
- package/src/__tests__/mutationCache.test.ts +40 -0
- package/src/__tests__/queryCache.test.ts +48 -0
- package/src/__tests__/queryClient.test.ts +365 -0
- package/src/__tests__/test-utils.ts +62 -0
- package/src/__tests__/useInfiniteQuery.test.ts +37 -0
- package/src/__tests__/useInfiniteQuery.types.test.tsx +102 -0
- package/src/__tests__/useIsFetching.test.ts +77 -0
- package/src/__tests__/useIsMutating.test.ts +123 -0
- package/src/__tests__/useMutation.test.ts +335 -0
- package/src/__tests__/useMutation.types.test.tsx +97 -0
- package/src/__tests__/useQueries.test.ts +256 -0
- package/src/__tests__/useQuery.test.ts +290 -0
- package/src/__tests__/useQuery.types.test.tsx +88 -0
- package/src/__tests__/useQueryClient.test.ts +51 -0
- package/src/__tests__/utils.test.ts +148 -0
- package/src/__tests__/vueQueryPlugin.test.ts +351 -0
- package/src/devtools/devtools.ts +249 -0
- package/src/devtools/utils.ts +96 -0
- package/src/index.ts +30 -0
- package/src/mutationCache.ts +25 -0
- package/src/queryCache.ts +21 -0
- package/src/queryClient.ts +278 -0
- package/src/types.ts +17 -0
- package/src/useBaseQuery.ts +152 -0
- package/src/useInfiniteQuery.ts +105 -0
- package/src/useIsFetching.ts +37 -0
- package/src/useMutation.ts +109 -0
- package/src/useMutationState.ts +73 -0
- package/src/useQueries.ts +227 -0
- package/src/useQuery.ts +125 -0
- package/src/useQueryClient.ts +23 -0
- package/src/utils.ts +67 -0
- package/src/vueQueryPlugin.ts +101 -0
- package/build/lib/devtools/devtools.esm.js +0 -157
- package/build/lib/devtools/devtools.esm.js.map +0 -1
- package/build/lib/devtools/devtools.mjs.map +0 -1
- package/build/lib/devtools/utils.esm.js.map +0 -1
- package/build/lib/devtools/utils.mjs +0 -80
- package/build/lib/devtools/utils.mjs.map +0 -1
- package/build/lib/index.esm.js +0 -14
- package/build/lib/index.esm.js.map +0 -1
- package/build/lib/index.mjs +0 -14
- package/build/lib/index.mjs.map +0 -1
- package/build/lib/mutationCache.esm.js +0 -14
- package/build/lib/mutationCache.esm.js.map +0 -1
- package/build/lib/mutationCache.mjs +0 -14
- package/build/lib/queryCache.esm.js +0 -14
- package/build/lib/queryCache.esm.js.map +0 -1
- package/build/lib/queryCache.mjs +0 -14
- package/build/lib/queryClient.esm.js +0 -84
- package/build/lib/queryClient.esm.js.map +0 -1
- package/build/lib/queryClient.mjs +0 -84
- package/build/lib/useBaseQuery.esm.js.map +0 -1
- package/build/lib/useBaseQuery.mjs +0 -67
- package/build/lib/useInfiniteQuery.esm.js.map +0 -1
- package/build/lib/useInfiniteQuery.mjs +0 -18
- package/build/lib/useIsFetching.esm.js +0 -24
- package/build/lib/useIsFetching.esm.js.map +0 -1
- package/build/lib/useIsFetching.mjs +0 -24
- package/build/lib/useIsMutating.d.ts +0 -6
- package/build/lib/useIsMutating.esm.js +0 -24
- package/build/lib/useIsMutating.esm.js.map +0 -1
- package/build/lib/useIsMutating.js +0 -26
- package/build/lib/useIsMutating.js.map +0 -1
- package/build/lib/useIsMutating.mjs +0 -24
- package/build/lib/useIsMutating.mjs.map +0 -1
- package/build/lib/useMutation.esm.js +0 -39
- package/build/lib/useMutation.esm.js.map +0 -1
- package/build/lib/useMutation.mjs +0 -39
- package/build/lib/useQueries.esm.js +0 -46
- package/build/lib/useQueries.esm.js.map +0 -1
- package/build/lib/useQueries.mjs +0 -46
- package/build/lib/useQueries.mjs.map +0 -1
- package/build/lib/useQuery.esm.js +0 -13
- package/build/lib/useQuery.esm.js.map +0 -1
- package/build/lib/useQuery.mjs +0 -13
- package/build/lib/useQueryClient.esm.js +0 -19
- package/build/lib/useQueryClient.esm.js.map +0 -1
- package/build/lib/utils.esm.js +0 -50
- package/build/lib/utils.esm.js.map +0 -1
- package/build/lib/vueQueryPlugin.esm.js +0 -75
- package/build/lib/vueQueryPlugin.esm.js.map +0 -1
- package/build/lib/vueQueryPlugin.mjs.map +0 -1
- package/build/umd/index.development.js +0 -4193
- package/build/umd/index.development.js.map +0 -1
- package/build/umd/index.production.js +0 -2
- package/build/umd/index.production.js.map +0 -1
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { reactive } from 'vue'
|
|
2
|
+
import { useMutation } from '../useMutation'
|
|
3
|
+
import { doNotExecute, successMutator } from './test-utils'
|
|
4
|
+
import type { Equal, Expect } from './test-utils'
|
|
5
|
+
|
|
6
|
+
describe('Discriminated union return type', () => {
|
|
7
|
+
it('data should be possibly undefined by default', () => {
|
|
8
|
+
doNotExecute(() => {
|
|
9
|
+
const mutation = reactive(
|
|
10
|
+
useMutation({ mutationFn: successMutator<string> }),
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
const result: Expect<Equal<string | undefined, typeof mutation.data>> =
|
|
14
|
+
true
|
|
15
|
+
return result
|
|
16
|
+
})
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('data should be defined when mutation is success', () => {
|
|
20
|
+
doNotExecute(() => {
|
|
21
|
+
const mutation = reactive(
|
|
22
|
+
useMutation({ mutationFn: successMutator<string> }),
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
if (mutation.isSuccess) {
|
|
26
|
+
const result: Expect<Equal<string, typeof mutation.data>> = true
|
|
27
|
+
return result
|
|
28
|
+
}
|
|
29
|
+
return
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('error should be null when mutation is success', () => {
|
|
34
|
+
doNotExecute(() => {
|
|
35
|
+
const mutation = reactive(
|
|
36
|
+
useMutation({ mutationFn: successMutator<string> }),
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
if (mutation.isSuccess) {
|
|
40
|
+
const result: Expect<Equal<null, typeof mutation.error>> = true
|
|
41
|
+
return result
|
|
42
|
+
}
|
|
43
|
+
return
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('data should be undefined when mutation is pending', () => {
|
|
48
|
+
doNotExecute(() => {
|
|
49
|
+
const mutation = reactive(
|
|
50
|
+
useMutation({ mutationFn: successMutator<string> }),
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
if (mutation.isPending) {
|
|
54
|
+
const result: Expect<Equal<undefined, typeof mutation.data>> = true
|
|
55
|
+
return result
|
|
56
|
+
}
|
|
57
|
+
return
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('error should be defined when mutation is error', () => {
|
|
62
|
+
doNotExecute(() => {
|
|
63
|
+
const mutation = reactive(
|
|
64
|
+
useMutation({ mutationFn: successMutator<string> }),
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
if (mutation.isError) {
|
|
68
|
+
const result: Expect<Equal<Error, typeof mutation.error>> = true
|
|
69
|
+
return result
|
|
70
|
+
}
|
|
71
|
+
return
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('should narrow variables', () => {
|
|
76
|
+
doNotExecute(() => {
|
|
77
|
+
const mutation = reactive(
|
|
78
|
+
useMutation({ mutationFn: successMutator<string> }),
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
if (mutation.isIdle) {
|
|
82
|
+
const result: Expect<Equal<undefined, typeof mutation.variables>> = true
|
|
83
|
+
return result
|
|
84
|
+
}
|
|
85
|
+
if (mutation.isPending) {
|
|
86
|
+
const result: Expect<Equal<string, typeof mutation.variables>> = true
|
|
87
|
+
return result
|
|
88
|
+
}
|
|
89
|
+
if (mutation.isSuccess) {
|
|
90
|
+
const result: Expect<Equal<string, typeof mutation.variables>> = true
|
|
91
|
+
return result
|
|
92
|
+
}
|
|
93
|
+
const result: Expect<Equal<string, typeof mutation.variables>> = true
|
|
94
|
+
return result
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
})
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { onScopeDispose, ref } from 'vue-demi'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
flushPromises,
|
|
5
|
+
rejectFetcher,
|
|
6
|
+
simpleFetcher,
|
|
7
|
+
getSimpleFetcherWithReturnData,
|
|
8
|
+
} from './test-utils'
|
|
9
|
+
import { useQueries } from '../useQueries'
|
|
10
|
+
import { useQueryClient } from '../useQueryClient'
|
|
11
|
+
import { QueryClient } from '../queryClient'
|
|
12
|
+
import { vi } from 'vitest'
|
|
13
|
+
import type { MockedFunction } from 'vitest'
|
|
14
|
+
|
|
15
|
+
vi.mock('../useQueryClient')
|
|
16
|
+
|
|
17
|
+
describe('useQueries', () => {
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
vi.restoreAllMocks()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('should return result for each query', () => {
|
|
23
|
+
const queries = [
|
|
24
|
+
{
|
|
25
|
+
queryKey: ['key1'],
|
|
26
|
+
queryFn: simpleFetcher,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
queryKey: ['key2'],
|
|
30
|
+
queryFn: simpleFetcher,
|
|
31
|
+
},
|
|
32
|
+
]
|
|
33
|
+
const queriesState = useQueries({ queries })
|
|
34
|
+
|
|
35
|
+
expect(queriesState.value).toMatchObject([
|
|
36
|
+
{
|
|
37
|
+
status: 'pending',
|
|
38
|
+
isPending: true,
|
|
39
|
+
isFetching: true,
|
|
40
|
+
isStale: true,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
status: 'pending',
|
|
44
|
+
isPending: true,
|
|
45
|
+
isFetching: true,
|
|
46
|
+
isStale: true,
|
|
47
|
+
},
|
|
48
|
+
])
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('should resolve to success and update reactive state', async () => {
|
|
52
|
+
const queries = [
|
|
53
|
+
{
|
|
54
|
+
queryKey: ['key11'],
|
|
55
|
+
queryFn: simpleFetcher,
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
queryKey: ['key12'],
|
|
59
|
+
queryFn: simpleFetcher,
|
|
60
|
+
},
|
|
61
|
+
]
|
|
62
|
+
const queriesState = useQueries({ queries })
|
|
63
|
+
|
|
64
|
+
await flushPromises()
|
|
65
|
+
|
|
66
|
+
expect(queriesState.value).toMatchObject([
|
|
67
|
+
{
|
|
68
|
+
status: 'success',
|
|
69
|
+
isPending: false,
|
|
70
|
+
isFetching: false,
|
|
71
|
+
isStale: true,
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
status: 'success',
|
|
75
|
+
isPending: false,
|
|
76
|
+
isFetching: false,
|
|
77
|
+
isStale: true,
|
|
78
|
+
},
|
|
79
|
+
])
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test('should reject one of the queries and update reactive state', async () => {
|
|
83
|
+
const queries = [
|
|
84
|
+
{
|
|
85
|
+
queryKey: ['key21'],
|
|
86
|
+
queryFn: rejectFetcher,
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
queryKey: ['key22'],
|
|
90
|
+
queryFn: simpleFetcher,
|
|
91
|
+
},
|
|
92
|
+
]
|
|
93
|
+
const queriesState = useQueries({ queries })
|
|
94
|
+
|
|
95
|
+
await flushPromises()
|
|
96
|
+
|
|
97
|
+
expect(queriesState.value).toMatchObject([
|
|
98
|
+
{
|
|
99
|
+
status: 'error',
|
|
100
|
+
isPending: false,
|
|
101
|
+
isFetching: false,
|
|
102
|
+
isStale: true,
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
status: 'success',
|
|
106
|
+
isPending: false,
|
|
107
|
+
isFetching: false,
|
|
108
|
+
isStale: true,
|
|
109
|
+
},
|
|
110
|
+
])
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('should return state for new queries', async () => {
|
|
114
|
+
const queries = ref([
|
|
115
|
+
{
|
|
116
|
+
queryKey: ['key31'],
|
|
117
|
+
queryFn: getSimpleFetcherWithReturnData('value31'),
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
queryKey: ['key32'],
|
|
121
|
+
queryFn: getSimpleFetcherWithReturnData('value32'),
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
queryKey: ['key33'],
|
|
125
|
+
queryFn: getSimpleFetcherWithReturnData('value33'),
|
|
126
|
+
},
|
|
127
|
+
])
|
|
128
|
+
const queriesState = useQueries({ queries })
|
|
129
|
+
|
|
130
|
+
await flushPromises()
|
|
131
|
+
|
|
132
|
+
queries.value.splice(
|
|
133
|
+
0,
|
|
134
|
+
queries.value.length,
|
|
135
|
+
{
|
|
136
|
+
queryKey: ['key31'],
|
|
137
|
+
queryFn: getSimpleFetcherWithReturnData('value31'),
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
queryKey: ['key34'],
|
|
141
|
+
queryFn: getSimpleFetcherWithReturnData('value34'),
|
|
142
|
+
},
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
await flushPromises()
|
|
146
|
+
await flushPromises()
|
|
147
|
+
|
|
148
|
+
expect(queriesState.value.length).toEqual(2)
|
|
149
|
+
expect(queriesState.value).toMatchObject([
|
|
150
|
+
{
|
|
151
|
+
data: 'value31',
|
|
152
|
+
status: 'success',
|
|
153
|
+
isPending: false,
|
|
154
|
+
isFetching: false,
|
|
155
|
+
isStale: true,
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
data: 'value34',
|
|
159
|
+
status: 'success',
|
|
160
|
+
isPending: false,
|
|
161
|
+
isFetching: false,
|
|
162
|
+
isStale: true,
|
|
163
|
+
},
|
|
164
|
+
])
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
test('should stop listening to changes on onScopeDispose', async () => {
|
|
168
|
+
const onScopeDisposeMock = onScopeDispose as MockedFunction<
|
|
169
|
+
typeof onScopeDispose
|
|
170
|
+
>
|
|
171
|
+
onScopeDisposeMock.mockImplementationOnce((fn) => fn())
|
|
172
|
+
|
|
173
|
+
const queries = [
|
|
174
|
+
{
|
|
175
|
+
queryKey: ['key41'],
|
|
176
|
+
queryFn: simpleFetcher,
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
queryKey: ['key42'],
|
|
180
|
+
queryFn: simpleFetcher,
|
|
181
|
+
},
|
|
182
|
+
]
|
|
183
|
+
const queriesState = useQueries({ queries })
|
|
184
|
+
await flushPromises()
|
|
185
|
+
|
|
186
|
+
expect(queriesState.value).toMatchObject([
|
|
187
|
+
{
|
|
188
|
+
status: 'pending',
|
|
189
|
+
isPending: true,
|
|
190
|
+
isFetching: true,
|
|
191
|
+
isStale: true,
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
status: 'pending',
|
|
195
|
+
isPending: true,
|
|
196
|
+
isFetching: true,
|
|
197
|
+
isStale: true,
|
|
198
|
+
},
|
|
199
|
+
])
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
test('should use queryClient provided via options', async () => {
|
|
203
|
+
const queryClient = new QueryClient()
|
|
204
|
+
const queries = [
|
|
205
|
+
{
|
|
206
|
+
queryKey: ['key41'],
|
|
207
|
+
queryFn: simpleFetcher,
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
queryKey: ['key42'],
|
|
211
|
+
queryFn: simpleFetcher,
|
|
212
|
+
},
|
|
213
|
+
]
|
|
214
|
+
|
|
215
|
+
useQueries({ queries }, queryClient)
|
|
216
|
+
await flushPromises()
|
|
217
|
+
|
|
218
|
+
expect(useQueryClient).toHaveBeenCalledTimes(0)
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
test('should combine queries', async () => {
|
|
222
|
+
const firstResult = 'first result'
|
|
223
|
+
const secondResult = 'second result'
|
|
224
|
+
|
|
225
|
+
const queryClient = new QueryClient()
|
|
226
|
+
const queries = [
|
|
227
|
+
{
|
|
228
|
+
queryKey: ['key41'],
|
|
229
|
+
queryFn: getSimpleFetcherWithReturnData(firstResult),
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
queryKey: ['key42'],
|
|
233
|
+
queryFn: getSimpleFetcherWithReturnData(secondResult),
|
|
234
|
+
},
|
|
235
|
+
]
|
|
236
|
+
|
|
237
|
+
const queriesResult = useQueries(
|
|
238
|
+
{
|
|
239
|
+
queries,
|
|
240
|
+
combine: (results) => {
|
|
241
|
+
return {
|
|
242
|
+
combined: true,
|
|
243
|
+
res: results.map((res) => res.data),
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
queryClient,
|
|
248
|
+
)
|
|
249
|
+
await flushPromises()
|
|
250
|
+
|
|
251
|
+
expect(queriesResult.value).toMatchObject({
|
|
252
|
+
combined: true,
|
|
253
|
+
res: [firstResult, secondResult],
|
|
254
|
+
})
|
|
255
|
+
})
|
|
256
|
+
})
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import {
|
|
2
|
+
computed,
|
|
3
|
+
reactive,
|
|
4
|
+
ref,
|
|
5
|
+
onScopeDispose,
|
|
6
|
+
getCurrentInstance,
|
|
7
|
+
} from 'vue-demi'
|
|
8
|
+
import { QueryObserver } from '@tanstack/query-core'
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
flushPromises,
|
|
12
|
+
rejectFetcher,
|
|
13
|
+
simpleFetcher,
|
|
14
|
+
getSimpleFetcherWithReturnData,
|
|
15
|
+
} from './test-utils'
|
|
16
|
+
import { useQuery } from '../useQuery'
|
|
17
|
+
import { useBaseQuery } from '../useBaseQuery'
|
|
18
|
+
import { vi } from 'vitest'
|
|
19
|
+
import type { Mock, MockedFunction } from 'vitest'
|
|
20
|
+
|
|
21
|
+
vi.mock('../useQueryClient')
|
|
22
|
+
vi.mock('../useBaseQuery')
|
|
23
|
+
|
|
24
|
+
describe('useQuery', () => {
|
|
25
|
+
test('should properly execute query', () => {
|
|
26
|
+
useQuery({ queryKey: ['key0'], queryFn: simpleFetcher, staleTime: 1000 })
|
|
27
|
+
|
|
28
|
+
expect(useBaseQuery).toBeCalledWith(
|
|
29
|
+
QueryObserver,
|
|
30
|
+
{
|
|
31
|
+
queryKey: ['key0'],
|
|
32
|
+
queryFn: simpleFetcher,
|
|
33
|
+
staleTime: 1000,
|
|
34
|
+
},
|
|
35
|
+
undefined,
|
|
36
|
+
)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('should return pending status initially', () => {
|
|
40
|
+
const query = useQuery({ queryKey: ['key1'], queryFn: simpleFetcher })
|
|
41
|
+
|
|
42
|
+
expect(query).toMatchObject({
|
|
43
|
+
status: { value: 'pending' },
|
|
44
|
+
isPending: { value: true },
|
|
45
|
+
isFetching: { value: true },
|
|
46
|
+
isStale: { value: true },
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('should resolve to success and update reactive state: useQuery(key, dataFn)', async () => {
|
|
51
|
+
const query = useQuery({
|
|
52
|
+
queryKey: ['key2'],
|
|
53
|
+
queryFn: getSimpleFetcherWithReturnData('result2'),
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
await flushPromises()
|
|
57
|
+
|
|
58
|
+
expect(query).toMatchObject({
|
|
59
|
+
status: { value: 'success' },
|
|
60
|
+
data: { value: 'result2' },
|
|
61
|
+
isPending: { value: false },
|
|
62
|
+
isFetching: { value: false },
|
|
63
|
+
isFetched: { value: true },
|
|
64
|
+
isSuccess: { value: true },
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('should resolve to success and update reactive state: useQuery(optionsObj)', async () => {
|
|
69
|
+
const query = useQuery({
|
|
70
|
+
queryKey: ['key31'],
|
|
71
|
+
queryFn: getSimpleFetcherWithReturnData('result31'),
|
|
72
|
+
enabled: true,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
await flushPromises()
|
|
76
|
+
|
|
77
|
+
expect(query).toMatchObject({
|
|
78
|
+
status: { value: 'success' },
|
|
79
|
+
data: { value: 'result31' },
|
|
80
|
+
isPending: { value: false },
|
|
81
|
+
isFetching: { value: false },
|
|
82
|
+
isFetched: { value: true },
|
|
83
|
+
isSuccess: { value: true },
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('should resolve to success and update reactive state: useQuery(key, optionsObj)', async () => {
|
|
88
|
+
const query = useQuery({
|
|
89
|
+
queryKey: ['key32'],
|
|
90
|
+
queryFn: getSimpleFetcherWithReturnData('result32'),
|
|
91
|
+
enabled: true,
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
await flushPromises()
|
|
95
|
+
|
|
96
|
+
expect(query).toMatchObject({
|
|
97
|
+
status: { value: 'success' },
|
|
98
|
+
data: { value: 'result32' },
|
|
99
|
+
isPending: { value: false },
|
|
100
|
+
isFetching: { value: false },
|
|
101
|
+
isFetched: { value: true },
|
|
102
|
+
isSuccess: { value: true },
|
|
103
|
+
})
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('should reject and update reactive state', async () => {
|
|
107
|
+
const query = useQuery({
|
|
108
|
+
queryKey: ['key3'],
|
|
109
|
+
queryFn: rejectFetcher,
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
await flushPromises()
|
|
113
|
+
|
|
114
|
+
expect(query).toMatchObject({
|
|
115
|
+
status: { value: 'error' },
|
|
116
|
+
data: { value: undefined },
|
|
117
|
+
error: { value: { message: 'Some error' } },
|
|
118
|
+
isPending: { value: false },
|
|
119
|
+
isFetching: { value: false },
|
|
120
|
+
isFetched: { value: true },
|
|
121
|
+
isError: { value: true },
|
|
122
|
+
failureCount: { value: 1 },
|
|
123
|
+
failureReason: { value: { message: 'Some error' } },
|
|
124
|
+
})
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
test('should update query on reactive (Ref) key change', async () => {
|
|
128
|
+
const secondKeyRef = ref('key7')
|
|
129
|
+
const query = useQuery({
|
|
130
|
+
queryKey: ['key6', secondKeyRef],
|
|
131
|
+
queryFn: simpleFetcher,
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
await flushPromises()
|
|
135
|
+
|
|
136
|
+
expect(query).toMatchObject({
|
|
137
|
+
status: { value: 'success' },
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
secondKeyRef.value = 'key8'
|
|
141
|
+
await flushPromises()
|
|
142
|
+
|
|
143
|
+
expect(query).toMatchObject({
|
|
144
|
+
status: { value: 'pending' },
|
|
145
|
+
data: { value: undefined },
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
await flushPromises()
|
|
149
|
+
|
|
150
|
+
expect(query).toMatchObject({
|
|
151
|
+
status: { value: 'success' },
|
|
152
|
+
})
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test("should update query when an option is passed as Ref and it's changed", async () => {
|
|
156
|
+
const enabled = ref(false)
|
|
157
|
+
const query = useQuery({
|
|
158
|
+
queryKey: ['key9'],
|
|
159
|
+
queryFn: simpleFetcher,
|
|
160
|
+
enabled,
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
await flushPromises()
|
|
164
|
+
|
|
165
|
+
expect(query).toMatchObject({
|
|
166
|
+
fetchStatus: { value: 'idle' },
|
|
167
|
+
data: { value: undefined },
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
enabled.value = true
|
|
171
|
+
|
|
172
|
+
await flushPromises()
|
|
173
|
+
|
|
174
|
+
expect(query).toMatchObject({
|
|
175
|
+
fetchStatus: { value: 'fetching' },
|
|
176
|
+
data: { value: undefined },
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
await flushPromises()
|
|
180
|
+
|
|
181
|
+
expect(query).toMatchObject({
|
|
182
|
+
status: { value: 'success' },
|
|
183
|
+
})
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
test('should properly execute dependant queries', async () => {
|
|
187
|
+
const { data } = useQuery({
|
|
188
|
+
queryKey: ['dependant1'],
|
|
189
|
+
queryFn: simpleFetcher,
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
const enabled = computed(() => !!data.value)
|
|
193
|
+
|
|
194
|
+
const { fetchStatus, status } = useQuery(
|
|
195
|
+
reactive({
|
|
196
|
+
queryKey: ['dependant2'],
|
|
197
|
+
queryFn: simpleFetcher,
|
|
198
|
+
enabled,
|
|
199
|
+
}),
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
expect(data.value).toStrictEqual(undefined)
|
|
203
|
+
expect(fetchStatus.value).toStrictEqual('idle')
|
|
204
|
+
|
|
205
|
+
await flushPromises()
|
|
206
|
+
|
|
207
|
+
expect(data.value).toStrictEqual('Some data')
|
|
208
|
+
expect(fetchStatus.value).toStrictEqual('fetching')
|
|
209
|
+
|
|
210
|
+
await flushPromises()
|
|
211
|
+
|
|
212
|
+
expect(fetchStatus.value).toStrictEqual('idle')
|
|
213
|
+
expect(status.value).toStrictEqual('success')
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
test('should stop listening to changes on onScopeDispose', async () => {
|
|
217
|
+
const onScopeDisposeMock = onScopeDispose as MockedFunction<
|
|
218
|
+
typeof onScopeDispose
|
|
219
|
+
>
|
|
220
|
+
onScopeDisposeMock.mockImplementationOnce((fn) => fn())
|
|
221
|
+
|
|
222
|
+
const { status } = useQuery({
|
|
223
|
+
queryKey: ['onScopeDispose'],
|
|
224
|
+
queryFn: simpleFetcher,
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
expect(status.value).toStrictEqual('pending')
|
|
228
|
+
|
|
229
|
+
await flushPromises()
|
|
230
|
+
|
|
231
|
+
expect(status.value).toStrictEqual('pending')
|
|
232
|
+
|
|
233
|
+
await flushPromises()
|
|
234
|
+
|
|
235
|
+
expect(status.value).toStrictEqual('pending')
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
describe('suspense', () => {
|
|
239
|
+
test('should return a Promise', () => {
|
|
240
|
+
const getCurrentInstanceSpy = getCurrentInstance as Mock
|
|
241
|
+
getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }))
|
|
242
|
+
|
|
243
|
+
const query = useQuery({ queryKey: ['suspense'], queryFn: simpleFetcher })
|
|
244
|
+
const result = query.suspense()
|
|
245
|
+
|
|
246
|
+
expect(result).toBeInstanceOf(Promise)
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
test('should resolve after being enabled', () => {
|
|
250
|
+
const getCurrentInstanceSpy = getCurrentInstance as Mock
|
|
251
|
+
getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }))
|
|
252
|
+
|
|
253
|
+
let afterTimeout = false
|
|
254
|
+
const isEnabeld = ref(false)
|
|
255
|
+
const query = useQuery({
|
|
256
|
+
queryKey: ['suspense'],
|
|
257
|
+
queryFn: simpleFetcher,
|
|
258
|
+
enabled: isEnabeld,
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
setTimeout(() => {
|
|
262
|
+
afterTimeout = true
|
|
263
|
+
isEnabeld.value = true
|
|
264
|
+
}, 200)
|
|
265
|
+
|
|
266
|
+
return query.suspense().then(() => {
|
|
267
|
+
expect(afterTimeout).toBe(true)
|
|
268
|
+
})
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
test('should resolve immidiately when stale without refetching', () => {
|
|
272
|
+
const getCurrentInstanceSpy = getCurrentInstance as Mock
|
|
273
|
+
getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }))
|
|
274
|
+
|
|
275
|
+
const fetcherSpy = vi.fn(() => simpleFetcher())
|
|
276
|
+
|
|
277
|
+
// let afterTimeout = false;
|
|
278
|
+
const query = useQuery({
|
|
279
|
+
queryKey: ['suspense'],
|
|
280
|
+
queryFn: simpleFetcher,
|
|
281
|
+
staleTime: 10000,
|
|
282
|
+
initialData: 'foo',
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
return query.suspense().then(() => {
|
|
286
|
+
expect(fetcherSpy).toHaveBeenCalledTimes(0)
|
|
287
|
+
})
|
|
288
|
+
})
|
|
289
|
+
})
|
|
290
|
+
})
|