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