Open History to see the last commit
1import { fireEvent, waitFor } from '@testing-library/react'
2import * as React from 'react'
3import { ErrorBoundary } from 'react-error-boundary'
4import { QueryClient } from '@tanstack/query-core'
5import { useIsMutating } from '../useIsMutating'
6import { useMutation } from '../useMutation'
7import * as MutationCacheModule from '../../../query-core/src/mutationCache'
8import {
9 createQueryClient,
10 renderWithClient,
11 setActTimeout,
12 sleep,
13} from './utils'
14β
15describe('useIsMutating', () => {
16 it('should return the number of fetching mutations', async () => {
17 const isMutatings: number[] = []
18 const queryClient = createQueryClient()
19β
20 function IsMutating() {
21 const isMutating = useIsMutating()
22 isMutatings.push(isMutating)
23 return null
24 }
25β
26 function Mutations() {
27 const { mutate: mutate1 } = useMutation(['mutation1'], async () => {
28 await sleep(150)
29 return 'data'
30 })
31 const { mutate: mutate2 } = useMutation(['mutation2'], async () => {
32 await sleep(50)
33 return 'data'
34 })
35β
36 React.useEffect(() => {
37 mutate1()
38 setActTimeout(() => {
39 mutate2()
40 }, 50)
41 }, [mutate1, mutate2])
42β
43 return null
44 }
45β
46 function Page() {
47 return (
48 <div>
49 <IsMutating />
50 <Mutations />
51 </div>
52 )
53 }
54β
55 renderWithClient(queryClient, <Page />)
56 await waitFor(() => expect(isMutatings).toEqual([0, 1, 2, 1, 0]))
57 })
58β
59 it('should filter correctly by mutationKey', async () => {
60 const isMutatings: number[] = []
61 const queryClient = createQueryClient()
62β
63 function IsMutating() {
64 const isMutating = useIsMutating(['mutation1'])
65 isMutatings.push(isMutating)
66 return null
67 }
68β
69 function Page() {
70 const { mutate: mutate1 } = useMutation(['mutation1'], async () => {
71 await sleep(100)
72 return 'data'
73 })
74 const { mutate: mutate2 } = useMutation(['mutation2'], async () => {
75 await sleep(100)
76 return 'data'
77 })
78β
79 React.useEffect(() => {
80 mutate1()
81 mutate2()
82 }, [mutate1, mutate2])
83β
84 return <IsMutating />
85 }
86β
87 renderWithClient(queryClient, <Page />)
88 await waitFor(() => expect(isMutatings).toEqual([0, 1, 1, 0]))
89 })
90β
91 it('should filter correctly by predicate', async () => {
92 const isMutatings: number[] = []
93 const queryClient = createQueryClient()
94β
95 function IsMutating() {
96 const isMutating = useIsMutating({
97 predicate: (mutation) =>
98 mutation.options.mutationKey?.[0] === 'mutation1',
99 })
100 isMutatings.push(isMutating)
101 return null
102 }
103β
104 function Page() {
105 const { mutate: mutate1 } = useMutation(['mutation1'], async () => {
106 await sleep(100)
107 return 'data'
108 })
109 const { mutate: mutate2 } = useMutation(['mutation2'], async () => {
110 await sleep(100)
111 return 'data'
112 })
113β
114 React.useEffect(() => {
115 mutate1()
116 mutate2()
117 }, [mutate1, mutate2])
118β
119 return <IsMutating />
120 }
121β
122 renderWithClient(queryClient, <Page />)
123 await waitFor(() => expect(isMutatings).toEqual([0, 1, 1, 0]))
124 })
125β
126 it('should not change state if unmounted', async () => {
127 // We have to mock the MutationCache to not unsubscribe
128 // the listener when the component is unmounted
129 class MutationCacheMock extends MutationCacheModule.MutationCache {
130 subscribe(listener: any) {
131 super.subscribe(listener)
132 return () => void 0
133 }
134 }
135β
136 const MutationCacheSpy = jest
137 .spyOn(MutationCacheModule, 'MutationCache')
138 .mockImplementation((fn) => {
139 return new MutationCacheMock(fn)
140 })
141β
142 const queryClient = createQueryClient()
143β
144 function IsMutating() {
145 useIsMutating()
146 return null
147 }
148β
149 function Page() {
150 const [mounted, setMounted] = React.useState(true)
151 const { mutate: mutate1 } = useMutation(['mutation1'], async () => {
152 await sleep(10)
153 return 'data'
154 })
155β
156 React.useEffect(() => {
157 mutate1()
158 }, [mutate1])
159β
160 return (
161 <div>
162 <button onClick={() => setMounted(false)}>unmount</button>
163 {mounted && <IsMutating />}
164 </div>
165 )
166 }
167β
168 const { getByText } = renderWithClient(queryClient, <Page />)
169 fireEvent.click(getByText('unmount'))
170β
171 // Should not display the console error
172 // "Warning: Can't perform a React state update on an unmounted component"
173β
174 await sleep(20)
175 MutationCacheSpy.mockRestore()
176 })
177β
178 describe('with custom context', () => {
179 it('should return the number of fetching mutations', async () => {
180 const context = React.createContext<QueryClient | undefined>(undefined)
181β
182 const isMutatings: number[] = []
183 const queryClient = new QueryClient()
184β
185 function IsMutating() {
186 const isMutating = useIsMutating(undefined, { context })
187 isMutatings.push(isMutating)
188 return null
189 }
190β
191 function Page() {
192 const { mutate: mutate1 } = useMutation(
193 ['mutation1'],
194 async () => {
195 await sleep(150)
196 return 'data'
197 },
198 { context },
199 )
200 const { mutate: mutate2 } = useMutation(
201 ['mutation2'],
202 async () => {
203 await sleep(50)
204 return 'data'
205 },
206 { context },
207 )
208β
209 React.useEffect(() => {
210 mutate1()
211 setActTimeout(() => {
212 mutate2()
213 }, 50)
214 }, [mutate1, mutate2])
215β
216 return <IsMutating />
217 }
218β
219 renderWithClient(queryClient, <Page />, { context })
220 await waitFor(() => expect(isMutatings).toEqual([0, 1, 1, 2, 2, 1, 0]))
221 })
222β
223 it('should throw if the context is not passed to useIsMutating', async () => {
224 const context = React.createContext<QueryClient | undefined>(undefined)
225β
226 const isMutatings: number[] = []
227 const queryClient = new QueryClient()
228β
229 function IsMutating() {
230 const isMutating = useIsMutating(undefined)
231 isMutatings.push(isMutating)
232 return null
233 }
234β
235 function Page() {
236 const { mutate } = useMutation(['mutation'], async () => 'data', {
237 useErrorBoundary: true,
238 context,
239 })
240β
241 React.useEffect(() => {
242 mutate()
243 }, [mutate])
244β
245 return <IsMutating />
246 }
247β
248 const rendered = renderWithClient(
249 queryClient,
250 <ErrorBoundary fallbackRender={() => <div>error boundary</div>}>
251 <Page />
252 </ErrorBoundary>,
253 { context },
254 )
255β
256 await waitFor(() => rendered.getByText('error boundary'))
257 })
258 })
259})