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