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

QueryClientProvider.test.ts

244 lines7.8 KB

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})