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