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

useIsMutating.test.tsx

247 lines6.4 KB

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})