Open History to see the last commit
1import { reactive } from 'vue-demi'
2import { errorMutator, flushPromises, successMutator } from './test-utils'
3import { parseMutationArgs, 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((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β
39 mutation.mutate()
40β
41 await flushPromises(10)
42β
43 expect(mutation).toMatchObject({
44 isIdle: { value: false },
45 isLoading: { value: false },
46 isError: { value: true },
47 isSuccess: { value: false },
48 data: { value: undefined },
49 error: { value: Error('Some error') },
50 })
51 })
52β
53 test('should return data when request succeeds', async () => {
54 const result = 'Mock data'
55 const mutation = useMutation((params: string) => successMutator(params))
56β
57 mutation.mutate(result)
58β
59 await flushPromises(10)
60β
61 expect(mutation).toMatchObject({
62 isIdle: { value: false },
63 isLoading: { value: false },
64 isError: { value: false },
65 isSuccess: { value: true },
66 data: { value: 'Mock data' },
67 error: { value: null },
68 })
69 })
70β
71 test('should update reactive options', async () => {
72 const queryClient = useQueryClient()
73 const mutationCache = queryClient.getMutationCache()
74 const options = reactive({ mutationKey: ['foo'] })
75 const mutation = useMutation(
76 (params: string) => successMutator(params),
77 options,
78 )
79β
80 options.mutationKey = ['bar']
81 await flushPromises()
82 mutation.mutate('xyz')
83β
84 await flushPromises()
85β
86 const mutations = mutationCache.find({ mutationKey: ['bar'] })
87β
88 expect(mutations?.options.mutationKey).toEqual(['bar'])
89 })
90β
91 test('should reset state after invoking mutation.reset', async () => {
92 const mutation = useMutation((params: string) => errorMutator(params))
93β
94 mutation.mutate('')
95β
96 await flushPromises(10)
97β
98 mutation.reset()
99β
100 expect(mutation).toMatchObject({
101 isIdle: { value: true },
102 isLoading: { value: false },
103 isError: { value: false },
104 isSuccess: { value: false },
105 data: { value: undefined },
106 error: { value: null },
107 })
108 })
109β
110 describe('side effects', () => {
111 beforeEach(() => {
112 jest.clearAllMocks()
113 })
114β
115 test('should call onMutate when passed as an option', async () => {
116 const onMutate = jest.fn()
117 const mutation = useMutation((params: string) => successMutator(params), {
118 onMutate,
119 })
120β
121 mutation.mutate('')
122β
123 await flushPromises(10)
124β
125 expect(onMutate).toHaveBeenCalledTimes(1)
126 })
127β
128 test('should call onError when passed as an option', async () => {
129 const onError = jest.fn()
130 const mutation = useMutation((params: string) => errorMutator(params), {
131 onError,
132 })
133β
134 mutation.mutate('')
135β
136 await flushPromises(10)
137β
138 expect(onError).toHaveBeenCalledTimes(1)
139 })
140β
141 test('should call onSuccess when passed as an option', async () => {
142 const onSuccess = jest.fn()
143 const mutation = useMutation((params: string) => successMutator(params), {
144 onSuccess,
145 })
146β
147 mutation.mutate('')
148β
149 await flushPromises(10)
150β
151 expect(onSuccess).toHaveBeenCalledTimes(1)
152 })
153β
154 test('should call onSettled when passed as an option', async () => {
155 const onSettled = jest.fn()
156 const mutation = useMutation((params: string) => successMutator(params), {
157 onSettled,
158 })
159β
160 mutation.mutate('')
161β
162 await flushPromises(10)
163β
164 expect(onSettled).toHaveBeenCalledTimes(1)
165 })
166β
167 test('should call onError when passed as an argument of mutate function', async () => {
168 const onError = jest.fn()
169 const mutation = useMutation((params: string) => errorMutator(params))
170β
171 mutation.mutate('', { onError })
172β
173 await flushPromises(10)
174β
175 expect(onError).toHaveBeenCalledTimes(1)
176 })
177β
178 test('should call onSuccess when passed as an argument of mutate function', async () => {
179 const onSuccess = jest.fn()
180 const mutation = useMutation((params: string) => successMutator(params))
181β
182 mutation.mutate('', { onSuccess })
183β
184 await flushPromises(10)
185β
186 expect(onSuccess).toHaveBeenCalledTimes(1)
187 })
188β
189 test('should call onSettled when passed as an argument of mutate function', async () => {
190 const onSettled = jest.fn()
191 const mutation = useMutation((params: string) => successMutator(params))
192β
193 mutation.mutate('', { onSettled })
194β
195 await flushPromises(10)
196β
197 expect(onSettled).toHaveBeenCalledTimes(1)
198 })
199β
200 test('should fire both onSettled functions', async () => {
201 const onSettled = jest.fn()
202 const onSettledOnFunction = jest.fn()
203 const mutation = useMutation((params: string) => successMutator(params), {
204 onSettled,
205 })
206β
207 mutation.mutate('', { onSettled: onSettledOnFunction })
208β
209 await flushPromises(10)
210β
211 expect(onSettled).toHaveBeenCalledTimes(1)
212 expect(onSettledOnFunction).toHaveBeenCalledTimes(1)
213 })
214 })
215β
216 describe('async', () => {
217 beforeEach(() => {
218 jest.clearAllMocks()
219 })
220β
221 test('should resolve properly', async () => {
222 const result = 'Mock data'
223 const mutation = useMutation((params: string) => successMutator(params))
224β
225 await expect(mutation.mutateAsync(result)).resolves.toBe(result)
226β
227 expect(mutation).toMatchObject({
228 isIdle: { value: false },
229 isLoading: { value: false },
230 isError: { value: false },
231 isSuccess: { value: true },
232 data: { value: 'Mock data' },
233 error: { value: null },
234 })
235 })
236β
237 test('should throw on error', async () => {
238 const mutation = useMutation(errorMutator)
239β
240 await expect(mutation.mutateAsync()).rejects.toThrowError('Some error')
241β
242 expect(mutation).toMatchObject({
243 isIdle: { value: false },
244 isLoading: { value: false },
245 isError: { value: true },
246 isSuccess: { value: false },
247 data: { value: undefined },
248 error: { value: Error('Some error') },
249 })
250 })
251 })
252β
253 describe('parseMutationArgs', () => {
254 test('should return the same instance of options', () => {
255 const options = { retry: false }
256 const result = parseMutationArgs(options)
257β
258 expect(result).toEqual(options)
259 })
260β
261 test('should merge query key with options', () => {
262 const options = { retry: false }
263 const result = parseMutationArgs(['key'], options)
264 const expected = { ...options, mutationKey: ['key'] }
265β
266 expect(result).toEqual(expected)
267 })
268β
269 test('should merge query fn with options', () => {
270 const options = { retry: false }
271 const result = parseMutationArgs(successMutator, options)
272 const expected = { ...options, mutationFn: successMutator }
273β
274 expect(result).toEqual(expected)
275 })
276β
277 test('should merge query key and fn with options', () => {
278 const options = { retry: false }
279 const result = parseMutationArgs(['key'], successMutator, options)
280 const expected = {
281 ...options,
282 mutationKey: ['key'],
283 mutationFn: successMutator,
284 }
285β
286 expect(result).toEqual(expected)
287 })
288 })
289})