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