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