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