@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,69 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test, vi } from 'vitest'
|
|
2
|
-
import { useInfiniteQuery } from '../useInfiniteQuery'
|
|
3
|
-
import { infiniteQueryOptions } from '../infiniteQueryOptions'
|
|
4
|
-
import { flushPromises, infiniteFetcher } from './test-utils'
|
|
5
|
-
|
|
6
|
-
vi.mock('../useQueryClient')
|
|
7
|
-
|
|
8
|
-
describe('useQuery', () => {
|
|
9
|
-
test('should properly execute infinite query', async () => {
|
|
10
|
-
const { data, fetchNextPage, status } = useInfiniteQuery({
|
|
11
|
-
queryKey: ['infiniteQuery'],
|
|
12
|
-
queryFn: infiniteFetcher,
|
|
13
|
-
initialPageParam: 0,
|
|
14
|
-
getNextPageParam: () => 12,
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
expect(data.value).toStrictEqual(undefined)
|
|
18
|
-
expect(status.value).toStrictEqual('pending')
|
|
19
|
-
|
|
20
|
-
await flushPromises()
|
|
21
|
-
|
|
22
|
-
expect(data.value).toStrictEqual({
|
|
23
|
-
pageParams: [0],
|
|
24
|
-
pages: ['data on page 0'],
|
|
25
|
-
})
|
|
26
|
-
expect(status.value).toStrictEqual('success')
|
|
27
|
-
|
|
28
|
-
fetchNextPage()
|
|
29
|
-
|
|
30
|
-
await flushPromises()
|
|
31
|
-
|
|
32
|
-
expect(data.value).toStrictEqual({
|
|
33
|
-
pageParams: [0, 12],
|
|
34
|
-
pages: ['data on page 0', 'data on page 12'],
|
|
35
|
-
})
|
|
36
|
-
expect(status.value).toStrictEqual('success')
|
|
37
|
-
})
|
|
38
|
-
test('should properly execute infinite query using infiniteQueryOptions', async () => {
|
|
39
|
-
const options = infiniteQueryOptions({
|
|
40
|
-
queryKey: ['infiniteQueryOptions'],
|
|
41
|
-
queryFn: infiniteFetcher,
|
|
42
|
-
initialPageParam: 0,
|
|
43
|
-
getNextPageParam: () => 12,
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
const { data, fetchNextPage, status } = useInfiniteQuery(options)
|
|
47
|
-
|
|
48
|
-
expect(data.value).toStrictEqual(undefined)
|
|
49
|
-
expect(status.value).toStrictEqual('pending')
|
|
50
|
-
|
|
51
|
-
await flushPromises()
|
|
52
|
-
|
|
53
|
-
expect(data.value).toStrictEqual({
|
|
54
|
-
pageParams: [0],
|
|
55
|
-
pages: ['data on page 0'],
|
|
56
|
-
})
|
|
57
|
-
expect(status.value).toStrictEqual('success')
|
|
58
|
-
|
|
59
|
-
fetchNextPage()
|
|
60
|
-
|
|
61
|
-
await flushPromises()
|
|
62
|
-
|
|
63
|
-
expect(data.value).toStrictEqual({
|
|
64
|
-
pageParams: [0, 12],
|
|
65
|
-
pages: ['data on page 0', 'data on page 12'],
|
|
66
|
-
})
|
|
67
|
-
expect(status.value).toStrictEqual('success')
|
|
68
|
-
})
|
|
69
|
-
})
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test, vi } from 'vitest'
|
|
2
|
-
import { onScopeDispose, reactive } from 'vue-demi'
|
|
3
|
-
import { useQuery } from '../useQuery'
|
|
4
|
-
import { useIsFetching } from '../useIsFetching'
|
|
5
|
-
import { flushPromises, simpleFetcher } from './test-utils'
|
|
6
|
-
import type { MockedFunction } from 'vitest'
|
|
7
|
-
|
|
8
|
-
vi.mock('../useQueryClient')
|
|
9
|
-
|
|
10
|
-
describe('useIsFetching', () => {
|
|
11
|
-
test('should properly return isFetching state', async () => {
|
|
12
|
-
const { isFetching: isFetchingQuery } = useQuery({
|
|
13
|
-
queryKey: ['isFetching1'],
|
|
14
|
-
queryFn: simpleFetcher,
|
|
15
|
-
})
|
|
16
|
-
useQuery({ queryKey: ['isFetching2'], queryFn: simpleFetcher })
|
|
17
|
-
const isFetching = useIsFetching()
|
|
18
|
-
|
|
19
|
-
expect(isFetchingQuery.value).toStrictEqual(true)
|
|
20
|
-
expect(isFetching.value).toStrictEqual(2)
|
|
21
|
-
|
|
22
|
-
await flushPromises()
|
|
23
|
-
|
|
24
|
-
expect(isFetchingQuery.value).toStrictEqual(false)
|
|
25
|
-
expect(isFetching.value).toStrictEqual(0)
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
test('should stop listening to changes on onScopeDispose', async () => {
|
|
29
|
-
const onScopeDisposeMock = onScopeDispose as MockedFunction<
|
|
30
|
-
typeof onScopeDispose
|
|
31
|
-
>
|
|
32
|
-
onScopeDisposeMock.mockImplementation((fn) => fn())
|
|
33
|
-
|
|
34
|
-
const { status } = useQuery({
|
|
35
|
-
queryKey: ['onScopeDispose'],
|
|
36
|
-
queryFn: simpleFetcher,
|
|
37
|
-
})
|
|
38
|
-
const isFetching = useIsFetching()
|
|
39
|
-
|
|
40
|
-
expect(status.value).toStrictEqual('pending')
|
|
41
|
-
expect(isFetching.value).toStrictEqual(1)
|
|
42
|
-
|
|
43
|
-
await flushPromises()
|
|
44
|
-
|
|
45
|
-
expect(status.value).toStrictEqual('pending')
|
|
46
|
-
expect(isFetching.value).toStrictEqual(1)
|
|
47
|
-
|
|
48
|
-
await flushPromises()
|
|
49
|
-
|
|
50
|
-
expect(status.value).toStrictEqual('pending')
|
|
51
|
-
expect(isFetching.value).toStrictEqual(1)
|
|
52
|
-
|
|
53
|
-
onScopeDisposeMock.mockReset()
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
test('should properly update filters', async () => {
|
|
57
|
-
const filter = reactive({ stale: false })
|
|
58
|
-
useQuery({
|
|
59
|
-
queryKey: ['isFetching'],
|
|
60
|
-
queryFn: () =>
|
|
61
|
-
new Promise((resolve) => {
|
|
62
|
-
setTimeout(() => {
|
|
63
|
-
return resolve('Some data')
|
|
64
|
-
}, 100)
|
|
65
|
-
}),
|
|
66
|
-
})
|
|
67
|
-
const isFetching = useIsFetching(filter)
|
|
68
|
-
|
|
69
|
-
expect(isFetching.value).toStrictEqual(0)
|
|
70
|
-
|
|
71
|
-
filter.stale = true
|
|
72
|
-
await flushPromises()
|
|
73
|
-
|
|
74
|
-
expect(isFetching.value).toStrictEqual(1)
|
|
75
|
-
})
|
|
76
|
-
})
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, test, vi } from 'vitest'
|
|
2
|
-
import { onScopeDispose, reactive } from 'vue-demi'
|
|
3
|
-
import { useMutation } from '../useMutation'
|
|
4
|
-
import { useIsMutating, useMutationState } from '../useMutationState'
|
|
5
|
-
import { useQueryClient } from '../useQueryClient'
|
|
6
|
-
import { flushPromises, successMutator } from './test-utils'
|
|
7
|
-
import type { MockedFunction } from 'vitest'
|
|
8
|
-
|
|
9
|
-
vi.mock('../useQueryClient')
|
|
10
|
-
|
|
11
|
-
describe('useIsMutating', () => {
|
|
12
|
-
test('should properly return isMutating state', async () => {
|
|
13
|
-
const mutation = useMutation({
|
|
14
|
-
mutationFn: (params: string) => successMutator(params),
|
|
15
|
-
})
|
|
16
|
-
const mutation2 = useMutation({
|
|
17
|
-
mutationFn: (params: string) => successMutator(params),
|
|
18
|
-
})
|
|
19
|
-
const isMutating = useIsMutating()
|
|
20
|
-
|
|
21
|
-
expect(isMutating.value).toStrictEqual(0)
|
|
22
|
-
|
|
23
|
-
mutation.mutateAsync('a')
|
|
24
|
-
mutation2.mutateAsync('b')
|
|
25
|
-
|
|
26
|
-
await flushPromises()
|
|
27
|
-
|
|
28
|
-
expect(isMutating.value).toStrictEqual(2)
|
|
29
|
-
|
|
30
|
-
await flushPromises()
|
|
31
|
-
|
|
32
|
-
expect(isMutating.value).toStrictEqual(0)
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
test('should stop listening to changes on onScopeDispose', async () => {
|
|
36
|
-
const onScopeDisposeMock = onScopeDispose as MockedFunction<
|
|
37
|
-
typeof onScopeDispose
|
|
38
|
-
>
|
|
39
|
-
onScopeDisposeMock.mockImplementation((fn) => fn())
|
|
40
|
-
|
|
41
|
-
const mutation = useMutation({
|
|
42
|
-
mutationFn: (params: string) => successMutator(params),
|
|
43
|
-
})
|
|
44
|
-
const mutation2 = useMutation({
|
|
45
|
-
mutationFn: (params: string) => successMutator(params),
|
|
46
|
-
})
|
|
47
|
-
const isMutating = useIsMutating()
|
|
48
|
-
|
|
49
|
-
expect(isMutating.value).toStrictEqual(0)
|
|
50
|
-
|
|
51
|
-
mutation.mutateAsync('a')
|
|
52
|
-
mutation2.mutateAsync('b')
|
|
53
|
-
|
|
54
|
-
await flushPromises()
|
|
55
|
-
|
|
56
|
-
expect(isMutating.value).toStrictEqual(0)
|
|
57
|
-
|
|
58
|
-
await flushPromises()
|
|
59
|
-
|
|
60
|
-
expect(isMutating.value).toStrictEqual(0)
|
|
61
|
-
|
|
62
|
-
onScopeDisposeMock.mockReset()
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
test('should properly update filters', async () => {
|
|
66
|
-
const filter = reactive({ mutationKey: ['foo'] })
|
|
67
|
-
const { mutate } = useMutation({
|
|
68
|
-
mutationKey: ['isMutating'],
|
|
69
|
-
mutationFn: (params: string) => successMutator(params),
|
|
70
|
-
})
|
|
71
|
-
mutate('foo')
|
|
72
|
-
|
|
73
|
-
const isMutating = useIsMutating(filter)
|
|
74
|
-
|
|
75
|
-
expect(isMutating.value).toStrictEqual(0)
|
|
76
|
-
|
|
77
|
-
filter.mutationKey = ['isMutating']
|
|
78
|
-
|
|
79
|
-
await flushPromises()
|
|
80
|
-
|
|
81
|
-
expect(isMutating.value).toStrictEqual(1)
|
|
82
|
-
})
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
describe('useMutationState', () => {
|
|
86
|
-
it('should return variables after calling mutate 1', async () => {
|
|
87
|
-
const mutationKey = ['mutation']
|
|
88
|
-
const variables = 'foo123'
|
|
89
|
-
|
|
90
|
-
const { mutate } = useMutation({
|
|
91
|
-
mutationKey: mutationKey,
|
|
92
|
-
mutationFn: (params: string) => successMutator(params),
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
mutate(variables)
|
|
96
|
-
|
|
97
|
-
const mutationState = useMutationState({
|
|
98
|
-
filters: { mutationKey, status: 'pending' },
|
|
99
|
-
select: (mutation) => mutation.state.variables,
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
expect(mutationState.value).toEqual([variables])
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
it('should return variables after calling mutate 2', async () => {
|
|
106
|
-
const queryClient = useQueryClient()
|
|
107
|
-
queryClient.clear()
|
|
108
|
-
const mutationKey = ['mutation']
|
|
109
|
-
const variables = 'bar234'
|
|
110
|
-
|
|
111
|
-
const { mutate } = useMutation({
|
|
112
|
-
mutationKey: mutationKey,
|
|
113
|
-
mutationFn: (params: string) => successMutator(params),
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
mutate(variables)
|
|
117
|
-
|
|
118
|
-
const mutationState = useMutationState()
|
|
119
|
-
|
|
120
|
-
expect(mutationState.value[0]?.variables).toEqual(variables)
|
|
121
|
-
})
|
|
122
|
-
})
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { describe, expectTypeOf, it } from 'vitest'
|
|
2
|
-
import { reactive } from 'vue-demi'
|
|
3
|
-
import { useMutation } from '../useMutation'
|
|
4
|
-
import { successMutator } from './test-utils'
|
|
5
|
-
|
|
6
|
-
describe('Discriminated union return type', () => {
|
|
7
|
-
it('data should be possibly undefined by default', () => {
|
|
8
|
-
const mutation = reactive(
|
|
9
|
-
useMutation({ mutationFn: successMutator<string> }),
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
it('data should be defined when mutation is success', () => {
|
|
16
|
-
const mutation = reactive(
|
|
17
|
-
useMutation({ mutationFn: successMutator<string> }),
|
|
18
|
-
)
|
|
19
|
-
|
|
20
|
-
if (mutation.isSuccess) {
|
|
21
|
-
expectTypeOf(mutation.data).toEqualTypeOf<string>()
|
|
22
|
-
}
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it('error should be null when mutation is success', () => {
|
|
26
|
-
const mutation = reactive(
|
|
27
|
-
useMutation({ mutationFn: successMutator<string> }),
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
if (mutation.isSuccess) {
|
|
31
|
-
expectTypeOf(mutation.error).toEqualTypeOf<null>()
|
|
32
|
-
}
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
it('data should be undefined when mutation is pending', () => {
|
|
36
|
-
const mutation = reactive(
|
|
37
|
-
useMutation({ mutationFn: successMutator<string> }),
|
|
38
|
-
)
|
|
39
|
-
|
|
40
|
-
if (mutation.isPending) {
|
|
41
|
-
expectTypeOf(mutation.data).toEqualTypeOf<undefined>()
|
|
42
|
-
}
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
it('error should be defined when mutation is error', () => {
|
|
46
|
-
const mutation = reactive(
|
|
47
|
-
useMutation({ mutationFn: successMutator<string> }),
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
if (mutation.isError) {
|
|
51
|
-
expectTypeOf(mutation.error).toEqualTypeOf<Error>()
|
|
52
|
-
}
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
it('should narrow variables', () => {
|
|
56
|
-
const mutation = reactive(
|
|
57
|
-
useMutation({ mutationFn: successMutator<string> }),
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
if (mutation.isIdle) {
|
|
61
|
-
expectTypeOf(mutation.variables).toEqualTypeOf<undefined>()
|
|
62
|
-
return
|
|
63
|
-
}
|
|
64
|
-
if (mutation.isPending) {
|
|
65
|
-
expectTypeOf(mutation.variables).toEqualTypeOf<string>()
|
|
66
|
-
return
|
|
67
|
-
}
|
|
68
|
-
if (mutation.isSuccess) {
|
|
69
|
-
expectTypeOf(mutation.variables).toEqualTypeOf<string>()
|
|
70
|
-
return
|
|
71
|
-
}
|
|
72
|
-
expectTypeOf(mutation.variables).toEqualTypeOf<string>()
|
|
73
|
-
})
|
|
74
|
-
})
|
|
@@ -1,355 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, test, vi } from 'vitest'
|
|
2
|
-
import { reactive, ref } from 'vue-demi'
|
|
3
|
-
import { useMutation } from '../useMutation'
|
|
4
|
-
import { useQueryClient } from '../useQueryClient'
|
|
5
|
-
import { errorMutator, flushPromises, successMutator } from './test-utils'
|
|
6
|
-
|
|
7
|
-
vi.mock('../useQueryClient')
|
|
8
|
-
|
|
9
|
-
describe('useMutation', () => {
|
|
10
|
-
test('should be in idle state initially', () => {
|
|
11
|
-
const mutation = useMutation({
|
|
12
|
-
mutationFn: (params) => successMutator(params),
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
expect(mutation).toMatchObject({
|
|
16
|
-
isIdle: { value: true },
|
|
17
|
-
isPending: { value: false },
|
|
18
|
-
isError: { value: false },
|
|
19
|
-
isSuccess: { value: false },
|
|
20
|
-
})
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
test('should change state after invoking mutate', () => {
|
|
24
|
-
const result = 'Mock data'
|
|
25
|
-
const mutation = useMutation({
|
|
26
|
-
mutationFn: (params: string) => successMutator(params),
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
mutation.mutate(result)
|
|
30
|
-
|
|
31
|
-
expect(mutation).toMatchObject({
|
|
32
|
-
isIdle: { value: false },
|
|
33
|
-
isPending: { value: true },
|
|
34
|
-
isError: { value: false },
|
|
35
|
-
isSuccess: { value: false },
|
|
36
|
-
data: { value: undefined },
|
|
37
|
-
error: { value: null },
|
|
38
|
-
})
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
test('should return error when request fails', async () => {
|
|
42
|
-
const mutation = useMutation({ mutationFn: errorMutator })
|
|
43
|
-
mutation.mutate({})
|
|
44
|
-
await flushPromises(10)
|
|
45
|
-
expect(mutation).toMatchObject({
|
|
46
|
-
isIdle: { value: false },
|
|
47
|
-
isPending: { value: false },
|
|
48
|
-
isError: { value: true },
|
|
49
|
-
isSuccess: { value: false },
|
|
50
|
-
data: { value: undefined },
|
|
51
|
-
error: { value: Error('Some error') },
|
|
52
|
-
})
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
test('should return data when request succeeds', async () => {
|
|
56
|
-
const result = 'Mock data'
|
|
57
|
-
const mutation = useMutation({
|
|
58
|
-
mutationFn: (params: string) => successMutator(params),
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
mutation.mutate(result)
|
|
62
|
-
|
|
63
|
-
await flushPromises(10)
|
|
64
|
-
|
|
65
|
-
expect(mutation).toMatchObject({
|
|
66
|
-
isIdle: { value: false },
|
|
67
|
-
isPending: { value: false },
|
|
68
|
-
isError: { value: false },
|
|
69
|
-
isSuccess: { value: true },
|
|
70
|
-
data: { value: 'Mock data' },
|
|
71
|
-
error: { value: null },
|
|
72
|
-
})
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
test('should update reactive options', async () => {
|
|
76
|
-
const queryClient = useQueryClient()
|
|
77
|
-
const mutationCache = queryClient.getMutationCache()
|
|
78
|
-
const options = reactive({
|
|
79
|
-
mutationKey: ['foo'],
|
|
80
|
-
mutationFn: (params: string) => successMutator(params),
|
|
81
|
-
})
|
|
82
|
-
const mutation = useMutation(options)
|
|
83
|
-
|
|
84
|
-
options.mutationKey = ['bar']
|
|
85
|
-
await flushPromises()
|
|
86
|
-
mutation.mutate('xyz')
|
|
87
|
-
|
|
88
|
-
await flushPromises()
|
|
89
|
-
|
|
90
|
-
const mutations = mutationCache.find({ mutationKey: ['bar'] })
|
|
91
|
-
|
|
92
|
-
expect(mutations?.options.mutationKey).toEqual(['bar'])
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
test('should update reactive options deeply', async () => {
|
|
96
|
-
type MutationKeyTest = {
|
|
97
|
-
entity: string
|
|
98
|
-
otherObject: {
|
|
99
|
-
name: string
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
const mutationKey = ref<Array<MutationKeyTest>>([
|
|
103
|
-
{
|
|
104
|
-
entity: 'test',
|
|
105
|
-
otherObject: { name: 'objectName' },
|
|
106
|
-
},
|
|
107
|
-
])
|
|
108
|
-
const queryClient = useQueryClient()
|
|
109
|
-
const mutationCache = queryClient.getMutationCache()
|
|
110
|
-
const options = reactive({
|
|
111
|
-
mutationKey,
|
|
112
|
-
mutationFn: (params: string) => successMutator(params),
|
|
113
|
-
})
|
|
114
|
-
const mutation = useMutation(options)
|
|
115
|
-
|
|
116
|
-
mutationKey.value[0]!.otherObject.name = 'someOtherObjectName'
|
|
117
|
-
await flushPromises()
|
|
118
|
-
mutation.mutate('xyz')
|
|
119
|
-
|
|
120
|
-
await flushPromises()
|
|
121
|
-
|
|
122
|
-
const mutations = mutationCache.getAll()
|
|
123
|
-
const relevantMutation = mutations.find((m) => {
|
|
124
|
-
return (
|
|
125
|
-
Array.isArray(m.options.mutationKey) &&
|
|
126
|
-
!!m.options.mutationKey[0].otherObject
|
|
127
|
-
)
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
expect(
|
|
131
|
-
(relevantMutation?.options.mutationKey as Array<MutationKeyTest>)[0]
|
|
132
|
-
?.otherObject.name === 'someOtherObjectName',
|
|
133
|
-
).toBe(true)
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
test('should allow for non-options object (mutationFn or mutationKey) passed as arg1 & arg2 to trigger reactive updates', async () => {
|
|
137
|
-
const mutationKey = ref<Array<string>>(['foo2'])
|
|
138
|
-
const mutationFn = ref((params: string) => successMutator(params))
|
|
139
|
-
const queryClient = useQueryClient()
|
|
140
|
-
const mutationCache = queryClient.getMutationCache()
|
|
141
|
-
const mutation = useMutation({ mutationKey, mutationFn })
|
|
142
|
-
|
|
143
|
-
mutationKey.value = ['bar2']
|
|
144
|
-
let proof = false
|
|
145
|
-
mutationFn.value = (params: string) => {
|
|
146
|
-
proof = true
|
|
147
|
-
return successMutator(params)
|
|
148
|
-
}
|
|
149
|
-
await flushPromises()
|
|
150
|
-
|
|
151
|
-
mutation.mutate('xyz')
|
|
152
|
-
await flushPromises()
|
|
153
|
-
|
|
154
|
-
const mutations = mutationCache.find({ mutationKey: ['bar2'] })
|
|
155
|
-
expect(mutations?.options.mutationKey).toEqual(['bar2'])
|
|
156
|
-
expect(proof).toEqual(true)
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
test('should reset state after invoking mutation.reset', async () => {
|
|
160
|
-
const mutation = useMutation({
|
|
161
|
-
mutationFn: (params: string) => errorMutator(params),
|
|
162
|
-
})
|
|
163
|
-
|
|
164
|
-
mutation.mutate('')
|
|
165
|
-
|
|
166
|
-
await flushPromises(10)
|
|
167
|
-
|
|
168
|
-
mutation.reset()
|
|
169
|
-
|
|
170
|
-
expect(mutation).toMatchObject({
|
|
171
|
-
isIdle: { value: true },
|
|
172
|
-
isPending: { value: false },
|
|
173
|
-
isError: { value: false },
|
|
174
|
-
isSuccess: { value: false },
|
|
175
|
-
data: { value: undefined },
|
|
176
|
-
error: { value: null },
|
|
177
|
-
})
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
describe('side effects', () => {
|
|
181
|
-
beforeEach(() => {
|
|
182
|
-
vi.clearAllMocks()
|
|
183
|
-
})
|
|
184
|
-
|
|
185
|
-
test('should call onMutate when passed as an option', async () => {
|
|
186
|
-
const onMutate = vi.fn()
|
|
187
|
-
const mutation = useMutation({
|
|
188
|
-
mutationFn: (params: string) => successMutator(params),
|
|
189
|
-
onMutate,
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
mutation.mutate('')
|
|
193
|
-
|
|
194
|
-
await flushPromises(10)
|
|
195
|
-
|
|
196
|
-
expect(onMutate).toHaveBeenCalledTimes(1)
|
|
197
|
-
})
|
|
198
|
-
|
|
199
|
-
test('should call onError when passed as an option', async () => {
|
|
200
|
-
const onError = vi.fn()
|
|
201
|
-
const mutation = useMutation({
|
|
202
|
-
mutationFn: (params: string) => errorMutator(params),
|
|
203
|
-
onError,
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
mutation.mutate('')
|
|
207
|
-
|
|
208
|
-
await flushPromises(10)
|
|
209
|
-
|
|
210
|
-
expect(onError).toHaveBeenCalledTimes(1)
|
|
211
|
-
})
|
|
212
|
-
|
|
213
|
-
test('should call onSuccess when passed as an option', async () => {
|
|
214
|
-
const onSuccess = vi.fn()
|
|
215
|
-
const mutation = useMutation({
|
|
216
|
-
mutationFn: (params: string) => successMutator(params),
|
|
217
|
-
onSuccess,
|
|
218
|
-
})
|
|
219
|
-
|
|
220
|
-
mutation.mutate('')
|
|
221
|
-
|
|
222
|
-
await flushPromises(10)
|
|
223
|
-
|
|
224
|
-
expect(onSuccess).toHaveBeenCalledTimes(1)
|
|
225
|
-
})
|
|
226
|
-
|
|
227
|
-
test('should call onSettled when passed as an option', async () => {
|
|
228
|
-
const onSettled = vi.fn()
|
|
229
|
-
const mutation = useMutation({
|
|
230
|
-
mutationFn: (params: string) => successMutator(params),
|
|
231
|
-
onSettled,
|
|
232
|
-
})
|
|
233
|
-
|
|
234
|
-
mutation.mutate('')
|
|
235
|
-
|
|
236
|
-
await flushPromises(10)
|
|
237
|
-
|
|
238
|
-
expect(onSettled).toHaveBeenCalledTimes(1)
|
|
239
|
-
})
|
|
240
|
-
|
|
241
|
-
test('should call onError when passed as an argument of mutate function', async () => {
|
|
242
|
-
const onError = vi.fn()
|
|
243
|
-
const mutation = useMutation({
|
|
244
|
-
mutationFn: (params: string) => errorMutator(params),
|
|
245
|
-
})
|
|
246
|
-
|
|
247
|
-
mutation.mutate('', { onError })
|
|
248
|
-
|
|
249
|
-
await flushPromises(10)
|
|
250
|
-
|
|
251
|
-
expect(onError).toHaveBeenCalledTimes(1)
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
test('should call onSuccess when passed as an argument of mutate function', async () => {
|
|
255
|
-
const onSuccess = vi.fn()
|
|
256
|
-
const mutation = useMutation({
|
|
257
|
-
mutationFn: (params: string) => successMutator(params),
|
|
258
|
-
})
|
|
259
|
-
|
|
260
|
-
mutation.mutate('', { onSuccess })
|
|
261
|
-
|
|
262
|
-
await flushPromises(10)
|
|
263
|
-
|
|
264
|
-
expect(onSuccess).toHaveBeenCalledTimes(1)
|
|
265
|
-
})
|
|
266
|
-
|
|
267
|
-
test('should call onSettled when passed as an argument of mutate function', async () => {
|
|
268
|
-
const onSettled = vi.fn()
|
|
269
|
-
const mutation = useMutation({
|
|
270
|
-
mutationFn: (params: string) => successMutator(params),
|
|
271
|
-
})
|
|
272
|
-
|
|
273
|
-
mutation.mutate('', { onSettled })
|
|
274
|
-
|
|
275
|
-
await flushPromises(10)
|
|
276
|
-
|
|
277
|
-
expect(onSettled).toHaveBeenCalledTimes(1)
|
|
278
|
-
})
|
|
279
|
-
|
|
280
|
-
test('should fire both onSettled functions', async () => {
|
|
281
|
-
const onSettled = vi.fn()
|
|
282
|
-
const onSettledOnFunction = vi.fn()
|
|
283
|
-
const mutation = useMutation({
|
|
284
|
-
mutationFn: (params: string) => successMutator(params),
|
|
285
|
-
onSettled,
|
|
286
|
-
})
|
|
287
|
-
|
|
288
|
-
mutation.mutate('', { onSettled: onSettledOnFunction })
|
|
289
|
-
|
|
290
|
-
await flushPromises(10)
|
|
291
|
-
|
|
292
|
-
expect(onSettled).toHaveBeenCalledTimes(1)
|
|
293
|
-
expect(onSettledOnFunction).toHaveBeenCalledTimes(1)
|
|
294
|
-
})
|
|
295
|
-
})
|
|
296
|
-
|
|
297
|
-
describe('async', () => {
|
|
298
|
-
beforeEach(() => {
|
|
299
|
-
vi.clearAllMocks()
|
|
300
|
-
})
|
|
301
|
-
|
|
302
|
-
test('should resolve properly', async () => {
|
|
303
|
-
const result = 'Mock data'
|
|
304
|
-
const mutation = useMutation({
|
|
305
|
-
mutationFn: (params: string) => successMutator(params),
|
|
306
|
-
})
|
|
307
|
-
|
|
308
|
-
await expect(mutation.mutateAsync(result)).resolves.toBe(result)
|
|
309
|
-
|
|
310
|
-
expect(mutation).toMatchObject({
|
|
311
|
-
isIdle: { value: false },
|
|
312
|
-
isPending: { value: false },
|
|
313
|
-
isError: { value: false },
|
|
314
|
-
isSuccess: { value: true },
|
|
315
|
-
data: { value: 'Mock data' },
|
|
316
|
-
error: { value: null },
|
|
317
|
-
})
|
|
318
|
-
})
|
|
319
|
-
|
|
320
|
-
test('should throw on error', async () => {
|
|
321
|
-
const mutation = useMutation({ mutationFn: errorMutator })
|
|
322
|
-
|
|
323
|
-
await expect(mutation.mutateAsync({})).rejects.toThrowError('Some error')
|
|
324
|
-
|
|
325
|
-
expect(mutation).toMatchObject({
|
|
326
|
-
isIdle: { value: false },
|
|
327
|
-
isPending: { value: false },
|
|
328
|
-
isError: { value: true },
|
|
329
|
-
isSuccess: { value: false },
|
|
330
|
-
data: { value: undefined },
|
|
331
|
-
error: { value: Error('Some error') },
|
|
332
|
-
})
|
|
333
|
-
})
|
|
334
|
-
})
|
|
335
|
-
|
|
336
|
-
describe('throwOnError', () => {
|
|
337
|
-
test('should evaluate throwOnError when mutation is expected to throw', async () => {
|
|
338
|
-
const err = new Error('Expected mock error. All is well!')
|
|
339
|
-
const boundaryFn = vi.fn()
|
|
340
|
-
const { mutate } = useMutation({
|
|
341
|
-
mutationFn: () => {
|
|
342
|
-
return Promise.reject(err)
|
|
343
|
-
},
|
|
344
|
-
throwOnError: boundaryFn,
|
|
345
|
-
})
|
|
346
|
-
|
|
347
|
-
mutate()
|
|
348
|
-
|
|
349
|
-
await flushPromises()
|
|
350
|
-
|
|
351
|
-
expect(boundaryFn).toHaveBeenCalledTimes(1)
|
|
352
|
-
expect(boundaryFn).toHaveBeenCalledWith(err)
|
|
353
|
-
})
|
|
354
|
-
})
|
|
355
|
-
})
|