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