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

useIsMutating.test.tsx

257 lines6.3 KB

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