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