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

QueryClientProvider.test.ts

249 lines8.0 KB

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