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