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

mutationOptions.test.tsx

527 lines17.3 KB

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