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 filter correctly by mutationKey', async () => {
81 const isMutatingArray: Array<number> = []
82 const queryClient = new QueryClient()
83 const key1 = queryKey()
84 const key2 = queryKey()
85β
86 function IsMutating() {
87 const isMutating = useIsMutating({ mutationKey: key1 })
88 isMutatingArray.push(isMutating)
89 return null
90 }
91β
92 function Page() {
93 const { mutate: mutate1 } = useMutation({
94 mutationKey: key1,
95 mutationFn: () => sleep(100).then(() => 'data'),
96 })
97 const { mutate: mutate2 } = useMutation({
98 mutationKey: key2,
99 mutationFn: () => sleep(100).then(() => 'data'),
100 })
101β
102 React.useEffect(() => {
103 mutate1()
104 mutate2()
105 }, [mutate1, mutate2])
106β
107 return <IsMutating />
108 }
109β
110 renderWithClient(queryClient, <Page />)
111β
112 await vi.advanceTimersByTimeAsync(101)
113 expect(isMutatingArray).toEqual([0, 1, 0])
114 })
115β
116 it('should filter correctly by predicate', async () => {
117 const isMutatingArray: Array<number> = []
118 const queryClient = new QueryClient()
119 const key1 = queryKey()
120 const key2 = queryKey()
121β
122 function IsMutating() {
123 const isMutating = useIsMutating({
124 predicate: (mutation) => mutation.options.mutationKey?.[0] === key1[0],
125 })
126 isMutatingArray.push(isMutating)
127 return null
128 }
129β
130 function Page() {
131 const { mutate: mutate1 } = useMutation({
132 mutationKey: key1,
133 mutationFn: () => sleep(100).then(() => 'data'),
134 })
135 const { mutate: mutate2 } = useMutation({
136 mutationKey: key2,
137 mutationFn: () => sleep(100).then(() => 'data'),
138 })
139β
140 React.useEffect(() => {
141 mutate1()
142 mutate2()
143 }, [mutate1, mutate2])
144β
145 return <IsMutating />
146 }
147β
148 renderWithClient(queryClient, <Page />)
149β
150 await vi.advanceTimersByTimeAsync(101)
151 expect(isMutatingArray).toEqual([0, 1, 0])
152 })
153β
154 it('should use provided custom queryClient', async () => {
155 const queryClient = new QueryClient()
156 const key = queryKey()
157β
158 function Page() {
159 const isMutating = useIsMutating({}, queryClient)
160 const { mutate } = useMutation(
161 {
162 mutationKey: key,
163 mutationFn: () => sleep(10).then(() => 'data'),
164 },
165 queryClient,
166 )
167β
168 React.useEffect(() => {
169 mutate()
170 }, [mutate])
171β
172 return (
173 <div>
174 <div>mutating: {isMutating}</div>
175 </div>
176 )
177 }
178β
179 const rendered = render(<Page />)
180β
181 await vi.advanceTimersByTimeAsync(0)
182 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
183 })
184})
185β
186describe('useMutationState', () => {
187 beforeEach(() => {
188 vi.useFakeTimers()
189 })
190β
191 afterEach(() => {
192 vi.useRealTimers()
193 })
194β
195 it('should return variables after calling mutate', async () => {
196 const queryClient = new QueryClient()
197 const variables: Array<Array<unknown>> = []
198 const mutationKey = ['mutation']
199β
200 function Variables() {
201 variables.push(
202 useMutationState({
203 filters: { mutationKey, status: 'pending' },
204 select: (mutation) => mutation.state.variables,
205 }),
206 )
207β
208 return null
209 }
210β
211 function Mutate() {
212 const { mutate, data } = useMutation({
213 mutationKey,
214 mutationFn: (input: number) => sleep(150).then(() => 'data' + input),
215 })
216β
217 return (
218 <div>
219 data: {data ?? 'null'}
220 <button onClick={() => mutate(1)}>mutate</button>
221 </div>
222 )
223 }
224β
225 function Page() {
226 return (
227 <div>
228 <Variables />
229 <Mutate />
230 </div>
231 )
232 }
233β
234 const rendered = renderWithClient(queryClient, <Page />)
235β
236 expect(rendered.getByText('data: null')).toBeInTheDocument()
237β
238 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
239 await vi.advanceTimersByTimeAsync(151)
240 expect(rendered.getByText('data: data1')).toBeInTheDocument()
241β
242 expect(variables).toEqual([[], [1], []])
243 })
244})