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