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