@tanstack/vue-query 5.0.0-alpha.5 → 5.0.0-alpha.50

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.
Files changed (191) hide show
  1. package/README.md +18 -14
  2. package/build/lib/__mocks__/useBaseQuery.d.ts +2 -1
  3. package/build/lib/__mocks__/useBaseQuery.d.ts.map +1 -0
  4. package/build/lib/__mocks__/useQueryClient.d.ts +2 -1
  5. package/build/lib/__mocks__/useQueryClient.d.ts.map +1 -0
  6. package/build/lib/__tests__/mutationCache.test.d.ts +1 -0
  7. package/build/lib/__tests__/mutationCache.test.d.ts.map +1 -0
  8. package/build/lib/__tests__/queryCache.test.d.ts +1 -0
  9. package/build/lib/__tests__/queryCache.test.d.ts.map +1 -0
  10. package/build/lib/__tests__/queryClient.test.d.ts +1 -0
  11. package/build/lib/__tests__/queryClient.test.d.ts.map +1 -0
  12. package/build/lib/__tests__/test-utils.d.ts +3 -2
  13. package/build/lib/__tests__/test-utils.d.ts.map +1 -0
  14. package/build/lib/__tests__/useInfiniteQuery.test.d.ts +1 -0
  15. package/build/lib/__tests__/useInfiniteQuery.test.d.ts.map +1 -0
  16. package/build/lib/__tests__/useInfiniteQuery.types.test.d.ts +1 -0
  17. package/build/lib/__tests__/useInfiniteQuery.types.test.d.ts.map +1 -0
  18. package/build/lib/__tests__/useIsFetching.test.d.ts +1 -0
  19. package/build/lib/__tests__/useIsFetching.test.d.ts.map +1 -0
  20. package/build/lib/__tests__/useIsMutating.test.d.ts +1 -0
  21. package/build/lib/__tests__/useIsMutating.test.d.ts.map +1 -0
  22. package/build/lib/__tests__/useMutation.test.d.ts +1 -0
  23. package/build/lib/__tests__/useMutation.test.d.ts.map +1 -0
  24. package/build/lib/__tests__/useMutation.types.test.d.ts +1 -0
  25. package/build/lib/__tests__/useMutation.types.test.d.ts.map +1 -0
  26. package/build/lib/__tests__/useQueries.test.d.ts +1 -0
  27. package/build/lib/__tests__/useQueries.test.d.ts.map +1 -0
  28. package/build/lib/__tests__/useQuery.test.d.ts +1 -0
  29. package/build/lib/__tests__/useQuery.test.d.ts.map +1 -0
  30. package/build/lib/__tests__/useQuery.types.test.d.ts +1 -0
  31. package/build/lib/__tests__/useQuery.types.test.d.ts.map +1 -0
  32. package/build/lib/__tests__/useQueryClient.test.d.ts +1 -0
  33. package/build/lib/__tests__/useQueryClient.test.d.ts.map +1 -0
  34. package/build/lib/__tests__/utils.test.d.ts +1 -0
  35. package/build/lib/__tests__/utils.test.d.ts.map +1 -0
  36. package/build/lib/__tests__/vueQueryPlugin.test.d.ts +1 -0
  37. package/build/lib/__tests__/vueQueryPlugin.test.d.ts.map +1 -0
  38. package/build/lib/devtools/devtools.d.ts +1 -0
  39. package/build/lib/devtools/devtools.d.ts.map +1 -0
  40. package/build/lib/devtools/devtools.js +46 -7
  41. package/build/lib/devtools/devtools.js.map +1 -1
  42. package/build/lib/devtools/devtools.mjs +46 -7
  43. package/build/lib/devtools/devtools.mjs.map +1 -1
  44. package/build/lib/devtools/utils.d.ts +2 -1
  45. package/build/lib/devtools/utils.d.ts.map +1 -0
  46. package/build/lib/devtools/utils.js +3 -4
  47. package/build/lib/devtools/utils.js.map +1 -1
  48. package/build/lib/devtools/utils.mjs +3 -4
  49. package/build/lib/devtools/utils.mjs.map +1 -1
  50. package/build/lib/index.d.ts +3 -2
  51. package/build/lib/index.d.ts.map +1 -0
  52. package/build/lib/index.js +3 -2
  53. package/build/lib/index.js.map +1 -1
  54. package/build/lib/index.mjs +1 -1
  55. package/build/lib/mutationCache.d.ts +1 -0
  56. package/build/lib/mutationCache.d.ts.map +1 -0
  57. package/build/lib/mutationCache.js.map +1 -1
  58. package/build/lib/mutationCache.mjs.map +1 -1
  59. package/build/lib/queryCache.d.ts +1 -0
  60. package/build/lib/queryCache.d.ts.map +1 -0
  61. package/build/lib/queryCache.js.map +1 -1
  62. package/build/lib/queryCache.mjs.map +1 -1
  63. package/build/lib/queryClient.d.ts +1 -0
  64. package/build/lib/queryClient.d.ts.map +1 -0
  65. package/build/lib/queryClient.js.map +1 -1
  66. package/build/lib/queryClient.mjs.map +1 -1
  67. package/build/lib/types.d.ts +4 -3
  68. package/build/lib/types.d.ts.map +1 -0
  69. package/build/lib/useBaseQuery.d.ts +3 -2
  70. package/build/lib/useBaseQuery.d.ts.map +1 -0
  71. package/build/lib/useBaseQuery.js.map +1 -1
  72. package/build/lib/useBaseQuery.mjs.map +1 -1
  73. package/build/lib/useInfiniteQuery.d.ts +4 -3
  74. package/build/lib/useInfiniteQuery.d.ts.map +1 -0
  75. package/build/lib/useInfiniteQuery.js.map +1 -1
  76. package/build/lib/useInfiniteQuery.mjs.map +1 -1
  77. package/build/lib/useIsFetching.d.ts +2 -1
  78. package/build/lib/useIsFetching.d.ts.map +1 -0
  79. package/build/lib/useIsFetching.js.map +1 -1
  80. package/build/lib/useIsFetching.mjs.map +1 -1
  81. package/build/lib/useMutation.d.ts +5 -4
  82. package/build/lib/useMutation.d.ts.map +1 -0
  83. package/build/lib/useMutation.js.map +1 -1
  84. package/build/lib/useMutation.mjs.map +1 -1
  85. package/build/lib/useMutationState.d.ts +12 -0
  86. package/build/lib/useMutationState.d.ts.map +1 -0
  87. package/build/lib/useMutationState.js +36 -0
  88. package/build/lib/useMutationState.js.map +1 -0
  89. package/build/lib/useMutationState.mjs +33 -0
  90. package/build/lib/useMutationState.mjs.map +1 -0
  91. package/build/lib/useQueries.d.ts +12 -11
  92. package/build/lib/useQueries.d.ts.map +1 -0
  93. package/build/lib/useQueries.js +25 -11
  94. package/build/lib/useQueries.js.map +1 -1
  95. package/build/lib/useQueries.mjs +25 -11
  96. package/build/lib/useQueries.mjs.map +1 -1
  97. package/build/lib/useQuery.d.ts +4 -3
  98. package/build/lib/useQuery.d.ts.map +1 -0
  99. package/build/lib/useQuery.js.map +1 -1
  100. package/build/lib/useQuery.mjs.map +1 -1
  101. package/build/lib/useQueryClient.d.ts +1 -0
  102. package/build/lib/useQueryClient.d.ts.map +1 -0
  103. package/build/lib/useQueryClient.js.map +1 -1
  104. package/build/lib/useQueryClient.mjs.map +1 -1
  105. package/build/lib/utils.d.ts +1 -0
  106. package/build/lib/utils.d.ts.map +1 -0
  107. package/build/lib/utils.js.map +1 -1
  108. package/build/lib/utils.mjs.map +1 -1
  109. package/build/lib/vueQueryPlugin.d.ts +3 -2
  110. package/build/lib/vueQueryPlugin.d.ts.map +1 -0
  111. package/build/lib/vueQueryPlugin.js +0 -2
  112. package/build/lib/vueQueryPlugin.js.map +1 -1
  113. package/build/lib/vueQueryPlugin.mjs +0 -2
  114. package/build/lib/vueQueryPlugin.mjs.map +1 -1
  115. package/package.json +16 -13
  116. package/src/__mocks__/useBaseQuery.ts +8 -0
  117. package/src/__mocks__/useQueryClient.ts +10 -0
  118. package/src/__tests__/mutationCache.test.ts +40 -0
  119. package/src/__tests__/queryCache.test.ts +48 -0
  120. package/src/__tests__/queryClient.test.ts +365 -0
  121. package/src/__tests__/test-utils.ts +62 -0
  122. package/src/__tests__/useInfiniteQuery.test.ts +37 -0
  123. package/src/__tests__/useInfiniteQuery.types.test.tsx +102 -0
  124. package/src/__tests__/useIsFetching.test.ts +77 -0
  125. package/src/__tests__/useIsMutating.test.ts +123 -0
  126. package/src/__tests__/useMutation.test.ts +335 -0
  127. package/src/__tests__/useMutation.types.test.tsx +97 -0
  128. package/src/__tests__/useQueries.test.ts +256 -0
  129. package/src/__tests__/useQuery.test.ts +290 -0
  130. package/src/__tests__/useQuery.types.test.tsx +88 -0
  131. package/src/__tests__/useQueryClient.test.ts +51 -0
  132. package/src/__tests__/utils.test.ts +148 -0
  133. package/src/__tests__/vueQueryPlugin.test.ts +351 -0
  134. package/src/devtools/devtools.ts +249 -0
  135. package/src/devtools/utils.ts +96 -0
  136. package/src/index.ts +30 -0
  137. package/src/mutationCache.ts +25 -0
  138. package/src/queryCache.ts +21 -0
  139. package/src/queryClient.ts +278 -0
  140. package/src/types.ts +17 -0
  141. package/src/useBaseQuery.ts +152 -0
  142. package/src/useInfiniteQuery.ts +105 -0
  143. package/src/useIsFetching.ts +37 -0
  144. package/src/useMutation.ts +109 -0
  145. package/src/useMutationState.ts +73 -0
  146. package/src/useQueries.ts +227 -0
  147. package/src/useQuery.ts +125 -0
  148. package/src/useQueryClient.ts +23 -0
  149. package/src/utils.ts +67 -0
  150. package/src/vueQueryPlugin.ts +101 -0
  151. package/build/lib/devtools/devtools.esm.js +0 -155
  152. package/build/lib/devtools/devtools.esm.js.map +0 -1
  153. package/build/lib/devtools/utils.esm.js +0 -80
  154. package/build/lib/devtools/utils.esm.js.map +0 -1
  155. package/build/lib/index.esm.js +0 -14
  156. package/build/lib/index.esm.js.map +0 -1
  157. package/build/lib/mutationCache.esm.js +0 -14
  158. package/build/lib/mutationCache.esm.js.map +0 -1
  159. package/build/lib/queryCache.esm.js +0 -14
  160. package/build/lib/queryCache.esm.js.map +0 -1
  161. package/build/lib/queryClient.esm.js +0 -84
  162. package/build/lib/queryClient.esm.js.map +0 -1
  163. package/build/lib/useBaseQuery.esm.js +0 -67
  164. package/build/lib/useBaseQuery.esm.js.map +0 -1
  165. package/build/lib/useInfiniteQuery.esm.js +0 -18
  166. package/build/lib/useInfiniteQuery.esm.js.map +0 -1
  167. package/build/lib/useIsFetching.esm.js +0 -24
  168. package/build/lib/useIsFetching.esm.js.map +0 -1
  169. package/build/lib/useIsMutating.d.ts +0 -6
  170. package/build/lib/useIsMutating.esm.js +0 -24
  171. package/build/lib/useIsMutating.esm.js.map +0 -1
  172. package/build/lib/useIsMutating.js +0 -26
  173. package/build/lib/useIsMutating.js.map +0 -1
  174. package/build/lib/useIsMutating.mjs +0 -24
  175. package/build/lib/useIsMutating.mjs.map +0 -1
  176. package/build/lib/useMutation.esm.js +0 -39
  177. package/build/lib/useMutation.esm.js.map +0 -1
  178. package/build/lib/useQueries.esm.js +0 -46
  179. package/build/lib/useQueries.esm.js.map +0 -1
  180. package/build/lib/useQuery.esm.js +0 -13
  181. package/build/lib/useQuery.esm.js.map +0 -1
  182. package/build/lib/useQueryClient.esm.js +0 -18
  183. package/build/lib/useQueryClient.esm.js.map +0 -1
  184. package/build/lib/utils.esm.js +0 -50
  185. package/build/lib/utils.esm.js.map +0 -1
  186. package/build/lib/vueQueryPlugin.esm.js +0 -75
  187. package/build/lib/vueQueryPlugin.esm.js.map +0 -1
  188. package/build/umd/index.development.js +0 -4192
  189. package/build/umd/index.development.js.map +0 -1
  190. package/build/umd/index.production.js +0 -2
  191. package/build/umd/index.production.js.map +0 -1
