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

mutationOptions.test.tsx

137 lines4.0 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { QueryClient } from '@tanstack/query-core'
3import { sleep } from '@tanstack/query-test-utils'
4import { fireEvent } from '@testing-library/react'
5import { mutationOptions } from '../mutationOptions'
6import { useIsMutating, useMutation, useMutationState } from '..'
7import { renderWithClient } from './utils'
8import type { MutationState } from '@tanstack/query-core'
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.', () => {
20 const object = {
21 mutationKey: ['key'],
22 mutationFn: () => Promise.resolve(5),
23 } as const
24​
25 expect(mutationOptions(object)).toStrictEqual(object)
26 })
27​
28 it('should return the number of fetching mutations when used with useIsMutating', async () => {
29 const isMutatingArray: Array<number> = []
30 const queryClient = new QueryClient()
31​
32 function IsMutating() {
33 const isMutating = useIsMutating()
34 isMutatingArray.push(isMutating)
35 return null
36 }
37​
38 const mutationOpts = mutationOptions({
39 mutationKey: ['key'],
40 mutationFn: () => sleep(50).then(() => 'data'),
41 })
42​
43 function Mutation() {
44 const { mutate } = useMutation(mutationOpts)
45​
46 return (
47 <div>
48 <button onClick={() => mutate()}>mutate</button>
49 </div>
50 )
51 }
52​
53 function Page() {
54 return (
55 <div>
56 <IsMutating />
57 <Mutation />
58 </div>
59 )
60 }
61​
62 const rendered = renderWithClient(queryClient, <Page />)
63 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
64​
65 expect(isMutatingArray[0]).toEqual(0)
66 await vi.advanceTimersByTimeAsync(0)
67 expect(isMutatingArray[1]).toEqual(1)
68 await vi.advanceTimersByTimeAsync(51)
69 expect(isMutatingArray[2]).toEqual(0)
70 expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
71 })
72​
73 it('should return the number of fetching mutations when used with queryClient.isMutating', async () => {
74 const isMutatingArray: Array<number> = []
75 const queryClient = new QueryClient()
76​
77 const mutationOpts = mutationOptions({
78 mutationKey: ['mutation'],
79 mutationFn: () => sleep(500).then(() => 'data'),
80 })
81​
82 function Mutation() {
83 const isMutating = queryClient.isMutating(mutationOpts)
84 const { mutate } = useMutation(mutationOpts)
85 isMutatingArray.push(isMutating)
86​
87 return (
88 <div>
89 <button onClick={() => mutate()}>mutate</button>
90 </div>
91 )
92 }
93​
94 const rendered = renderWithClient(queryClient, <Mutation />)
95 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
96​
97 expect(isMutatingArray[0]).toEqual(0)
98 await vi.advanceTimersByTimeAsync(0)
99 expect(isMutatingArray[1]).toEqual(1)
100 await vi.advanceTimersByTimeAsync(501)
101 expect(isMutatingArray[2]).toEqual(0)
102 expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
103 })
104​
105 it('should return the number of fetching mutations when used with useMutationState', async () => {
106 const mutationStateArray: Array<
107 MutationState<unknown, Error, unknown, unknown>
108 > = []
109 const queryClient = new QueryClient()
110​
111 const mutationOpts = mutationOptions({
112 mutationKey: ['mutation'],
113 mutationFn: () => Promise.resolve('data'),
114 })
115​
116 function Mutation() {
117 const { mutate } = useMutation(mutationOpts)
118 const data = useMutationState({
119 filters: { ...mutationOpts, status: 'success' },
120 })
121 mutationStateArray.push(...data)
122​
123 return (
124 <div>
125 <button onClick={() => mutate()}>mutate</button>
126 </div>
127 )
128 }
129​
130 const rendered = renderWithClient(queryClient, <Mutation />)
131 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
132​
133 await vi.advanceTimersByTimeAsync(0)
134 expect(mutationStateArray.length).toEqual(1)
135 expect(mutationStateArray[0]?.data).toEqual('data')
136 })
137})