Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

extra.test.ts

90 lines2.8 KB

1/**
2 * Additional conformance driven by the port review: query-key change β†’ refetch,
3 * unmount β†’ observer unsubscribe, the explicit-client (no-provider) path, and the
4 * provider mounting/unmounting the client.
5 */
6import { beforeEach, describe, expect, it } from 'vitest'
7import { QueryClient } from '@tanstack/octane-query'
8import { mount, nextPaint } from '../_helpers'
9import { KeyedApp } from '../_fixtures/extra.tsrx'
10import { App } from '../_fixtures/app.tsrx'
11import { Todo } from '../_fixtures/smoke.tsrx'
12​
13let client: QueryClient
14beforeEach(() => {
15 client = new QueryClient({ defaultOptions: { queries: { retry: false } } })
16 client.mount()
17})
18​
19async function flush() {
20 for (let i = 0; i < 5; i++) {
21 await new Promise((r) => setTimeout(r, 0))
22 await nextPaint()
23 }
25​
26describe('query-key change', () => {
27 it('swaps the query and refetches when the key changes', async () => {
28 const r = mount(KeyedApp, { client, k: 1 })
29 await flush()
30 expect(r.find('#status').textContent).toBe('data:v1')
31 r.update(KeyedApp, { client, k: 2 })
32 await flush()
33 expect(r.find('#status').textContent).toBe('data:v2')
34 r.unmount()
35 })
36})
37​
38describe('unmount unsubscribes the observer', () => {
39 it('drops the query observer on unmount (no leak)', async () => {
40 let resolveFn: (v: string) => void = () => {}
41 const queryFn = () => new Promise<string>((res) => (resolveFn = res))
42 const r = mount(App, { client, queryFn })
43 await flush()
44 resolveFn('x')
45 await flush()
46 const query = client.getQueryCache().find({ queryKey: ['k'] })!
47 expect(query.observers.length).toBeGreaterThan(0)
48 r.unmount()
49 await flush()
50 expect(query.observers.length).toBe(0)
51 })
52})
53​
54describe('explicit client (no provider)', () => {
55 it('useQuery(options, client) resolves the passed client', async () => {
56 let resolveFn: (v: string) => void = () => {}
57 const queryFn = () => new Promise<string>((res) => (resolveFn = res))
58 const r = mount(Todo, { client, queryFn })
59 expect(r.find('#status').textContent).toBe('pending')
60 await flush()
61 resolveFn('direct')
62 await flush()
63 expect(r.find('#status').textContent).toBe('data:direct')
64 r.unmount()
65 })
66})
67​
68describe('QueryClientProvider mounts/unmounts the client', () => {
69 it('calls client.mount() on mount and client.unmount() on unmount', async () => {
70 const c = new QueryClient({ defaultOptions: { queries: { retry: false } } })
71 let mounts = 0
72 let unmounts = 0
73 const realMount = c.mount.bind(c)
74 const realUnmount = c.unmount.bind(c)
75 c.mount = () => {
76 mounts++
77 return realMount()
78 }
79 c.unmount = () => {
80 unmounts++
81 return realUnmount()
82 }
83 const r = mount(App, { client: c, queryFn: () => Promise.resolve('x') })
84 await flush()
85 expect(mounts).toBe(1)
86 r.unmount()
87 await flush()
88 expect(unmounts).toBe(1)
89 })
90})