@@ -0,0 +1,123 @@
1
+ import { onScopeDispose, reactive } from 'vue-demi'
2
+
3
+ import { flushPromises, successMutator } from './test-utils'
4
+ import { useMutation } from '../useMutation'
5
+ import { useIsMutating, useMutationState } from '../useMutationState'
6
+ import { vi } from 'vitest'
7
+ import type { MockedFunction } from 'vitest'
8
+ import { useQueryClient } from '../useQueryClient'
9
+
10
+ vi.mock('../useQueryClient')
11
+
12
+ describe('useIsMutating', () => {
13
+ test('should properly return isMutating state', async () => {
14
+ const mutation = useMutation({
15
+ mutationFn: (params: string) => successMutator(params),
16
+ })
17
+ const mutation2 = useMutation({
18
+ mutationFn: (params: string) => successMutator(params),
19
+ })
20
+ const isMutating = useIsMutating()
21
+
22
+ expect(isMutating.value).toStrictEqual(0)
23
+
24
+ mutation.mutateAsync('a')
25
+ mutation2.mutateAsync('b')
26
+
27
+ await flushPromises()
28
+
29
+ expect(isMutating.value).toStrictEqual(2)
30
+
31
+ await flushPromises()
32
+
33
+ expect(isMutating.value).toStrictEqual(0)
34
+ })
35
+
36
+ test('should stop listening to changes on onScopeDispose', async () => {
37
+ const onScopeDisposeMock = onScopeDispose as MockedFunction<
38
+ typeof onScopeDispose
39
+ >
40
+ onScopeDisposeMock.mockImplementation((fn) => fn())
41
+
42
+ const mutation = useMutation({
43
+ mutationFn: (params: string) => successMutator(params),
44
+ })
45
+ const mutation2 = useMutation({
46
+ mutationFn: (params: string) => successMutator(params),
47
+ })
48
+ const isMutating = useIsMutating()
49
+
50
+ expect(isMutating.value).toStrictEqual(0)
51
+
52
+ mutation.mutateAsync('a')
53
+ mutation2.mutateAsync('b')
54
+
55
+ await flushPromises()
56
+
57
+ expect(isMutating.value).toStrictEqual(0)
58
+
59
+ await flushPromises()
60
+
61
+ expect(isMutating.value).toStrictEqual(0)
62
+
63
+ onScopeDisposeMock.mockReset()
64
+ })
65
+
66
+ test('should properly update filters', async () => {
67
+ const filter = reactive({ mutationKey: ['foo'] })
68
+ const { mutate } = useMutation({
69
+ mutationKey: ['isMutating'],
70
+ mutationFn: (params: string) => successMutator(params),
71
+ })
72
+ mutate('foo')
73
+
74
+ const isMutating = useIsMutating(filter)
75
+
76
+ expect(isMutating.value).toStrictEqual(0)
77
+
78
+ filter.mutationKey = ['isMutating']
79
+
80
+ await flushPromises()
81
+
82
+ expect(isMutating.value).toStrictEqual(1)
83
+ })
84
+ })
85
+
86
+ describe('useMutationState', () => {
87
+ it('should return variables after calling mutate', async () => {
88
+ const mutationKey = ['mutation']
89
+ const variables = 'foo123'
90
+
91
+ const { mutate } = useMutation({
92
+ mutationKey: mutationKey,
93
+ mutationFn: (params: string) => successMutator(params),
94
+ })
95
+
96
+ mutate(variables)
97
+
98
+ const mutationState = useMutationState({
99
+ filters: { mutationKey, status: 'pending' },
100
+ select: (mutation) => mutation.state.variables,
101
+ })
102
+
103
+ expect(mutationState.value).toEqual([variables])
104
+ })
105
+
106
+ it('should return variables after calling mutate', async () => {
107
+ const queryClient = useQueryClient()
108
+ queryClient.clear()
109
+ const mutationKey = ['mutation']
110
+ const variables = 'bar234'
111
+
112
+ const { mutate } = useMutation({
113
+ mutationKey: mutationKey,
114
+ mutationFn: (params: string) => successMutator(params),
115
+ })
116
+
117
+ mutate(variables)
118
+
119
+ const mutationState = useMutationState()
120
+
121
+ expect(mutationState.value[0]).toContain({ variables: variables })
122
+ })
123
+ })
@@ -0,0 +1,335 @@
1
+ import { reactive, ref } from 'vue-demi'
2
+ import { errorMutator, flushPromises, successMutator } from './test-utils'
3
+ import { useMutation } from '../useMutation'
4
+ import { useQueryClient } from '../useQueryClient'
5
+ import { vi } from 'vitest'
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<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 MutationKeyTest[])[0]
132
+ ?.otherObject.name === 'someOtherObjectName',
133
+ )
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<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
+ })
@@ -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
+ })