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