Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useMutation.test.ts

334 lines9.0 KB

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