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

useIsMutating.test.tsx

250 lines6.4 KB

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