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

QueryClientProvider.test.ts

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