Open History to see the last commit
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 { 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 isMutatingArray: Array<number> = []
11 const queryClient = createQueryClient()
12β
13 function IsMutating() {
14 const isMutating = useIsMutating()
15 createRenderEffect(() => {
16 isMutatingArray.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(isMutatingArray).toEqual([0, 1, 2, 1, 0]))
62 })
63β
64 it('should filter correctly by mutationKey', async () => {
65 const isMutatingArray: Array<number> = []
66 const queryClient = createQueryClient()
67β
68 function IsMutating() {
69 const isMutating = useIsMutating(() => ({ mutationKey: ['mutation1'] }))
70 createRenderEffect(() => {
71 isMutatingArray.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(isMutatingArray).toEqual([0, 1, 0]))
107 })
108β
109 it('should filter correctly by predicate', async () => {
110 const isMutatingArray: 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 isMutatingArray.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(isMutatingArray).toEqual([0, 1, 0]))
156 })
157β
158 it('should use provided custom queryClient', async () => {
159 const queryClient = createQueryClient()
160β
161 function Page() {
162 const isMutating = useIsMutating(undefined, () => queryClient)
163 const { mutate } = createMutation(
164 () => ({
165 mutationKey: ['mutation1'],
166 mutationFn: async () => {
167 await sleep(10)
168 return 'data'
169 },
170 }),
171 () => queryClient,
172 )
173 createEffect(() => {
174 mutate()
175 })
176 return (
177 <div>
178 <div>mutating: {isMutating()}</div>
179 </div>
180 )
181 }
182β
183 const rendered = render(() => <Page></Page>)
184β
185 await waitFor(() => rendered.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 QueryCore.MutationCache {
192 subscribe(listener: any) {
193 super.subscribe(listener)
194 return () => void 0
195 }
196 }
197β
198 const MutationCacheSpy = vi
199 .spyOn(QueryCore, '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 const rendered = render(() => (
236 <QueryClientProvider client={queryClient}>
237 <Page />
238 </QueryClientProvider>
239 ))
240 fireEvent.click(rendered.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})