Open History to see the last commit
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})