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

useMutationState.test.tsx

255 lines6.4 KB

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})