Open History to see the last commit
1import { QueryClient } from '@tanstack/query-core'
2import { createKey, sleep } from '@tanstack/query-test-utils'
3import { LitElement } from 'lit'
4import {
5 afterEach,
6 beforeAll,
7 beforeEach,
8 describe,
9 expect,
10 it,
11 vi,
12} from 'vitest'
13import { QueryClientProvider } from '../QueryClientProvider.js'
14import { createQueryController } from '../createQueryController.js'
15import {
16 getDefaultQueryClient,
17 resolveQueryClient,
18 useQueryClient,
19} from '../index.js'
20import { generateElementName } from './utils.js'
21β
22const tagName = generateElementName()
23β
24describe('QueryClientProvider/context', () => {
25 let queryClient: QueryClient
26 let container: HTMLElement
27β
28 beforeAll(() => {
29 customElements.define(tagName, QueryClientProvider)
30 })
31β
32 beforeEach(() => {
33 vi.useFakeTimers()
34 queryClient = new QueryClient()
35 container = document.createElement('div')
36 document.body.append(container)
37 })
38β
39 afterEach(() => {
40 container.remove()
41 queryClient.clear()
42 vi.useRealTimers()
43 })
44β
45 it('should register and unregister the default query client for public helpers', async () => {
46 const provider = document.createElement(tagName) as QueryClientProvider
47 provider.client = queryClient
48 container.append(provider)
49 await provider.updateComplete
50β
51 expect(useQueryClient()).toBe(queryClient)
52 expect(resolveQueryClient()).toBe(queryClient)
53β
54 provider.remove()
55 expect(() => useQueryClient()).toThrow(/No QueryClient available/)
56 })
57β
58 it('should prefer an explicit client in resolveQueryClient', () => {
59 expect(resolveQueryClient(queryClient)).toBe(queryClient)
60 })
61β
62 it('should keep the default client registered until the last provider using it disconnects', async () => {
63 const providerA = document.createElement(tagName) as QueryClientProvider
64 const providerB = document.createElement(tagName) as QueryClientProvider
65 providerA.client = queryClient
66 providerB.client = queryClient
67 container.append(providerA)
68 container.append(providerB)
69 await providerA.updateComplete
70 await providerB.updateComplete
71β
72 expect(useQueryClient()).toBe(queryClient)
73β
74 providerB.remove()
75 await vi.advanceTimersByTimeAsync(0)
76 expect(useQueryClient()).toBe(queryClient)
77β
78 providerA.remove()
79 expect(() => useQueryClient()).toThrow(/No QueryClient available/)
80 })
81β
82 it('should throw when multiple different providers make global lookup ambiguous', async () => {
83 const clientA = new QueryClient()
84 const clientB = new QueryClient()
85 const providerA = document.createElement(tagName) as QueryClientProvider
86 const providerB = document.createElement(tagName) as QueryClientProvider
87 providerA.client = clientA
88 providerB.client = clientB
89 container.append(providerA)
90 container.append(providerB)
91 await providerA.updateComplete
92 await providerB.updateComplete
93β
94 expect(getDefaultQueryClient()).toBeUndefined()
95 expect(() => useQueryClient()).toThrow(/Multiple QueryClients are mounted/)
96 expect(() => resolveQueryClient()).toThrow(
97 /Multiple QueryClients are mounted/,
98 )
99β
100 providerB.remove()
101 expect(getDefaultQueryClient()).toBe(clientA)
102 expect(useQueryClient()).toBe(clientA)
103β
104 providerA.remove()
105 })
106β
107 it('should require an explicit client before connect', () => {
108 const provider = document.createElement(tagName) as QueryClientProvider
109β
110 expect(() => provider.connectedCallback()).toThrow(
111 /No QueryClient available/,
112 )
113 })
114β
115 it('should preserve the mount/unmount contract when the provider is swapped while disconnected', async () => {
116 const clientA = new QueryClient()
117 const clientB = new QueryClient()
118β
119 const mountA = vi.spyOn(clientA, 'mount')
120 const unmountA = vi.spyOn(clientA, 'unmount')
121 const mountB = vi.spyOn(clientB, 'mount')
122 const unmountB = vi.spyOn(clientB, 'unmount')
123β
124 const provider = document.createElement(tagName) as QueryClientProvider
125 provider.client = clientA
126 container.append(provider)
127 await provider.updateComplete
128β
129 expect(mountA).toHaveBeenCalledTimes(1)
130 expect(unmountA).toHaveBeenCalledTimes(0)
131 expect(mountB).toHaveBeenCalledTimes(0)
132 expect(unmountB).toHaveBeenCalledTimes(0)
133β
134 provider.remove()
135 await vi.advanceTimersByTimeAsync(0)
136 expect(unmountA).toHaveBeenCalledTimes(1)
137 expect(mountB).toHaveBeenCalledTimes(0)
138β
139 provider.client = clientB
140 await provider.updateComplete
141β
142 expect(unmountA).toHaveBeenCalledTimes(1)
143 expect(mountB).toHaveBeenCalledTimes(0)
144β
145 container.append(provider)
146 await provider.updateComplete
147 expect(mountA).toHaveBeenCalledTimes(1)
148 expect(unmountA).toHaveBeenCalledTimes(1)
149 expect(mountB).toHaveBeenCalledTimes(1)
150 expect(unmountB).toHaveBeenCalledTimes(0)
151β
152 provider.remove()
153 await vi.advanceTimersByTimeAsync(0)
154 expect(unmountB).toHaveBeenCalledTimes(1)
155 })
156β
157 it('should unmount the initial client when the client is replaced before the first update', async () => {
158 const clientA = new QueryClient()
159 const clientB = new QueryClient()
160β
161 const mountA = vi.spyOn(clientA, 'mount')
162 const unmountA = vi.spyOn(clientA, 'unmount')
163 const mountB = vi.spyOn(clientB, 'mount')
164 const unmountB = vi.spyOn(clientB, 'unmount')
165β
166 const provider = document.createElement(tagName) as QueryClientProvider
167 provider.client = clientA
168 container.append(provider)
169β
170 provider.client = clientB
171 await provider.updateComplete
172β
173 expect(mountA).toHaveBeenCalledTimes(1)
174 expect(unmountA).toHaveBeenCalledTimes(1)
175 expect(mountB).toHaveBeenCalledTimes(1)
176 expect(unmountB).toHaveBeenCalledTimes(0)
177 expect(useQueryClient()).toBe(clientB)
178β
179 provider.remove()
180 expect(unmountB).toHaveBeenCalledTimes(1)
181 expect(() => useQueryClient()).toThrow(/No QueryClient available/)
182 })
183β
184 it('should not throw when the client is cleared on a disconnected provider', async () => {
185 const provider = document.createElement(tagName) as QueryClientProvider
186 provider.client = queryClient
187 container.append(provider)
188 await provider.updateComplete
189β
190 provider.remove()
191 provider.client = undefined as unknown as QueryClient
192β
193 await expect(provider.updateComplete).resolves.toBe(true)
194 expect(() => useQueryClient()).toThrow(/No QueryClient available/)
195 })
196β
197 it('should tear down the mounted client before surfacing the error when a connected client is updated to an invalid value', async () => {
198 const key = createKey()
199 const mount = vi.spyOn(queryClient, 'mount')
200 const unmount = vi.spyOn(queryClient, 'unmount')
201β
202 class Consumer extends LitElement {
203 readonly query = createQueryController(this, {
204 queryKey: key,
205 queryFn: () => sleep(10).then(() => 'ok'),
206 retry: false,
207 })
208 }
209β
210 customElements.define(generateElementName(), Consumer)
211β
212 const provider = document.createElement(tagName) as QueryClientProvider
213 const consumer = new Consumer()
214 provider.client = queryClient
215 provider.append(consumer)
216 container.append(provider)
217 await provider.updateComplete
218 await consumer.updateComplete
219β
220 await vi.advanceTimersByTimeAsync(10)
221 expect(consumer.query().isSuccess).toBe(true)
222 expect(mount).toHaveBeenCalledTimes(1)
223 expect(unmount).toHaveBeenCalledTimes(0)
224 expect(consumer.query().data).toBe('ok')
225β
226 provider.client = undefined as unknown as QueryClient
227β
228 await expect(provider.updateComplete).rejects.toThrow(
229 /No QueryClient available/,
230 )
231 expect(unmount).toHaveBeenCalledTimes(1)
232 expect(getDefaultQueryClient()).toBeUndefined()
233 expect(() => useQueryClient()).toThrow(/No QueryClient available/)
234 expect(() => consumer.query()).toThrow(/No QueryClient available/)
235 await expect(consumer.query.refetch()).rejects.toThrow(
236 /No QueryClient available/,
237 )
238β
239 consumer.query.destroy()
240 provider.remove()
241 await vi.advanceTimersByTimeAsync(0)
242 expect(unmount).toHaveBeenCalledTimes(1)
243 })
244})