Open History to see the last commit
1import { waitFor } from '@testing-library/react'
2import { MutationObserver } from '../mutationObserver'
3import { createQueryClient, executeMutation, queryKey, sleep } from './utils'
4import type { QueryClient } from '..'
5import type { MutationState } from '../mutation'
6β
7describe('mutations', () => {
8 let queryClient: QueryClient
9β
10 beforeEach(() => {
11 queryClient = createQueryClient()
12 queryClient.mount()
13 })
14β
15 afterEach(() => {
16 queryClient.clear()
17 })
18β
19 test('mutate should trigger a mutation', async () => {
20 const result = await executeMutation(queryClient, {
21 mutationFn: async (text: string) => text,
22 variables: 'todo',
23 })
24β
25 expect(result).toBe(result)
26 })
27β
28 test('mutate should accept null values', async () => {
29 let variables
30β
31 const mutation = new MutationObserver(queryClient, {
32 mutationFn: async (vars: unknown) => {
33 variables = vars
34 return vars
35 },
36 })
37β
38 mutation.mutate(null)
39β
40 await sleep(10)
41β
42 expect(variables).toBe(null)
43 })
44β
45 test('setMutationDefaults should be able to set defaults', async () => {
46 const key = queryKey()
47β
48 queryClient.setMutationDefaults(key, {
49 mutationFn: async (text: string) => text,
50 })
51β
52 const result = await executeMutation(queryClient, {
53 mutationKey: key,
54 variables: 'todo',
55 })
56β
57 expect(result).toBe(result)
58 })
59β
60 test('mutation should set correct success states', async () => {
61 const mutation = new MutationObserver(queryClient, {
62 mutationFn: async (text: string) => {
63 await sleep(10)
64 return text
65 },
66 onMutate: (text) => text,
67 variables: 'todo',
68 })
69β
70 expect(mutation.getCurrentResult()).toEqual({
71 context: undefined,
72 data: undefined,
73 error: null,
74 failureCount: 0,
75 failureReason: null,
76 isError: false,
77 isIdle: true,
78 isLoading: false,
79 isPending: false,
80 isPaused: false,
81 isSuccess: false,
82 mutate: expect.any(Function),
83 reset: expect.any(Function),
84 status: 'idle',
85 variables: undefined,
86 })
87β
88 const states: MutationState<string, unknown, string, string>[] = []
89β
90 mutation.subscribe((state) => {
91 states.push(state)
92 })
93β
94 mutation.mutate()
95β
96 await sleep(0)
97β
98 expect(states[0]).toEqual({
99 context: undefined,
100 data: undefined,
101 error: null,
102 failureCount: 0,
103 failureReason: null,
104 isError: false,
105 isIdle: false,
106 isLoading: true,
107 isPending: true,
108 isPaused: false,
109 isSuccess: false,
110 mutate: expect.any(Function),
111 reset: expect.any(Function),
112 status: 'loading',
113 variables: 'todo',
114 })
115β
116 await sleep(5)
117β
118 expect(states[1]).toEqual({
119 context: 'todo',
120 data: undefined,
121 error: null,
122 failureCount: 0,
123 failureReason: null,
124 isError: false,
125 isIdle: false,
126 isLoading: true,
127 isPending: true,
128 isPaused: false,
129 isSuccess: false,
130 mutate: expect.any(Function),
131 reset: expect.any(Function),
132 status: 'loading',
133 variables: 'todo',
134 })
135β
136 await sleep(20)
137β
138 expect(states[2]).toEqual({
139 context: 'todo',
140 data: 'todo',
141 error: null,
142 failureCount: 0,
143 failureReason: null,
144 isError: false,
145 isIdle: false,
146 isLoading: false,
147 isPending: false,
148 isPaused: false,
149 isSuccess: true,
150 mutate: expect.any(Function),
151 reset: expect.any(Function),
152 status: 'success',
153 variables: 'todo',
154 })
155 })
156β
157 test('mutation should set correct error states', async () => {
158 const mutation = new MutationObserver(queryClient, {
159 mutationFn: async () => {
160 await sleep(20)
161 return Promise.reject('err')
162 },
163 onMutate: (text) => text,
164 variables: 'todo',
165 retry: 1,
166 retryDelay: 1,
167 })
168β
169 const states: MutationState<string, unknown, string, string>[] = []
170β
171 mutation.subscribe((state) => {
172 states.push(state)
173 })
174β
175 mutation.mutate().catch(() => undefined)
176β
177 await sleep(0)
178β
179 expect(states[0]).toEqual({
180 context: undefined,
181 data: undefined,
182 error: null,
183 failureCount: 0,
184 failureReason: null,
185 isError: false,
186 isIdle: false,
187 isLoading: true,
188 isPending: true,
189 isPaused: false,
190 isSuccess: false,
191 mutate: expect.any(Function),
192 reset: expect.any(Function),
193 status: 'loading',
194 variables: 'todo',
195 })
196β
197 await sleep(10)
198β
199 expect(states[1]).toEqual({
200 context: 'todo',
201 data: undefined,
202 error: null,
203 failureCount: 0,
204 failureReason: null,
205 isError: false,
206 isIdle: false,
207 isLoading: true,
208 isPending: true,
209 isPaused: false,
210 isSuccess: false,
211 mutate: expect.any(Function),
212 reset: expect.any(Function),
213 status: 'loading',
214 variables: 'todo',
215 })
216β
217 await sleep(20)
218β
219 expect(states[2]).toEqual({
220 context: 'todo',
221 data: undefined,
222 error: null,
223 failureCount: 1,
224 failureReason: 'err',
225 isError: false,
226 isIdle: false,
227 isLoading: true,
228 isPending: true,
229 isPaused: false,
230 isSuccess: false,
231 mutate: expect.any(Function),
232 reset: expect.any(Function),
233 status: 'loading',
234 variables: 'todo',
235 })
236β
237 await sleep(30)
238β
239 expect(states[3]).toEqual({
240 context: 'todo',
241 data: undefined,
242 error: 'err',
243 failureCount: 2,
244 failureReason: 'err',
245 isError: true,
246 isIdle: false,
247 isLoading: false,
248 isPending: false,
249 isPaused: false,
250 isSuccess: false,
251 mutate: expect.any(Function),
252 reset: expect.any(Function),
253 status: 'error',
254 variables: 'todo',
255 })
256 })
257β
258 test('should be able to restore a mutation', async () => {
259 const key = queryKey()
260β
261 const onMutate = jest.fn()
262 const onSuccess = jest.fn()
263 const onSettled = jest.fn()
264β
265 queryClient.setMutationDefaults(key, {
266 mutationFn: async (text: string) => text,
267 onMutate,
268 onSuccess,
269 onSettled,
270 })
271β
272 const mutation = queryClient
273 .getMutationCache()
274 .build<string, unknown, string, string>(
275 queryClient,
276 {
277 mutationKey: key,
278 },
279 {
280 context: 'todo',
281 data: undefined,
282 error: null,
283 failureCount: 1,
284 failureReason: 'err',
285 isPaused: true,
286 status: 'loading',
287 variables: 'todo',
288 },
289 )
290β
291 expect(mutation.state).toEqual({
292 context: 'todo',
293 data: undefined,
294 error: null,
295 failureCount: 1,
296 failureReason: 'err',
297 isPaused: true,
298 status: 'loading',
299 variables: 'todo',
300 })
301β
302 await queryClient.resumePausedMutations()
303β
304 expect(mutation.state).toEqual({
305 context: 'todo',
306 data: 'todo',
307 error: null,
308 failureCount: 0,
309 failureReason: null,
310 isPaused: false,
311 status: 'success',
312 variables: 'todo',
313 })
314β
315 expect(onMutate).not.toHaveBeenCalled()
316 expect(onSuccess).toHaveBeenCalled()
317 expect(onSettled).toHaveBeenCalled()
318 })
319β
320 test('addObserver should not add an existing observer', async () => {
321 const mutationCache = queryClient.getMutationCache()
322 const observer = new MutationObserver(queryClient, {})
323 const currentMutation = mutationCache.build(queryClient, {})
324β
325 const fn = jest.fn()
326β
327 const unsubscribe = mutationCache.subscribe((event) => {
328 fn(event.type)
329 })
330β
331 currentMutation.addObserver(observer)
332 currentMutation.addObserver(observer)
333β
334 expect(fn).toHaveBeenCalledTimes(1)
335 expect(fn).toHaveBeenCalledWith('observerAdded')
336β
337 unsubscribe()
338 })
339β
340 test('mutate should throw an error if no mutationFn found', async () => {
341 const mutation = new MutationObserver(queryClient, {
342 mutationFn: undefined,
343 retry: false,
344 })
345β
346 let error: any
347 try {
348 await mutation.mutate()
349 } catch (err) {
350 error = err
351 }
352 expect(error).toEqual('No mutationFn found')
353 })
354β
355 test('mutate update the mutation state even without an active subscription', async () => {
356 const onSuccess = jest.fn()
357 const onSettled = jest.fn()
358β
359 const mutation = new MutationObserver(queryClient, {
360 mutationFn: async () => {
361 return 'update'
362 },
363 })
364β
365 await mutation.mutate(undefined, { onSuccess, onSettled })
366 expect(mutation.getCurrentResult().data).toEqual('update')
367 expect(onSuccess).not.toHaveBeenCalled()
368 expect(onSettled).not.toHaveBeenCalled()
369 })
370β
371 test('mutation callbacks should see updated options', async () => {
372 const onSuccess = jest.fn()
373β
374 const mutation = new MutationObserver(queryClient, {
375 mutationFn: async () => {
376 sleep(100)
377 return 'update'
378 },
379 onSuccess: () => {
380 onSuccess(1)
381 },
382 })
383β
384 void mutation.mutate()
385β
386 mutation.setOptions({
387 mutationFn: async () => {
388 sleep(100)
389 return 'update'
390 },
391 onSuccess: () => {
392 onSuccess(2)
393 },
394 })
395β
396 await waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
397β
398 expect(onSuccess).toHaveBeenCalledWith(2)
399 })
400})