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

useMutationState.test.tsx

263 lines6.6 KB

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