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