Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

mutations.test.tsx

400 lines9.1 KB

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})