Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { QueryClient } from '@tanstack/query-core'
3import { queryKey, sleep } from '@tanstack/query-test-utils'
4import { LitElement, html } from 'lit'
5import { QueryClientProvider } from '../QueryClientProvider.js'
6import { createMutationController } from '../createMutationController.js'
7import { useIsMutating } from '../useIsMutating.js'
8import { generateElementName } from './utils.js'
9import type { IsMutatingAccessor } from '../useIsMutating.js'
10β
11const providerTagName = generateElementName()
12customElements.define(providerTagName, QueryClientProvider)
13β
14describe('useIsMutating', () => {
15 let queryClient: QueryClient
16 let container: HTMLElement
17β
18 beforeEach(() => {
19 vi.useFakeTimers()
20 queryClient = new QueryClient()
21 container = document.createElement('div')
22 document.body.append(container)
23 })
24β
25 afterEach(() => {
26 container.remove()
27 queryClient.clear()
28 vi.useRealTimers()
29 })
30β
31 it('should keep the pre-connect placeholder zero until a provider binds', async () => {
32 const mutationKey = queryKey()
33β
34 class Consumer extends LitElement {
35 readonly mutationKey = mutationKey
36β
37 readonly mutation = createMutationController(this, {
38 mutationKey: this.mutationKey,
39 mutationFn: () => sleep(10).then(() => 'mutation-ok'),
40 })
41β
42 readonly isMutating = useIsMutating(this, {
43 mutationKey: this.mutationKey,
44 })
45 }
46 customElements.define(generateElementName(), Consumer)
47 const consumer = new Consumer()
48β
49 expect(consumer.isMutating()).toBe(0)
50β
51 const provider = document.createElement(
52 providerTagName,
53 ) as QueryClientProvider
54 provider.client = queryClient
55 provider.append(consumer)
56β
57 container.append(provider)
58 await provider.updateComplete
59 await consumer.updateComplete
60β
61 consumer.mutation.mutate()
62 expect(consumer.isMutating()).toBe(1)
63 await vi.advanceTimersByTimeAsync(10)
64 expect(consumer.isMutating()).toBe(0)
65β
66 consumer.mutation.destroy()
67 consumer.isMutating.destroy()
68 provider.remove()
69 })
70β
71 it('should prefer an explicit client over the provider context', async () => {
72 const mutationKey = queryKey()
73 const providerClient = new QueryClient()
74β
75 const provider = document.createElement(
76 providerTagName,
77 ) as QueryClientProvider
78 provider.client = providerClient
79β
80 class Consumer extends LitElement {
81 readonly mutationKey = mutationKey
82β
83 readonly mutation = createMutationController(
84 this,
85 {
86 mutationKey: this.mutationKey,
87 mutationFn: () => sleep(10).then(() => 'mutation-ok'),
88 },
89 queryClient,
90 )
91β
92 readonly isMutating = useIsMutating(
93 this,
94 { mutationKey: this.mutationKey },
95 queryClient,
96 )
97 }
98 customElements.define(generateElementName(), Consumer)
99 const consumer = new Consumer()
100 provider.append(consumer)
101β
102 container.append(provider)
103 await provider.updateComplete
104 await consumer.updateComplete
105β
106 consumer.mutation.mutate()
107 expect(consumer.isMutating()).toBe(1)
108 await vi.advanceTimersByTimeAsync(10)
109 expect(consumer.isMutating()).toBe(0)
110 expect(
111 queryClient
112 .getMutationCache()
113 .findAll({ mutationKey: consumer.mutationKey }).length,
114 ).toBeGreaterThan(0)
115 expect(
116 providerClient
117 .getMutationCache()
118 .findAll({ mutationKey: consumer.mutationKey }).length,
119 ).toBe(0)
120β
121 consumer.mutation.destroy()
122 consumer.isMutating.destroy()
123 provider.remove()
124 })
125β
126 it('should track the mutating count', async () => {
127 class Producer extends LitElement {
128 readonly mutation = createMutationController(
129 this,
130 {
131 mutationFn: (value: number) => sleep(10).then(() => value + 10),
132 },
133 queryClient,
134 )
135 }
136 customElements.define(generateElementName(), Producer)
137β
138 class Host extends LitElement {
139 readonly isMutating = useIsMutating(this, {}, queryClient)
140β
141 override render() {
142 return html`<p>mutating: ${this.isMutating()}</p>`
143 }
144 }
145 customElements.define(generateElementName(), Host)
146 const producer = new Producer()
147 const host = new Host()
148 container.append(producer, host)
149 const { mutation } = producer
150 const { isMutating } = host
151β
152 await vi.advanceTimersByTimeAsync(0)
153 mutation.mutate(1)
154 expect(isMutating()).toBe(1)
155 await vi.advanceTimersByTimeAsync(0)
156 expect(host.shadowRoot).toHaveTextContent('mutating: 1')
157 await vi.advanceTimersByTimeAsync(10)
158 expect(isMutating()).toBe(0)
159 expect(host.shadowRoot).toHaveTextContent('mutating: 0')
160 })
161β
162 it('should not process mutation cache updates while disconnected', async () => {
163 class Producer extends LitElement {
164 readonly mutation = createMutationController(
165 this,
166 {
167 mutationFn: (value: number) => sleep(10).then(() => value + 10),
168 },
169 queryClient,
170 )
171 }
172 customElements.define(generateElementName(), Producer)
173β
174 class Host extends LitElement {
175 updatesRequested = 0
176β
177 readonly isMutating = useIsMutating(this, {}, queryClient)
178β
179 override requestUpdate(
180 ...args: Parameters<LitElement['requestUpdate']>
181 ): void {
182 this.updatesRequested += 1
183 super.requestUpdate(...args)
184 }
185 }
186 customElements.define(generateElementName(), Host)
187 const producer = new Producer()
188 const host = new Host()
189 container.append(producer, host)
190 const { mutation } = producer
191 await host.updateComplete
192β
193 host.remove()
194 await host.updateComplete
195 const updatesAfterDisconnect = host.updatesRequested
196β
197 mutation.mutate(1)
198 await vi.advanceTimersByTimeAsync(10)
199 expect(host.updatesRequested).toBe(updatesAfterDisconnect)
200 })
201β
202 it('should count all mutating mutations when filters are omitted', async () => {
203 class Producer extends LitElement {
204 readonly mutation1 = createMutationController(this, {
205 mutationKey: queryKey(),
206 mutationFn: () => sleep(10).then(() => 'data1'),
207 })
208β
209 readonly mutation2 = createMutationController(this, {
210 mutationKey: queryKey(),
211 mutationFn: () => sleep(10).then(() => 'data2'),
212 })
213 }
214 customElements.define(generateElementName(), Producer)
215β
216 class Host extends LitElement {
217 readonly isMutating = useIsMutating(this)
218β
219 override render() {
220 return html`<p>mutating: ${this.isMutating()}</p>`
221 }
222 }
223 customElements.define(generateElementName(), Host)
224 const provider = document.createElement(
225 providerTagName,
226 ) as QueryClientProvider
227 provider.client = queryClient
228 const producer = new Producer()
229 const host = new Host()
230 provider.append(producer, host)
231β
232 container.append(provider)
233 await provider.updateComplete
234β
235 expect(host.shadowRoot).toHaveTextContent('mutating: 0')
236β
237 producer.mutation1.mutate()
238 producer.mutation2.mutate()
239 await vi.advanceTimersByTimeAsync(0)
240 expect(host.shadowRoot).toHaveTextContent('mutating: 2')
241 await vi.advanceTimersByTimeAsync(10)
242 expect(host.shadowRoot).toHaveTextContent('mutating: 0')
243 })
244β
245 it('should be able to filter', async () => {
246 const mutationKey1 = queryKey()
247 const mutationKey2 = queryKey()
248β
249 class Host extends LitElement {
250 readonly mutationA = createMutationController(
251 this,
252 {
253 mutationKey: mutationKey1,
254 mutationFn: () => sleep(10).then(() => 1),
255 },
256 queryClient,
257 )
258β
259 readonly mutationB = createMutationController(
260 this,
261 {
262 mutationKey: mutationKey2,
263 mutationFn: () => sleep(20).then(() => 2),
264 },
265 queryClient,
266 )
267β
268 readonly isMutatingAll = useIsMutating(this, {}, queryClient)
269 readonly isMutatingFiltered = useIsMutating(
270 this,
271 { mutationKey: mutationKey1 },
272 queryClient,
273 )
274β
275 override render() {
276 const all = this.isMutatingAll()
277 const filtered = this.isMutatingFiltered()
278 return html`<p>all: ${all}, filtered: ${filtered}</p>`
279 }
280 }
281 customElements.define(generateElementName(), Host)
282 const host = new Host()
283 container.append(host)
284 const { mutationA, mutationB, isMutatingAll, isMutatingFiltered } = host
285β
286 mutationA.mutate()
287 mutationB.mutate()
288 await vi.advanceTimersByTimeAsync(0)
289 expect(isMutatingAll()).toBe(2)
290 expect(isMutatingFiltered()).toBe(1)
291 expect(host.shadowRoot).toHaveTextContent('all: 2, filtered: 1')
292 await vi.advanceTimersByTimeAsync(10)
293 expect(isMutatingAll()).toBe(1)
294 expect(isMutatingFiltered()).toBe(0)
295 expect(host.shadowRoot).toHaveTextContent('all: 1, filtered: 0')
296 await vi.advanceTimersByTimeAsync(10)
297 expect(isMutatingAll()).toBe(0)
298 expect(isMutatingFiltered()).toBe(0)
299 expect(host.shadowRoot).toHaveTextContent('all: 0, filtered: 0')
300 })
301β
302 it('should apply updated filters on host updates', async () => {
303 const mutationKey1 = queryKey()
304 const mutationKey2 = queryKey()
305 const unmatchedMutationKey = queryKey()
306 let activeFilter: { mutationKey?: ReadonlyArray<string> } = {
307 mutationKey: mutationKey1,
308 }
309β
310 class Host extends LitElement {
311 readonly mutationA = createMutationController(
312 this,
313 {
314 mutationKey: mutationKey1,
315 mutationFn: () => sleep(10).then(() => 1),
316 },
317 queryClient,
318 )
319β
320 readonly mutationB = createMutationController(
321 this,
322 {
323 mutationKey: mutationKey2,
324 mutationFn: () => sleep(20).then(() => 2),
325 },
326 queryClient,
327 )
328β
329 readonly isMutatingFiltered = useIsMutating(
330 this,
331 () => activeFilter,
332 queryClient,
333 )
334β
335 override render() {
336 return html`<p>filtered: ${this.isMutatingFiltered()}</p>`
337 }
338 }
339 customElements.define(generateElementName(), Host)
340 const host = new Host()
341 container.append(host)
342 const { mutationA, mutationB, isMutatingFiltered } = host
343β
344 mutationA.mutate()
345 mutationB.mutate()
346 await vi.advanceTimersByTimeAsync(0)
347 expect(isMutatingFiltered()).toBe(1)
348 expect(host.shadowRoot).toHaveTextContent('filtered: 1')
349β
350 activeFilter = { mutationKey: unmatchedMutationKey }
351 host.requestUpdate()
352 await host.updateComplete
353β
354 expect(isMutatingFiltered()).toBe(0)
355 expect(host.shadowRoot).toHaveTextContent('filtered: 0')
356β
357 activeFilter = { mutationKey: mutationKey2 }
358 host.requestUpdate()
359 await host.updateComplete
360β
361 expect(isMutatingFiltered()).toBe(1)
362 expect(host.shadowRoot).toHaveTextContent('filtered: 1')
363 await vi.advanceTimersByTimeAsync(10)
364 expect(isMutatingFiltered()).toBe(1)
365 expect(host.shadowRoot).toHaveTextContent('filtered: 1')
366 await vi.advanceTimersByTimeAsync(10)
367 expect(isMutatingFiltered()).toBe(0)
368 expect(host.shadowRoot).toHaveTextContent('filtered: 0')
369 })
370β
371 it('should fail after the handshake and recover under a provider', async () => {
372 const mutationKey = queryKey()
373β
374 class Consumer extends LitElement {
375 readonly mutationKey = mutationKey
376β
377 readonly isMutating = useIsMutating(this, {
378 mutationKey: this.mutationKey,
379 })
380 }
381 customElements.define(generateElementName(), Consumer)
382 const consumer = new Consumer()
383β
384 expect(consumer.isMutating()).toBe(0)
385β
386 container.append(consumer)
387β
388 await vi.advanceTimersByTimeAsync(0)
389 expect(() => consumer.isMutating()).toThrow(/No QueryClient available/)
390β
391 const provider = document.createElement(
392 providerTagName,
393 ) as QueryClientProvider
394 provider.client = queryClient
395 provider.append(consumer)
396β
397 container.append(provider)
398 await provider.updateComplete
399 await consumer.updateComplete
400β
401 expect(consumer.isMutating()).toBe(0)
402β
403 consumer.isMutating.destroy()
404 provider.remove()
405 })
406β
407 it('should not throw on an already-connected host with an explicit client', async () => {
408 const mutationKey = queryKey()
409β
410 class Producer extends LitElement {
411 readonly mutation = createMutationController(
412 this,
413 {
414 mutationKey,
415 mutationFn: () => sleep(10).then(() => 'mutation-ok'),
416 },
417 queryClient,
418 )
419 }
420 customElements.define(generateElementName(), Producer)
421 const producer = new Producer()
422 container.append(producer)
423 const producerMutation = producer.mutation
424β
425 producerMutation.mutate()
426 expect(queryClient.isMutating()).toBe(1)
427β
428 class Host extends LitElement {
429 isMutating?: IsMutatingAccessor
430 }
431 customElements.define(generateElementName(), Host)
432 const host = new Host()
433 container.append(host)
434 await host.updateComplete
435β
436 // Lit calls hostConnected immediately when a controller is added to an
437 // already-connected host
438 host.isMutating = useIsMutating(host, {}, queryClient)
439 const { isMutating } = host
440β
441 expect(isMutating()).toBe(1)
442 await vi.advanceTimersByTimeAsync(10)
443 expect(isMutating()).toBe(0)
444β
445 isMutating.destroy()
446 producerMutation.destroy()
447 })
448})