Open History to see the last commit
1import {
2 afterEach,
3 beforeEach,
4 describe,
5 expect,
6 expectTypeOf,
7 it,
8 vi,
9} from 'vitest'
10import { fireEvent, render } from '@testing-library/react'
11import * as React from 'react'
12import { useIsMutating, useMutationState } from '../useMutationState'
13import { useMutation } from '../useMutation'
14import {
15 createQueryClient,
16 doNotExecute,
17 renderWithClient,
18 sleep,
19} from './utils'
20import type { MutationState, MutationStatus } from '@tanstack/query-core'
21β
22describe('useIsMutating', () => {
23 beforeEach(() => {
24 vi.useFakeTimers()
25 })
26β
27 afterEach(() => {
28 vi.useRealTimers()
29 })
30β
31 it('should return the number of fetching mutations', async () => {
32 const isMutatingArray: Array<number> = []
33 const queryClient = createQueryClient()
34β
35 function IsMutating() {
36 const isMutating = useIsMutating()
37 isMutatingArray.push(isMutating)
38 return null
39 }
40β
41 function Mutations() {
42 const { mutate: mutate1 } = useMutation({
43 mutationKey: ['mutation1'],
44 mutationFn: () => sleep(50).then(() => 'data'),
45 })
46 const { mutate: mutate2 } = useMutation({
47 mutationKey: ['mutation2'],
48 mutationFn: () => sleep(10).then(() => 'data'),
49 })
50β
51 return (
52 <div>
53 <button onClick={() => mutate1()}>mutate1</button>
54 <button onClick={() => mutate2()}>mutate2</button>
55 </div>
56 )
57 }
58β
59 function Page() {
60 return (
61 <div>
62 <IsMutating />
63 <Mutations />
64 </div>
65 )
66 }
67β
68 const rendered = renderWithClient(queryClient, <Page />)
69 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
70 await vi.advanceTimersByTimeAsync(10)
71 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
72β
73 // we don't really care if this yields
74 // [ +0, 1, 2, +0 ]
75 // or
76 // [ +0, 1, 2, 1, +0 ]
77 // our batching strategy might yield different results
78β
79 await vi.waitFor(() => expect(isMutatingArray[0]).toEqual(0))
80 await vi.waitFor(() => expect(isMutatingArray[1]).toEqual(1))
81 await vi.waitFor(() => expect(isMutatingArray[2]).toEqual(2))
82 await vi.waitFor(() =>
83 expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0),
84 )
85 })
86β
87 it('should filter correctly by mutationKey', async () => {
88 const isMutatingArray: Array<number> = []
89 const queryClient = createQueryClient()
90β
91 function IsMutating() {
92 const isMutating = useIsMutating({ mutationKey: ['mutation1'] })
93 isMutatingArray.push(isMutating)
94 return null
95 }
96β
97 function Page() {
98 const { mutate: mutate1 } = useMutation({
99 mutationKey: ['mutation1'],
100 mutationFn: () => sleep(100).then(() => 'data'),
101 })
102 const { mutate: mutate2 } = useMutation({
103 mutationKey: ['mutation2'],
104 mutationFn: () => sleep(100).then(() => 'data'),
105 })
106β
107 React.useEffect(() => {
108 mutate1()
109 mutate2()
110 }, [mutate1, mutate2])
111β
112 return <IsMutating />
113 }
114β
115 renderWithClient(queryClient, <Page />)
116 await vi.waitFor(() => expect(isMutatingArray).toEqual([0, 1, 0]))
117 })
118β
119 it('should filter correctly by predicate', async () => {
120 const isMutatingArray: Array<number> = []
121 const queryClient = createQueryClient()
122β
123 function IsMutating() {
124 const isMutating = useIsMutating({
125 predicate: (mutation) =>
126 mutation.options.mutationKey?.[0] === 'mutation1',
127 })
128 isMutatingArray.push(isMutating)
129 return null
130 }
131β
132 function Page() {
133 const { mutate: mutate1 } = useMutation({
134 mutationKey: ['mutation1'],
135 mutationFn: () => sleep(100).then(() => 'data'),
136 })
137 const { mutate: mutate2 } = useMutation({
138 mutationKey: ['mutation2'],
139 mutationFn: () => sleep(100).then(() => 'data'),
140 })
141β
142 React.useEffect(() => {
143 mutate1()
144 mutate2()
145 }, [mutate1, mutate2])
146β
147 return <IsMutating />
148 }
149β
150 renderWithClient(queryClient, <Page />)
151 await vi.waitFor(() => expect(isMutatingArray).toEqual([0, 1, 0]))
152 })
153β
154 it('should use provided custom queryClient', async () => {
155 const queryClient = createQueryClient()
156β
157 function Page() {
158 const isMutating = useIsMutating({}, queryClient)
159 const { mutate } = useMutation(
160 {
161 mutationKey: ['mutation1'],
162 mutationFn: () => sleep(10).then(() => 'data'),
163 },
164 queryClient,
165 )
166β
167 React.useEffect(() => {
168 mutate()
169 }, [mutate])
170β
171 return (
172 <div>
173 <div>mutating: {isMutating}</div>
174 </div>
175 )
176 }
177β
178 const rendered = render(<Page></Page>)
179β
180 await vi.waitFor(() => rendered.getByText('mutating: 1'))
181 })
182})
183β
184describe('useMutationState', () => {
185 describe('types', () => {
186 it('should default to QueryState', () => {
187 doNotExecute(() => {
188 const result = useMutationState({
189 filters: { status: 'pending' },
190 })
191β
192 expectTypeOf(result).toEqualTypeOf<Array<MutationState>>()
193 })
194 })
195 it('should infer with select', () => {
196 doNotExecute(() => {
197 const result = useMutationState({
198 filters: { status: 'pending' },
199 select: (mutation) => mutation.state.status,
200 })
201β
202 expectTypeOf(result).toEqualTypeOf<Array<MutationStatus>>()
203 })
204 })
205 })
206 it('should return variables after calling mutate', async () => {
207 const queryClient = createQueryClient()
208 const variables: Array<Array<unknown>> = []
209 const mutationKey = ['mutation']
210β
211 function Variables() {
212 variables.push(
213 useMutationState({
214 filters: { mutationKey, status: 'pending' },
215 select: (mutation) => mutation.state.variables,
216 }),
217 )
218β
219 return null
220 }
221β
222 function Mutate() {
223 const { mutate, data } = useMutation({
224 mutationKey,
225 mutationFn: (input: number) => sleep(150).then(() => 'data' + input),
226 })
227β
228 return (
229 <div>
230 data: {data ?? 'null'}
231 <button onClick={() => mutate(1)}>mutate</button>
232 </div>
233 )
234 }
235β
236 function Page() {
237 return (
238 <div>
239 <Variables />
240 <Mutate />
241 </div>
242 )
243 }
244β
245 const rendered = renderWithClient(queryClient, <Page />)
246β
247 await vi.waitFor(() => rendered.getByText('data: null'))
248β
249 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
250β
251 await vi.waitFor(() => rendered.getByText('data: data1'))
252β
253 expect(variables).toEqual([[], [1], []])
254 })
255})