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

useIsMutating.test.tsx

245 lines6.1 KB

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