Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { fireEvent, render } from '@testing-library/react'
3import * as React from 'react'
4import { queryKey, sleep } from '@tanstack/query-test-utils'
5import { QueryClient, useIsMutating, useMutation, useMutationState } from '..'
6import { renderWithClient } from './utils'
7β
8describe('useIsMutating', () => {
9 beforeEach(() => {
10 vi.useFakeTimers()
11 })
12β
13 afterEach(() => {
14 vi.useRealTimers()
15 })
16β
17 it('should return the number of fetching mutations', async () => {
18 const isMutatingArray: Array<number> = []
19 const queryClient = new QueryClient()
20 const key1 = queryKey()
21 const key2 = queryKey()
22β
23 function IsMutating() {
24 const isMutating = useIsMutating()
25β
26 isMutatingArray.push(isMutating)
27β
28 return null
29 }
30β
31 function Mutations() {
32 const { mutate: mutate1 } = useMutation({
33 mutationKey: key1,
34 mutationFn: () => sleep(50).then(() => 'data'),
35 })
36 const { mutate: mutate2 } = useMutation({
37 mutationKey: key2,
38 mutationFn: () => sleep(10).then(() => 'data'),
39 })
40β
41 return (
42 <div>
43 <button onClick={() => mutate1()}>mutate1</button>
44 <button onClick={() => mutate2()}>mutate2</button>
45 </div>
46 )
47 }
48β
49 function Page() {
50 return (
51 <div>
52 <IsMutating />
53 <Mutations />
54 </div>
55 )
56 }
57β
58 const rendered = renderWithClient(queryClient, <Page />)
59β
60 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
61 await vi.advanceTimersByTimeAsync(10)
62 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
63β
64 // we don't really care if this yields
65 // [ +0, 1, 2, +0 ]
66 // or
67 // [ +0, 1, 2, 1, +0 ]
68 // our batching strategy might yield different results
69β
70 await vi.advanceTimersByTimeAsync(41)
71 expect(isMutatingArray[0]).toEqual(0)
72 expect(isMutatingArray[1]).toEqual(1)
73 expect(isMutatingArray[2]).toEqual(2)
74 expect(isMutatingArray[3]).toEqual(1)
75 expect(isMutatingArray[4]).toEqual(0)
76β
77 expect(isMutatingArray).toEqual([0, 1, 2, 1, 0])
78 })
79β
80 it('should update when the query client is cleared', async () => {
81 const queryClient = new QueryClient()
82β
83 function Page() {
84 const { mutate } = useMutation({
85 mutationFn: () => sleep(1000).then(() => 'data'),
86 })
87 const isMutating = useIsMutating()
88β
89 return (
90 <div>
91 <div>mutating: {isMutating}</div>
92 <button onClick={() => mutate()}>mutate</button>
93 <button onClick={() => queryClient.clear()}>clear</button>
94 </div>
95 )
96 }
97β
98 const rendered = renderWithClient(queryClient, <Page />)
99 fireEvent.click(rendered.getByRole('button', { name: 'mutate' }))
100 await vi.advanceTimersByTimeAsync(1)
101 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
102β
103 fireEvent.click(rendered.getByRole('button', { name: 'clear' }))
104 await vi.advanceTimersByTimeAsync(1)
105 expect(queryClient.isMutating()).toBe(0)
106 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
107β
108 await vi.advanceTimersByTimeAsync(1000)
109 })
110β
111 it('should filter correctly by mutationKey', async () => {
112 const isMutatingArray: Array<number> = []
113 const queryClient = new QueryClient()
114 const key1 = queryKey()
115 const key2 = queryKey()
116β
117 function IsMutating() {
118 const isMutating = useIsMutating({ mutationKey: key1 })
119 isMutatingArray.push(isMutating)
120 return null
121 }
122β
123 function Page() {
124 const { mutate: mutate1 } = useMutation({
125 mutationKey: key1,
126 mutationFn: () => sleep(100).then(() => 'data'),
127 })
128 const { mutate: mutate2 } = useMutation({
129 mutationKey: key2,
130 mutationFn: () => sleep(100).then(() => 'data'),
131 })
132β
133 React.useEffect(() => {
134 mutate1()
135 mutate2()
136 }, [mutate1, mutate2])
137β
138 return <IsMutating />
139 }
140β
141 renderWithClient(queryClient, <Page />)
142β
143 await vi.advanceTimersByTimeAsync(101)
144 expect(isMutatingArray).toEqual([0, 1, 0])
145 })
146β
147 it('should filter correctly by predicate', async () => {
148 const isMutatingArray: Array<number> = []
149 const queryClient = new QueryClient()
150 const key1 = queryKey()
151 const key2 = queryKey()
152β
153 function IsMutating() {
154 const isMutating = useIsMutating({
155 predicate: (mutation) => mutation.options.mutationKey?.[0] === key1[0],
156 })
157 isMutatingArray.push(isMutating)
158 return null
159 }
160β
161 function Page() {
162 const { mutate: mutate1 } = useMutation({
163 mutationKey: key1,
164 mutationFn: () => sleep(100).then(() => 'data'),
165 })
166 const { mutate: mutate2 } = useMutation({
167 mutationKey: key2,
168 mutationFn: () => sleep(100).then(() => 'data'),
169 })
170β
171 React.useEffect(() => {
172 mutate1()
173 mutate2()
174 }, [mutate1, mutate2])
175β
176 return <IsMutating />
177 }
178β
179 renderWithClient(queryClient, <Page />)
180β
181 await vi.advanceTimersByTimeAsync(101)
182 expect(isMutatingArray).toEqual([0, 1, 0])
183 })
184β
185 it('should use provided custom queryClient', async () => {
186 const queryClient = new QueryClient()
187 const key = queryKey()
188β
189 function Page() {
190 const isMutating = useIsMutating({}, queryClient)
191 const { mutate } = useMutation(
192 {
193 mutationKey: key,
194 mutationFn: () => sleep(10).then(() => 'data'),
195 },
196 queryClient,
197 )
198β
199 React.useEffect(() => {
200 mutate()
201 }, [mutate])
202β
203 return (
204 <div>
205 <div>mutating: {isMutating}</div>
206 </div>
207 )
208 }
209β
210 const rendered = render(<Page />)
211β
212 await vi.advanceTimersByTimeAsync(0)
213 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
214 })
215})
216β
217describe('useMutationState', () => {
218 beforeEach(() => {
219 vi.useFakeTimers()
220 })
221β
222 afterEach(() => {
223 vi.useRealTimers()
224 })
225β
226 it('should remove successful mutations when the query client is cleared', async () => {
227 const queryClient = new QueryClient()
228β
229 function Page() {
230 const { mutate } = useMutation({
231 mutationFn: () => Promise.resolve('saved'),
232 })
233 const data = useMutationState({
234 filters: { status: 'success' },
235 select: (mutation) => mutation.state.data,
236 })
237β
238 return (
239 <div>
240 <div>data: {JSON.stringify(data)}</div>
241 <button onClick={() => mutate()}>mutate</button>
242 <button onClick={() => queryClient.clear()}>clear</button>
243 </div>
244 )
245 }
246β
247 const rendered = renderWithClient(queryClient, <Page />)
248 fireEvent.click(rendered.getByRole('button', { name: 'mutate' }))
249 await vi.advanceTimersByTimeAsync(1)
250 expect(rendered.getByText('data: ["saved"]')).toBeInTheDocument()
251β
252 fireEvent.click(rendered.getByRole('button', { name: 'clear' }))
253 await vi.advanceTimersByTimeAsync(1)
254 expect(queryClient.getMutationCache().getAll()).toEqual([])
255 expect(rendered.getByText('data: []')).toBeInTheDocument()
256 })
257β
258 it('should return variables after calling mutate', async () => {
259 const queryClient = new QueryClient()
260 const variables: Array<Array<unknown>> = []
261 const mutationKey = ['mutation']
262β
263 function Variables() {
264 variables.push(
265 useMutationState({
266 filters: { mutationKey, status: 'pending' },
267 select: (mutation) => mutation.state.variables,
268 }),
269 )
270β
271 return null
272 }
273β
274 function Mutate() {
275 const { mutate, data } = useMutation({
276 mutationKey,
277 mutationFn: (input: number) => sleep(150).then(() => 'data' + input),
278 })
279β
280 return (
281 <div>
282 data: {data ?? 'null'}
283 <button onClick={() => mutate(1)}>mutate</button>
284 </div>
285 )
286 }
287β
288 function Page() {
289 return (
290 <div>
291 <Variables />
292 <Mutate />
293 </div>
294 )
295 }
296β
297 const rendered = renderWithClient(queryClient, <Page />)
298β
299 expect(rendered.getByText('data: null')).toBeInTheDocument()
300β
301 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
302 await vi.advanceTimersByTimeAsync(151)
303 expect(rendered.getByText('data: data1')).toBeInTheDocument()
304β
305 expect(variables).toEqual([[], [1], []])
306 })
307})