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

mutationOptions.test.tsx

500 lines15.9 KB

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