Open History to see the last commit
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(),
31}
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})