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