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