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