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

useIsMutating.test.tsx

271 lines6.7 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 { 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})