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