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

vueQueryPlugin.test.ts

398 lines11.4 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { isVue2, isVue3, ref } from 'vue-demi'
3import { queryKey } from '@tanstack/query-test-utils'
4import { QueryClient } from '../queryClient'
5import { VueQueryPlugin } from '../vueQueryPlugin'
6import { VUE_QUERY_CLIENT } from '../utils'
7import { setupDevtools } from '../devtools/devtools'
8import { useQuery } from '../useQuery'
9import { useQueries } from '../useQueries'
10import type { App, ComponentOptions } from 'vue'
11import type { Mock } from 'vitest'
12​
13vi.mock('../devtools/devtools')
14vi.mock('../useQueryClient')
15vi.mock('../useBaseQuery')
16​
17type UnmountCallback = () => void
18​
19interface TestApp extends App {
20 onUnmount: UnmountCallback
21 _unmount: UnmountCallback
22 _mixin: ComponentOptions
23 _provided: Record<string, any>
24 $root: TestApp
26​
27const itIf = (condition: boolean) => (condition ? it : it.skip)
28​
29function getAppMock(withUnmountHook = false): TestApp {
30 const mock = {
31 provide: vi.fn(),
32 unmount: vi.fn(),
33 onUnmount: withUnmountHook
34 ? vi.fn((u: UnmountCallback) => {
35 mock._unmount = u
36 })
37 : undefined,
38 mixin: (m: ComponentOptions) => {
39 mock._mixin = m
40 },
41 } as unknown as TestApp
42​
43 return mock
45​
46describe('VueQueryPlugin', () => {
47 beforeEach(() => {
48 vi.useFakeTimers()
49 })
50​
51 afterEach(() => {
52 vi.useRealTimers()
53 })
54​
55 describe('devtools', () => {
56 it('should NOT setup devtools', () => {
57 const setupDevtoolsMock = setupDevtools as Mock
58 const appMock = getAppMock()
59 VueQueryPlugin.install(appMock)
60​
61 expect(setupDevtoolsMock).toHaveBeenCalledTimes(0)
62 })
63​
64 itIf(isVue2)('should NOT setup devtools by default', () => {
65 const envCopy = process.env.NODE_ENV
66 process.env.NODE_ENV = 'development'
67 const setupDevtoolsMock = setupDevtools as Mock
68 const appMock = getAppMock()
69 VueQueryPlugin.install(appMock)
70​
71 appMock.$root = appMock
72 appMock._mixin.beforeCreate?.call(appMock)
73 process.env.NODE_ENV = envCopy
74​
75 expect(setupDevtoolsMock).toHaveBeenCalledTimes(0)
76 })
77​
78 itIf(isVue2)('should setup devtools', () => {
79 const envCopy = process.env.NODE_ENV
80 process.env.NODE_ENV = 'development'
81 const setupDevtoolsMock = setupDevtools as Mock
82 const appMock = getAppMock()
83 VueQueryPlugin.install(appMock, { enableDevtoolsV6Plugin: true })
84​
85 appMock.$root = appMock
86 appMock._mixin.beforeCreate?.call(appMock)
87 process.env.NODE_ENV = envCopy
88​
89 expect(setupDevtoolsMock).toHaveBeenCalledTimes(1)
90 })
91​
92 itIf(isVue3)('should NOT setup devtools by default', () => {
93 const envCopy = process.env.NODE_ENV
94 process.env.NODE_ENV = 'development'
95 const setupDevtoolsMock = setupDevtools as Mock
96 const appMock = getAppMock()
97 VueQueryPlugin.install(appMock)
98 process.env.NODE_ENV = envCopy
99​
100 expect(setupDevtoolsMock).toHaveBeenCalledTimes(0)
101 })
102​
103 itIf(isVue3)('should setup devtools', () => {
104 const envCopy = process.env.NODE_ENV
105 process.env.NODE_ENV = 'development'
106 const setupDevtoolsMock = setupDevtools as Mock
107 const appMock = getAppMock()
108 VueQueryPlugin.install(appMock, { enableDevtoolsV6Plugin: true })
109 process.env.NODE_ENV = envCopy
110​
111 expect(setupDevtoolsMock).toHaveBeenCalledTimes(1)
112 })
113 })
114​
115 describe('when app unmounts', () => {
116 it('should call unmount on each client when onUnmount is missing', () => {
117 const appMock = getAppMock()
118 const customClient = {
119 mount: vi.fn(),
120 unmount: vi.fn(),
121 } as unknown as QueryClient
122 const originalUnmount = appMock.unmount
123 VueQueryPlugin.install(appMock, {
124 queryClient: customClient,
125 })
126​
127 appMock.unmount()
128​
129 expect(appMock.unmount).not.toEqual(originalUnmount)
130 expect(customClient.unmount).toHaveBeenCalledTimes(1)
131 expect(originalUnmount).toHaveBeenCalledTimes(1)
132 })
133​
134 it('should call onUnmount if present', () => {
135 const appMock = getAppMock(true)
136 const customClient = {
137 mount: vi.fn(),
138 unmount: vi.fn(),
139 } as unknown as QueryClient
140 const originalUnmount = appMock.unmount
141 VueQueryPlugin.install(appMock, { queryClient: customClient })
142​
143 appMock._unmount()
144​
145 expect(appMock.unmount).toEqual(originalUnmount)
146 expect(customClient.unmount).toHaveBeenCalledTimes(1)
147 })
148 })
149​
150 describe('when called without additional options', () => {
151 itIf(isVue2)('should provide a client with default clientKey', () => {
152 const appMock = getAppMock()
153 VueQueryPlugin.install(appMock)
154​
155 appMock._mixin.beforeCreate?.call(appMock)
156​
157 expect(appMock._provided).toMatchObject({
158 VUE_QUERY_CLIENT: expect.any(QueryClient),
159 })
160 })
161​
162 itIf(isVue3)('should provide a client with default clientKey', () => {
163 const appMock = getAppMock()
164 VueQueryPlugin.install(appMock)
165​
166 expect(appMock.provide).toHaveBeenCalledWith(
167 VUE_QUERY_CLIENT,
168 expect.any(QueryClient),
169 )
170 })
171 })
172​
173 describe('when called with custom clientKey', () => {
174 itIf(isVue2)('should provide a client with customized clientKey', () => {
175 const appMock = getAppMock()
176 VueQueryPlugin.install(appMock, { queryClientKey: 'CUSTOM' })
177​
178 appMock._mixin.beforeCreate?.call(appMock)
179​
180 expect(appMock._provided).toMatchObject({
181 [VUE_QUERY_CLIENT + ':CUSTOM']: expect.any(QueryClient),
182 })
183 })
184​
185 itIf(isVue3)('should provide a client with customized clientKey', () => {
186 const appMock = getAppMock()
187 VueQueryPlugin.install(appMock, { queryClientKey: 'CUSTOM' })
188​
189 expect(appMock.provide).toHaveBeenCalledWith(
190 VUE_QUERY_CLIENT + ':CUSTOM',
191 expect.any(QueryClient),
192 )
193 })
194 })
195​
196 describe('when called with custom client', () => {
197 itIf(isVue2)('should provide that custom client', () => {
198 const appMock = getAppMock()
199 const customClient = { mount: vi.fn() } as unknown as QueryClient
200 VueQueryPlugin.install(appMock, { queryClient: customClient })
201​
202 appMock._mixin.beforeCreate?.call(appMock)
203​
204 expect(customClient.mount).toHaveBeenCalled()
205 expect(appMock._provided).toMatchObject({
206 VUE_QUERY_CLIENT: customClient,
207 })
208 })
209​
210 itIf(isVue3)('should provide that custom client', () => {
211 const appMock = getAppMock()
212 const customClient = { mount: vi.fn() } as unknown as QueryClient
213 VueQueryPlugin.install(appMock, { queryClient: customClient })
214​
215 expect(customClient.mount).toHaveBeenCalled()
216 expect(appMock.provide).toHaveBeenCalledWith(
217 VUE_QUERY_CLIENT,
218 customClient,
219 )
220 })
221 })
222​
223 describe('when called with custom client config', () => {
224 itIf(isVue2)('should instantiate a client with the provided config', () => {
225 const appMock = getAppMock()
226 const config = {
227 defaultOptions: { queries: { enabled: true } },
228 }
229 VueQueryPlugin.install(appMock, {
230 queryClientConfig: config,
231 })
232​
233 appMock._mixin.beforeCreate?.call(appMock)
234 const client = appMock._provided.VUE_QUERY_CLIENT as QueryClient
235 const defaultOptions = client.getDefaultOptions()
236​
237 expect(defaultOptions).toEqual(config.defaultOptions)
238 })
239​
240 itIf(isVue3)('should instantiate a client with the provided config', () => {
241 const appMock = getAppMock()
242 const config = {
243 defaultOptions: { queries: { enabled: true } },
244 }
245 VueQueryPlugin.install(appMock, {
246 queryClientConfig: config,
247 })
248​
249 const client = (appMock.provide as Mock).mock.calls[0]?.[1]
250 const defaultOptions = client.getDefaultOptions()
251​
252 expect(defaultOptions).toEqual(config.defaultOptions)
253 })
254 })
255​
256 describe('when persister is provided', () => {
257 it('should properly modify isRestoring flag on queryClient', async () => {
258 const appMock = getAppMock()
259 const customClient = {
260 mount: vi.fn(),
261 isRestoring: ref(false),
262 } as unknown as QueryClient
263​
264 VueQueryPlugin.install(appMock, {
265 queryClient: customClient,
266 clientPersister: () => [
267 vi.fn(),
268 new Promise((resolve) => {
269 resolve()
270 }),
271 ],
272 })
273​
274 expect(customClient.isRestoring?.value).toBe(true)
275​
276 await vi.advanceTimersByTimeAsync(0)
277​
278 expect(customClient.isRestoring?.value).toBe(false)
279 })
280​
281 it('should delay useQuery subscription and not call fetcher if data is not stale', async () => {
282 const key = queryKey()
283 const appMock = getAppMock()
284 const customClient = new QueryClient({
285 defaultOptions: {
286 queries: {
287 staleTime: 1000 * 60 * 60,
288 },
289 },
290 })
291​
292 VueQueryPlugin.install(appMock, {
293 queryClient: customClient,
294 clientPersister: (client) => [
295 vi.fn(),
296 new Promise((resolve) => {
297 setTimeout(() => {
298 client.setQueryData(key, () => ({
299 foo: 'bar',
300 }))
301 resolve()
302 }, 0)
303 }),
304 ],
305 })
306​
307 const fnSpy = vi.fn()
308​
309 const query = useQuery(
310 {
311 queryKey: key,
312 queryFn: fnSpy,
313 },
314 customClient,
315 )
316​
317 expect(customClient.isRestoring?.value).toBe(true)
318 expect(query.isFetching.value).toBe(false)
319 expect(query.data.value).toStrictEqual(undefined)
320 expect(fnSpy).toHaveBeenCalledTimes(0)
321​
322 await vi.advanceTimersByTimeAsync(0)
323​
324 expect(customClient.isRestoring?.value).toBe(false)
325 expect(query.data.value).toStrictEqual({ foo: 'bar' })
326 expect(fnSpy).toHaveBeenCalledTimes(0)
327 })
328​
329 it('should delay useQueries subscription and not call fetcher if data is not stale', async () => {
330 const key1 = queryKey()
331 const key2 = queryKey()
332 const appMock = getAppMock()
333 const customClient = new QueryClient({
334 defaultOptions: {
335 queries: {
336 staleTime: 1000 * 60 * 60,
337 },
338 },
339 })
340​
341 VueQueryPlugin.install(appMock, {
342 queryClient: customClient,
343 clientPersister: (client) => [
344 vi.fn(),
345 new Promise((resolve) => {
346 setTimeout(() => {
347 client.setQueryData(key1, () => ({
348 foo1: 'bar1',
349 }))
350 client.setQueryData(key2, () => ({
351 foo2: 'bar2',
352 }))
353 resolve()
354 }, 0)
355 }),
356 ],
357 })
358​
359 const fnSpy = vi.fn()
360​
361 const query = useQuery(
362 {
363 queryKey: key1,
364 queryFn: fnSpy,
365 },
366 customClient,
367 )
368​
369 const queries = useQueries(
370 {
371 queries: [
372 {
373 queryKey: key2,
374 queryFn: fnSpy,
375 },
376 ],
377 },
378 customClient,
379 )
380​
381 expect(customClient.isRestoring?.value).toBe(true)
382​
383 expect(query.isFetching.value).toBe(false)
384 expect(query.data.value).toStrictEqual(undefined)
385​
386 expect(queries.value[0].isFetching).toBe(false)
387 expect(queries.value[0].data).toStrictEqual(undefined)
388 expect(fnSpy).toHaveBeenCalledTimes(0)
389​
390 await vi.advanceTimersByTimeAsync(0)
391​
392 expect(customClient.isRestoring?.value).toBe(false)
393 expect(query.data.value).toStrictEqual({ foo1: 'bar1' })
394 expect(queries.value[0].data).toStrictEqual({ foo2: 'bar2' })
395 expect(fnSpy).toHaveBeenCalledTimes(0)
396 })
397 })
398})