Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useIsMutating.test.tsx

269 lines6.5 KB

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