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