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

providers.test.ts

321 lines8.0 KB

1import { beforeEach, describe, expect, test, vi } from 'vitest'
2import { QueryClient } from '@tanstack/query-core'
3import { TestBed } from '@angular/core/testing'
4import {
5 ENVIRONMENT_INITIALIZER,
6 provideExperimentalZonelessChangeDetection,
7 signal,
8} from '@angular/core'
9import { isDevMode } from '../util/is-dev-mode/is-dev-mode'
10import { provideTanStackQuery, withDevtools } from '../providers'
11import type { DevtoolsOptions } from '../providers'
12import type { Mock } from 'vitest'
13import type {
14 DevtoolsButtonPosition,
15 DevtoolsErrorType,
16 DevtoolsPosition,
17} from '@tanstack/query-devtools'
18​
19vi.mock('../util/is-dev-mode/is-dev-mode', () => ({
20 isDevMode: vi.fn(),
21}))
22​
23const mockDevtoolsInstance = {
24 mount: vi.fn(),
25 unmount: vi.fn(),
26 setClient: vi.fn(),
27 setPosition: vi.fn(),
28 setErrorTypes: vi.fn(),
29 setButtonPosition: vi.fn(),
30 setInitialIsOpen: vi.fn(),
32​
33const mockTanstackQueryDevtools = vi.fn(() => mockDevtoolsInstance)
34​
35vi.mock('@tanstack/query-devtools', () => ({
36 TanstackQueryDevtools: mockTanstackQueryDevtools,
37}))
38​
39describe('withDevtools feature', () => {
40 let isDevModeMock: Mock
41​
42 beforeEach(() => {
43 vi.useFakeTimers()
44 isDevModeMock = isDevMode as Mock
45 })
46​
47 afterEach(() => {
48 vi.restoreAllMocks()
49 })
50​
51 test.each([
52 {
53 description:
54 'should provide developer tools in development mode by default',
55 isDevModeValue: true,
56 expectedCalled: true,
57 },
58 {
59 description:
60 'should not provide developer tools in production mode by default',
61 isDevModeValue: false,
62 expectedCalled: false,
63 },
64 {
65 description: `should provide developer tools in development mode when 'loadDeveloperTools' is set to 'auto'`,
66 isDevModeValue: true,
67 loadDevtools: 'auto',
68 expectedCalled: true,
69 },
70 {
71 description: `should not provide developer tools in production mode when 'loadDeveloperTools' is set to 'auto'`,
72 isDevModeValue: false,
73 loadDevtools: 'auto',
74 expectedCalled: false,
75 },
76 {
77 description:
78 "should provide developer tools in development mode when 'loadDevtools' is set to true",
79 isDevModeValue: true,
80 loadDevtools: true,
81 expectedCalled: true,
82 },
83 {
84 description:
85 "should provide developer tools in production mode when 'loadDevtools' is set to true",
86 isDevModeValue: false,
87 loadDevtools: true,
88 expectedCalled: true,
89 },
90 {
91 description:
92 "should not provide developer tools in development mode when 'loadDevtools' is set to false",
93 isDevModeValue: true,
94 loadDevtools: false,
95 expectedCalled: false,
96 },
97 {
98 description:
99 "should not provide developer tools in production mode when 'loadDevtools' is set to false",
100 isDevModeValue: false,
101 loadDevtools: false,
102 expectedCalled: false,
103 },
104 ])(
105 '$description',
106 async ({ isDevModeValue, loadDevtools, expectedCalled }) => {
107 isDevModeMock.mockReturnValue(isDevModeValue)
108​
109 const providers = [
110 provideExperimentalZonelessChangeDetection(),
111 provideTanStackQuery(
112 new QueryClient(),
113 loadDevtools !== undefined
114 ? withDevtools(
115 () =>
116 ({
117 loadDevtools,
118 }) as DevtoolsOptions,
119 )
120 : withDevtools(),
121 ),
122 ]
123​
124 TestBed.configureTestingModule({
125 providers,
126 })
127​
128 TestBed.inject(ENVIRONMENT_INITIALIZER)
129 await vi.runAllTimersAsync()
130​
131 if (expectedCalled) {
132 expect(mockTanstackQueryDevtools).toHaveBeenCalled()
133 } else {
134 expect(mockTanstackQueryDevtools).not.toHaveBeenCalled()
135 }
136 },
137 )
138​
139 it('should update error types', async () => {
140 const errorTypes = signal([] as Array<DevtoolsErrorType>)
141​
142 TestBed.configureTestingModule({
143 providers: [
144 provideExperimentalZonelessChangeDetection(),
145 provideTanStackQuery(
146 new QueryClient(),
147 withDevtools(() => ({
148 loadDevtools: true,
149 errorTypes: errorTypes(),
150 })),
151 ),
152 ],
153 })
154​
155 TestBed.inject(ENVIRONMENT_INITIALIZER)
156 await vi.runAllTimersAsync()
157​
158 TestBed.flushEffects()
159​
160 expect(mockDevtoolsInstance.setErrorTypes).toHaveBeenCalledTimes(0)
161​
162 errorTypes.set([
163 {
164 name: '',
165 initializer: () => new Error(),
166 },
167 ])
168​
169 TestBed.flushEffects()
170​
171 expect(mockDevtoolsInstance.setErrorTypes).toHaveBeenCalledTimes(1)
172 })
173​
174 it('should update client', async () => {
175 const client = signal(new QueryClient())
176​
177 TestBed.configureTestingModule({
178 providers: [
179 provideExperimentalZonelessChangeDetection(),
180 provideTanStackQuery(
181 new QueryClient(),
182 withDevtools(() => ({
183 loadDevtools: true,
184 client: client(),
185 })),
186 ),
187 ],
188 })
189​
190 TestBed.inject(ENVIRONMENT_INITIALIZER)
191 await vi.runAllTimersAsync()
192​
193 TestBed.flushEffects()
194​
195 expect(mockDevtoolsInstance.setClient).toHaveBeenCalledTimes(0)
196​
197 client.set(new QueryClient())
198​
199 TestBed.flushEffects()
200​
201 expect(mockDevtoolsInstance.setClient).toHaveBeenCalledTimes(1)
202 })
203​
204 it('should update position', async () => {
205 const position = signal<DevtoolsPosition>('top')
206​
207 TestBed.configureTestingModule({
208 providers: [
209 provideExperimentalZonelessChangeDetection(),
210 provideTanStackQuery(
211 new QueryClient(),
212 withDevtools(() => ({
213 loadDevtools: true,
214 position: position(),
215 })),
216 ),
217 ],
218 })
219​
220 TestBed.inject(ENVIRONMENT_INITIALIZER)
221 await vi.runAllTimersAsync()
222​
223 TestBed.flushEffects()
224​
225 expect(mockDevtoolsInstance.setPosition).toHaveBeenCalledTimes(0)
226​
227 position.set('left')
228​
229 TestBed.flushEffects()
230​
231 expect(mockDevtoolsInstance.setPosition).toHaveBeenCalledTimes(1)
232 })
233​
234 it('should update button position', async () => {
235 const buttonPosition = signal<DevtoolsButtonPosition>('bottom-left')
236​
237 TestBed.configureTestingModule({
238 providers: [
239 provideExperimentalZonelessChangeDetection(),
240 provideTanStackQuery(
241 new QueryClient(),
242 withDevtools(() => ({
243 loadDevtools: true,
244 buttonPosition: buttonPosition(),
245 })),
246 ),
247 ],
248 })
249​
250 TestBed.inject(ENVIRONMENT_INITIALIZER)
251 await vi.runAllTimersAsync()
252​
253 TestBed.flushEffects()
254​
255 expect(mockDevtoolsInstance.setButtonPosition).toHaveBeenCalledTimes(0)
256​
257 buttonPosition.set('bottom-right')
258​
259 TestBed.flushEffects()
260​
261 expect(mockDevtoolsInstance.setButtonPosition).toHaveBeenCalledTimes(1)
262 })
263​
264 it('should update initialIsOpen', async () => {
265 const initialIsOpen = signal(false)
266​
267 TestBed.configureTestingModule({
268 providers: [
269 provideExperimentalZonelessChangeDetection(),
270 provideTanStackQuery(
271 new QueryClient(),
272 withDevtools(() => ({
273 loadDevtools: true,
274 initialIsOpen: initialIsOpen(),
275 })),
276 ),
277 ],
278 })
279​
280 TestBed.inject(ENVIRONMENT_INITIALIZER)
281 await vi.runAllTimersAsync()
282​
283 TestBed.flushEffects()
284​
285 expect(mockDevtoolsInstance.setInitialIsOpen).toHaveBeenCalledTimes(0)
286​
287 initialIsOpen.set(true)
288​
289 TestBed.flushEffects()
290​
291 expect(mockDevtoolsInstance.setInitialIsOpen).toHaveBeenCalledTimes(1)
292 })
293​
294 it('should destroy devtools', async () => {
295 const loadDevtools = signal(true)
296​
297 TestBed.configureTestingModule({
298 providers: [
299 provideExperimentalZonelessChangeDetection(),
300 provideTanStackQuery(
301 new QueryClient(),
302 withDevtools(() => ({
303 loadDevtools: loadDevtools(),
304 })),
305 ),
306 ],
307 })
308​
309 TestBed.inject(ENVIRONMENT_INITIALIZER)
310 await vi.runAllTimersAsync()
311​
312 expect(mockDevtoolsInstance.mount).toHaveBeenCalledTimes(1)
313 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(0)
314​
315 loadDevtools.set(false)
316​
317 TestBed.flushEffects()
318​
319 expect(mockDevtoolsInstance.unmount).toHaveBeenCalledTimes(1)
320 })
321})