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

providers.test.ts

312 lines7.6 KB

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