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

mutationOptions.test.tsx

499 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'
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 key = createKey()
221 const mutationOpts = mutationOptions({
222 mutationKey: key,
223 mutationFn: () => sleep(500).then(() => 'data'),
224 })
225​
226 function Mutation() {
227 const isMutating = queryClient.isMutating(mutationOpts)
228 const { mutate } = useMutation(mutationOpts)
229​
230 isMutatingArray.push(isMutating)
231​
232 return (
233 <div>
234 <button onClick={() => mutate()}>mutate</button>
235 </div>
236 )
237 }
238​
239 const rendered = renderWithClient(queryClient, <Mutation />)
240​
241 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
242 expect(isMutatingArray[0]).toBe(0)
243 await vi.advanceTimersByTimeAsync(0)
244 expect(isMutatingArray[1]).toBe(1)
245 await vi.advanceTimersByTimeAsync(501)
246 expect(isMutatingArray[2]).toBe(0)
247 expect(isMutatingArray[isMutatingArray.length - 1]).toBe(0)
248 })
249​
250 it('should return the number of fetching mutations when used with queryClient.isMutating (without mutationKey in mutationOptions)', async () => {
251 const isMutatingArray: Array<number> = []
252 const queryClient = new QueryClient()
253 const mutationOpts = mutationOptions({
254 mutationFn: () => sleep(500).then(() => 'data'),
255 })
256​
257 function Mutation() {
258 const isMutating = queryClient.isMutating()
259 const { mutate } = useMutation(mutationOpts)
260​
261 isMutatingArray.push(isMutating)
262​
263 return (
264 <div>
265 <button onClick={() => mutate()}>mutate</button>
266 </div>
267 )
268 }
269​
270 const rendered = renderWithClient(queryClient, <Mutation />)
271​
272 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
273 expect(isMutatingArray[0]).toBe(0)
274 await vi.advanceTimersByTimeAsync(0)
275 expect(isMutatingArray[1]).toBe(1)
276 await vi.advanceTimersByTimeAsync(501)
277 expect(isMutatingArray[2]).toBe(0)
278 expect(isMutatingArray[isMutatingArray.length - 1]).toBe(0)
279 })
280​
281 it('should return the number of fetching mutations when used with queryClient.isMutating', async () => {
282 const isMutatingArray: Array<number> = []
283 const queryClient = new QueryClient()
284 const key = createKey()
285 const mutationOpts1 = mutationOptions({
286 mutationKey: key,
287 mutationFn: () => sleep(500).then(() => 'data1'),
288 })
289 const mutationOpts2 = mutationOptions({
290 mutationFn: () => sleep(500).then(() => 'data2'),
291 })
292​
293 function Mutation() {
294 const isMutating = queryClient.isMutating()
295 const { mutate: mutate1 } = useMutation(mutationOpts1)
296 const { mutate: mutate2 } = useMutation(mutationOpts2)
297​
298 isMutatingArray.push(isMutating)
299​
300 return (
301 <div>
302 <button onClick={() => mutate1()}>mutate1</button>
303 <button onClick={() => mutate2()}>mutate2</button>
304 </div>
305 )
306 }
307​
308 const rendered = renderWithClient(queryClient, <Mutation />)
309​
310 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
311 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
312 expect(isMutatingArray[0]).toBe(0)
313 await vi.advanceTimersByTimeAsync(0)
314 expect(isMutatingArray[1]).toBe(2)
315 await vi.advanceTimersByTimeAsync(501)
316 expect(isMutatingArray[2]).toBe(0)
317 expect(isMutatingArray[isMutatingArray.length - 1]).toBe(0)
318 })
319​
320 it('should return the number of fetching mutations when used with queryClient.isMutating (filter mutationOpts1.mutationKey)', async () => {
321 const isMutatingArray: Array<number> = []
322 const queryClient = new QueryClient()
323 const key = createKey()
324 const mutationOpts1 = mutationOptions({
325 mutationKey: key,
326 mutationFn: () => sleep(500).then(() => 'data1'),
327 })
328 const mutationOpts2 = mutationOptions({
329 mutationFn: () => sleep(500).then(() => 'data2'),
330 })
331​
332 function Mutation() {
333 const isMutating = queryClient.isMutating({
334 mutationKey: mutationOpts1.mutationKey,
335 })
336 const { mutate: mutate1 } = useMutation(mutationOpts1)
337 const { mutate: mutate2 } = useMutation(mutationOpts2)
338​
339 isMutatingArray.push(isMutating)
340​
341 return (
342 <div>
343 <button onClick={() => mutate1()}>mutate1</button>
344 <button onClick={() => mutate2()}>mutate2</button>
345 </div>
346 )
347 }
348​
349 const rendered = renderWithClient(queryClient, <Mutation />)
350​
351 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
352 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
353 expect(isMutatingArray[0]).toBe(0)
354 await vi.advanceTimersByTimeAsync(0)
355 expect(isMutatingArray[1]).toBe(1)
356 await vi.advanceTimersByTimeAsync(501)
357 expect(isMutatingArray[2]).toBe(0)
358 expect(isMutatingArray[isMutatingArray.length - 1]).toBe(0)
359 })
360​
361 it('should return the number of fetching mutations when used with useMutationState (with mutationKey in mutationOptions)', async () => {
362 const queryClient = new QueryClient()
363 const mutationOpts = mutationOptions({
364 mutationKey: createKey(),
365 mutationFn: () => sleep(10).then(() => 'data'),
366 })
367​
368 function Mutation() {
369 const { mutate } = useMutation(mutationOpts)
370 const data = useMutationState({
371 filters: { mutationKey: mutationOpts.mutationKey, status: 'success' },
372 })
373​
374 return (
375 <div>
376 <div>
377 results: {JSON.stringify(data.map((mutation) => mutation.data))}
378 </div>
379 <button onClick={() => mutate()}>mutate</button>
380 </div>
381 )
382 }
383​
384 const rendered = renderWithClient(queryClient, <Mutation />)
385​
386 expect(rendered.getByText('results: []')).toBeInTheDocument()
387​
388 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
389 await vi.advanceTimersByTimeAsync(11)
390 expect(rendered.getByText('results: ["data"]')).toBeInTheDocument()
391 })
392​
393 it('should return the number of fetching mutations when used with useMutationState (without mutationKey in mutationOptions)', async () => {
394 const queryClient = new QueryClient()
395 const mutationOpts = mutationOptions({
396 mutationFn: () => sleep(10).then(() => 'data'),
397 })
398​
399 function Mutation() {
400 const { mutate } = useMutation(mutationOpts)
401 const data = useMutationState({
402 filters: { status: 'success' },
403 })
404​
405 return (
406 <div>
407 <div>
408 results: {JSON.stringify(data.map((mutation) => mutation.data))}
409 </div>
410 <button onClick={() => mutate()}>mutate</button>
411 </div>
412 )
413 }
414​
415 const rendered = renderWithClient(queryClient, <Mutation />)
416​
417 expect(rendered.getByText('results: []')).toBeInTheDocument()
418​
419 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
420 await vi.advanceTimersByTimeAsync(11)
421 expect(rendered.getByText('results: ["data"]')).toBeInTheDocument()
422 })
423​
424 it('should return the number of fetching mutations when used with useMutationState', async () => {
425 const queryClient = new QueryClient()
426 const mutationOpts1 = mutationOptions({
427 mutationKey: createKey(),
428 mutationFn: () => sleep(10).then(() => 'data1'),
429 })
430 const mutationOpts2 = mutationOptions({
431 mutationFn: () => sleep(10).then(() => 'data2'),
432 })
433​
434 function Mutation() {
435 const { mutate: mutate1 } = useMutation(mutationOpts1)
436 const { mutate: mutate2 } = useMutation(mutationOpts2)
437 const data = useMutationState({
438 filters: { status: 'success' },
439 })
440​
441 return (
442 <div>
443 <div>
444 results: {JSON.stringify(data.map((mutation) => mutation.data))}
445 </div>
446 <button onClick={() => mutate1()}>mutate1</button>
447 <button onClick={() => mutate2()}>mutate2</button>
448 </div>
449 )
450 }
451​
452 const rendered = renderWithClient(queryClient, <Mutation />)
453​
454 expect(rendered.getByText('results: []')).toBeInTheDocument()
455​
456 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
457 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
458 await vi.advanceTimersByTimeAsync(11)
459 expect(rendered.getByText('results: ["data1","data2"]')).toBeInTheDocument()
460 })
461​
462 it('should return the number of fetching mutations when used with useMutationState (filter mutationOpts1.mutationKey)', async () => {
463 const queryClient = new QueryClient()
464 const mutationOpts1 = mutationOptions({
465 mutationKey: createKey(),
466 mutationFn: () => sleep(10).then(() => 'data1'),
467 })
468 const mutationOpts2 = mutationOptions({
469 mutationFn: () => sleep(10).then(() => 'data2'),
470 })
471​
472 function Mutation() {
473 const { mutate: mutate1 } = useMutation(mutationOpts1)
474 const { mutate: mutate2 } = useMutation(mutationOpts2)
475 const data = useMutationState({
476 filters: { mutationKey: mutationOpts1.mutationKey, status: 'success' },
477 })
478​
479 return (
480 <div>
481 <div>
482 results: {JSON.stringify(data.map((mutation) => mutation.data))}
483 </div>
484 <button onClick={() => mutate1()}>mutate1</button>
485 <button onClick={() => mutate2()}>mutate2</button>
486 </div>
487 )
488 }
489​
490 const rendered = renderWithClient(queryClient, <Mutation />)
491​
492 expect(rendered.getByText('results: []')).toBeInTheDocument()
493​
494 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
495 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
496 await vi.advanceTimersByTimeAsync(11)
497 expect(rendered.getByText('results: ["data1"]')).toBeInTheDocument()
498 })
499})