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