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

mutationOptions.test.tsx

496 lines15.8 KB

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