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

mutationOptions.test.tsx

564 lines18.0 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { fireEvent, render } from '@solidjs/testing-library'
3import { createEffect, createRenderEffect } from 'solid-js'
4import { sleep } from '@tanstack/query-test-utils'
5import {
6 QueryClient,
7 QueryClientProvider,
8 useIsMutating,
9 useMutation,
10 useMutationState,
11} from '..'
12import { mutationOptions } from '../mutationOptions'
13import type { MutationState } from '@tanstack/query-core'
14​
15describe('mutationOptions', () => {
16 beforeEach(() => {
17 vi.useFakeTimers()
18 })
19​
20 afterEach(() => {
21 vi.useRealTimers()
22 })
23​
24 it('should return the object received as a parameter without any modification (with mutationKey in mutationOptions)', () => {
25 const object = {
26 mutationKey: ['key'],
27 mutationFn: () => sleep(10).then(() => 5),
28 } as const
29​
30 expect(mutationOptions(object)).toStrictEqual(object)
31 })
32​
33 it('should return the object received as a parameter without any modification (without mutationKey in mutationOptions)', () => {
34 const object = {
35 mutationFn: () => sleep(10).then(() => 5),
36 } as const
37​
38 expect(mutationOptions(object)).toStrictEqual(object)
39 })
40​
41 it('should return the number of fetching mutations when used with useIsMutating (with mutationKey in mutationOptions)', async () => {
42 const isMutatingArray: Array<number> = []
43 const queryClient = new QueryClient()
44 const mutationOpts = mutationOptions({
45 mutationKey: ['key'],
46 mutationFn: () => sleep(50).then(() => 'data'),
47 })
48​
49 function Mutation() {
50 const isMutating = useIsMutating()
51 const { mutate } = useMutation(() => mutationOpts)
52​
53 createRenderEffect(() => {
54 isMutatingArray.push(isMutating())
55 })
56​
57 return (
58 <div>
59 <button onClick={() => mutate()}>mutate</button>
60 </div>
61 )
62 }
63​
64 const rendered = render(() => (
65 <QueryClientProvider client={queryClient}>
66 <Mutation />
67 </QueryClientProvider>
68 ))
69​
70 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
71 expect(isMutatingArray[0]).toEqual(0)
72 await vi.advanceTimersByTimeAsync(0)
73 expect(isMutatingArray[1]).toEqual(1)
74 await vi.advanceTimersByTimeAsync(50)
75 expect(isMutatingArray[2]).toEqual(0)
76 expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
77 })
78​
79 it('should return the number of fetching mutations when used with useIsMutating (without mutationKey in mutationOptions)', async () => {
80 const isMutatingArray: Array<number> = []
81 const queryClient = new QueryClient()
82 const mutationOpts = mutationOptions({
83 mutationFn: () => sleep(50).then(() => 'data'),
84 })
85​
86 function Mutation() {
87 const isMutating = useIsMutating()
88 const { mutate } = useMutation(() => mutationOpts)
89​
90 createRenderEffect(() => {
91 isMutatingArray.push(isMutating())
92 })
93​
94 return (
95 <div>
96 <button onClick={() => mutate()}>mutate</button>
97 </div>
98 )
99 }
100​
101 const rendered = render(() => (
102 <QueryClientProvider client={queryClient}>
103 <Mutation />
104 </QueryClientProvider>
105 ))
106​
107 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
108 expect(isMutatingArray[0]).toEqual(0)
109 await vi.advanceTimersByTimeAsync(0)
110 expect(isMutatingArray[1]).toEqual(1)
111 await vi.advanceTimersByTimeAsync(50)
112 expect(isMutatingArray[2]).toEqual(0)
113 expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
114 })
115​
116 it('should return the number of fetching mutations when used with useIsMutating', async () => {
117 const isMutatingArray: Array<number> = []
118 const queryClient = new QueryClient()
119 const mutationOpts1 = mutationOptions({
120 mutationKey: ['key'],
121 mutationFn: () => sleep(50).then(() => 'data1'),
122 })
123 const mutationOpts2 = mutationOptions({
124 mutationFn: () => sleep(50).then(() => 'data2'),
125 })
126​
127 function Mutation() {
128 const isMutating = useIsMutating()
129 const { mutate: mutate1 } = useMutation(() => mutationOpts1)
130 const { mutate: mutate2 } = useMutation(() => mutationOpts2)
131​
132 createRenderEffect(() => {
133 isMutatingArray.push(isMutating())
134 })
135​
136 return (
137 <div>
138 <button onClick={() => mutate1()}>mutate1</button>
139 <button onClick={() => mutate2()}>mutate2</button>
140 </div>
141 )
142 }
143​
144 const rendered = render(() => (
145 <QueryClientProvider client={queryClient}>
146 <Mutation />
147 </QueryClientProvider>
148 ))
149​
150 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
151 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
152 expect(isMutatingArray[0]).toEqual(0)
153 await vi.advanceTimersByTimeAsync(0)
154 expect(isMutatingArray[2]).toEqual(2)
155 await vi.advanceTimersByTimeAsync(50)
156 expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
157 })
158​
159 it('should return the number of fetching mutations when used with useIsMutating (filter mutationOpts1.mutationKey)', async () => {
160 const isMutatingArray: Array<number> = []
161 const queryClient = new QueryClient()
162 const mutationOpts1 = mutationOptions({
163 mutationKey: ['key'],
164 mutationFn: () => sleep(50).then(() => 'data1'),
165 })
166 const mutationOpts2 = mutationOptions({
167 mutationFn: () => sleep(50).then(() => 'data2'),
168 })
169​
170 function Mutation() {
171 const isMutating = useIsMutating(() => ({
172 mutationKey: mutationOpts1.mutationKey,
173 }))
174 const { mutate: mutate1 } = useMutation(() => mutationOpts1)
175 const { mutate: mutate2 } = useMutation(() => mutationOpts2)
176​
177 createRenderEffect(() => {
178 isMutatingArray.push(isMutating())
179 })
180​
181 return (
182 <div>
183 <button onClick={() => mutate1()}>mutate1</button>
184 <button onClick={() => mutate2()}>mutate2</button>
185 </div>
186 )
187 }
188​
189 const rendered = render(() => (
190 <QueryClientProvider client={queryClient}>
191 <Mutation />
192 </QueryClientProvider>
193 ))
194​
195 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
196 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
197 expect(isMutatingArray[0]).toEqual(0)
198 await vi.advanceTimersByTimeAsync(0)
199 expect(isMutatingArray[1]).toEqual(1)
200 await vi.advanceTimersByTimeAsync(50)
201 expect(isMutatingArray[2]).toEqual(0)
202 expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
203 })
204​
205 it('should return the number of fetching mutations when used with queryClient.isMutating (with mutationKey in mutationOptions)', async () => {
206 const isMutatingArray: Array<number> = []
207 const queryClient = new QueryClient()
208 const mutationOpts = mutationOptions({
209 mutationKey: ['mutation'],
210 mutationFn: () => sleep(500).then(() => 'data'),
211 })
212​
213 function Mutation() {
214 const { mutate } = useMutation(() => mutationOpts)
215​
216 return (
217 <div>
218 <button onClick={() => mutate()}>mutate</button>
219 </div>
220 )
221 }
222​
223 const rendered = render(() => (
224 <QueryClientProvider client={queryClient}>
225 <Mutation />
226 </QueryClientProvider>
227 ))
228​
229 const mutationCache = queryClient.getMutationCache()
230 const unsubscribe = mutationCache.subscribe(() => {
231 isMutatingArray.push(queryClient.isMutating(mutationOpts))
232 })
233​
234 isMutatingArray.push(queryClient.isMutating(mutationOpts))
235​
236 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
237 expect(isMutatingArray[0]).toEqual(0)
238 await vi.advanceTimersByTimeAsync(0)
239 // Use Math.max because subscribe callback count is implementation-dependent
240 expect(Math.max(...isMutatingArray)).toEqual(1)
241 await vi.advanceTimersByTimeAsync(500)
242 expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
243​
244 unsubscribe()
245 })
246​
247 it('should return the number of fetching mutations when used with queryClient.isMutating (without mutationKey in mutationOptions)', async () => {
248 const isMutatingArray: Array<number> = []
249 const queryClient = new QueryClient()
250 const mutationOpts = mutationOptions({
251 mutationFn: () => sleep(500).then(() => 'data'),
252 })
253​
254 function Mutation() {
255 const { mutate } = useMutation(() => mutationOpts)
256​
257 return (
258 <div>
259 <button onClick={() => mutate()}>mutate</button>
260 </div>
261 )
262 }
263​
264 const rendered = render(() => (
265 <QueryClientProvider client={queryClient}>
266 <Mutation />
267 </QueryClientProvider>
268 ))
269​
270 const mutationCache = queryClient.getMutationCache()
271 const unsubscribe = mutationCache.subscribe(() => {
272 isMutatingArray.push(queryClient.isMutating())
273 })
274​
275 isMutatingArray.push(queryClient.isMutating())
276​
277 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
278 expect(isMutatingArray[0]).toEqual(0)
279 await vi.advanceTimersByTimeAsync(0)
280 // Use Math.max because subscribe callback count is implementation-dependent
281 expect(Math.max(...isMutatingArray)).toEqual(1)
282 await vi.advanceTimersByTimeAsync(500)
283 expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
284​
285 unsubscribe()
286 })
287​
288 it('should return the number of fetching mutations when used with queryClient.isMutating', async () => {
289 const isMutatingArray: Array<number> = []
290 const queryClient = new QueryClient()
291 const mutationOpts1 = mutationOptions({
292 mutationKey: ['mutation'],
293 mutationFn: () => sleep(500).then(() => 'data1'),
294 })
295 const mutationOpts2 = mutationOptions({
296 mutationFn: () => sleep(500).then(() => 'data2'),
297 })
298​
299 function Mutation() {
300 const { mutate: mutate1 } = useMutation(() => mutationOpts1)
301 const { mutate: mutate2 } = useMutation(() => mutationOpts2)
302​
303 return (
304 <div>
305 <button onClick={() => mutate1()}>mutate1</button>
306 <button onClick={() => mutate2()}>mutate2</button>
307 </div>
308 )
309 }
310​
311 const rendered = render(() => (
312 <QueryClientProvider client={queryClient}>
313 <Mutation />
314 </QueryClientProvider>
315 ))
316​
317 const mutationCache = queryClient.getMutationCache()
318 const unsubscribe = mutationCache.subscribe(() => {
319 isMutatingArray.push(queryClient.isMutating())
320 })
321​
322 isMutatingArray.push(queryClient.isMutating())
323​
324 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
325 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
326 expect(isMutatingArray[0]).toEqual(0)
327 await vi.advanceTimersByTimeAsync(0)
328 // Use Math.max because subscribe callback count is implementation-dependent
329 expect(Math.max(...isMutatingArray)).toEqual(2)
330 await vi.advanceTimersByTimeAsync(500)
331 expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
332​
333 unsubscribe()
334 })
335​
336 it('should return the number of fetching mutations when used with queryClient.isMutating (filter mutationOpt1.mutationKey)', async () => {
337 const isMutatingArray: Array<number> = []
338 const queryClient = new QueryClient()
339 const mutationOpts1 = mutationOptions({
340 mutationKey: ['mutation'],
341 mutationFn: () => sleep(500).then(() => 'data1'),
342 })
343 const mutationOpts2 = mutationOptions({
344 mutationFn: () => sleep(500).then(() => 'data2'),
345 })
346​
347 function Mutation() {
348 const { mutate: mutate1 } = useMutation(() => mutationOpts1)
349 const { mutate: mutate2 } = useMutation(() => mutationOpts2)
350​
351 return (
352 <div>
353 <button onClick={() => mutate1()}>mutate1</button>
354 <button onClick={() => mutate2()}>mutate2</button>
355 </div>
356 )
357 }
358​
359 const rendered = render(() => (
360 <QueryClientProvider client={queryClient}>
361 <Mutation />
362 </QueryClientProvider>
363 ))
364​
365 const mutationCache = queryClient.getMutationCache()
366 const unsubscribe = mutationCache.subscribe(() => {
367 isMutatingArray.push(
368 queryClient.isMutating({
369 mutationKey: mutationOpts1.mutationKey,
370 }),
371 )
372 })
373​
374 isMutatingArray.push(
375 queryClient.isMutating({
376 mutationKey: mutationOpts1.mutationKey,
377 }),
378 )
379​
380 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
381 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
382 expect(isMutatingArray[0]).toEqual(0)
383 await vi.advanceTimersByTimeAsync(0)
384 // Use Math.max because subscribe callback count is implementation-dependent
385 expect(Math.max(...isMutatingArray)).toEqual(1)
386 await vi.advanceTimersByTimeAsync(500)
387 expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
388​
389 unsubscribe()
390 })
391​
392 it('should return the number of fetching mutations when used with useMutationState (with mutationKey in mutationOptions)', async () => {
393 const mutationStateArray: Array<Array<MutationState>> = []
394 const queryClient = new QueryClient()
395 const mutationOpts = mutationOptions({
396 mutationKey: ['mutation'],
397 mutationFn: () => sleep(10).then(() => 'data'),
398 })
399​
400 function Mutation() {
401 const { mutate } = useMutation(() => mutationOpts)
402 const states = useMutationState(() => ({
403 filters: { mutationKey: mutationOpts.mutationKey, status: 'success' },
404 }))
405​
406 createEffect(() => {
407 mutationStateArray.push(states())
408 })
409​
410 return (
411 <div>
412 <button onClick={() => mutate()}>mutate</button>
413 </div>
414 )
415 }
416​
417 const rendered = render(() => (
418 <QueryClientProvider client={queryClient}>
419 <Mutation />
420 </QueryClientProvider>
421 ))
422​
423 expect(mutationStateArray[0]).toEqual([])
424​
425 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
426 await vi.advanceTimersByTimeAsync(10)
427 const lastSnapshot = mutationStateArray[mutationStateArray.length - 1]!
428 expect(lastSnapshot.length).toEqual(1)
429 expect(lastSnapshot[0]?.data).toEqual('data')
430 })
431​
432 it('should return the number of fetching mutations when used with useMutationState (without mutationKey in mutationOptions)', async () => {
433 const mutationStateArray: Array<Array<MutationState>> = []
434 const queryClient = new QueryClient()
435 const mutationOpts = mutationOptions({
436 mutationFn: () => sleep(10).then(() => 'data'),
437 })
438​
439 function Mutation() {
440 const { mutate } = useMutation(() => mutationOpts)
441 const states = useMutationState(() => ({
442 filters: { status: 'success' },
443 }))
444​
445 createEffect(() => {
446 mutationStateArray.push(states())
447 })
448​
449 return (
450 <div>
451 <button onClick={() => mutate()}>mutate</button>
452 </div>
453 )
454 }
455​
456 const rendered = render(() => (
457 <QueryClientProvider client={queryClient}>
458 <Mutation />
459 </QueryClientProvider>
460 ))
461​
462 expect(mutationStateArray[0]).toEqual([])
463​
464 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
465 await vi.advanceTimersByTimeAsync(10)
466 const lastSnapshot = mutationStateArray[mutationStateArray.length - 1]!
467 expect(lastSnapshot.length).toEqual(1)
468 expect(lastSnapshot[0]?.data).toEqual('data')
469 })
470​
471 it('should return the number of fetching mutations when used with useMutationState', async () => {
472 const mutationStateArray: Array<Array<MutationState>> = []
473 const queryClient = new QueryClient()
474 const mutationOpts1 = mutationOptions({
475 mutationKey: ['mutation'],
476 mutationFn: () => sleep(10).then(() => 'data1'),
477 })
478 const mutationOpts2 = mutationOptions({
479 mutationFn: () => sleep(10).then(() => 'data2'),
480 })
481​
482 function Mutation() {
483 const { mutate: mutate1 } = useMutation(() => mutationOpts1)
484 const { mutate: mutate2 } = useMutation(() => mutationOpts2)
485 const states = useMutationState(() => ({
486 filters: { status: 'success' },
487 }))
488​
489 createEffect(() => {
490 mutationStateArray.push(states())
491 })
492​
493 return (
494 <div>
495 <button onClick={() => mutate1()}>mutate1</button>
496 <button onClick={() => mutate2()}>mutate2</button>
497 </div>
498 )
499 }
500​
501 const rendered = render(() => (
502 <QueryClientProvider client={queryClient}>
503 <Mutation />
504 </QueryClientProvider>
505 ))
506​
507 expect(mutationStateArray[0]).toEqual([])
508​
509 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
510 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
511 await vi.advanceTimersByTimeAsync(10)
512 const lastSnapshot = mutationStateArray[mutationStateArray.length - 1]!
513 expect(lastSnapshot.length).toEqual(2)
514 expect(lastSnapshot[0]?.data).toEqual('data1')
515 expect(lastSnapshot[1]?.data).toEqual('data2')
516 })
517​
518 it('should return the number of fetching mutations when used with useMutationState (filter mutationOpt1.mutationKey)', async () => {
519 const mutationStateArray: Array<Array<MutationState>> = []
520 const queryClient = new QueryClient()
521 const mutationOpts1 = mutationOptions({
522 mutationKey: ['mutation'],
523 mutationFn: () => sleep(10).then(() => 'data1'),
524 })
525 const mutationOpts2 = mutationOptions({
526 mutationFn: () => sleep(10).then(() => 'data2'),
527 })
528​
529 function Mutation() {
530 const { mutate: mutate1 } = useMutation(() => mutationOpts1)
531 const { mutate: mutate2 } = useMutation(() => mutationOpts2)
532 const states = useMutationState(() => ({
533 filters: { mutationKey: mutationOpts1.mutationKey, status: 'success' },
534 }))
535​
536 createEffect(() => {
537 mutationStateArray.push(states())
538 })
539​
540 return (
541 <div>
542 <button onClick={() => mutate1()}>mutate1</button>
543 <button onClick={() => mutate2()}>mutate2</button>
544 </div>
545 )
546 }
547​
548 const rendered = render(() => (
549 <QueryClientProvider client={queryClient}>
550 <Mutation />
551 </QueryClientProvider>
552 ))
553​
554 expect(mutationStateArray[0]).toEqual([])
555​
556 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
557 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
558 await vi.advanceTimersByTimeAsync(10)
559 const lastSnapshot = mutationStateArray[mutationStateArray.length - 1]!
560 expect(lastSnapshot.length).toEqual(1)
561 expect(lastSnapshot[0]?.data).toEqual('data1')
562 expect(lastSnapshot[1]).toBeFalsy()
563 })
564})