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

vueQueryPlugin.test.ts

256 lines7.5 KB

1import type { App, ComponentOptions } from 'vue'
2import { isVue2, isVue3 } from 'vue-demi'
3​
4import type { QueryClient } from '../queryClient'
5import { VueQueryPlugin } from '../vueQueryPlugin'
6import { VUE_QUERY_CLIENT } from '../utils'
7import { setupDevtools } from '../devtools/devtools'
8​
9jest.mock('../devtools/devtools')
10​
11interface TestApp extends App {
12 onUnmount: Function
13 _unmount: Function
14 _mixin: ComponentOptions
15 _provided: Record<string, any>
16 $root: TestApp
18​
19const testIf = (condition: boolean) => (condition ? test : test.skip)
20​
21function getAppMock(withUnmountHook = false): TestApp {
22 const mock = {
23 provide: jest.fn(),
24 unmount: jest.fn(),
25 onUnmount: withUnmountHook
26 ? jest.fn((u: Function) => {
27 mock._unmount = u
28 })
29 : undefined,
30 mixin: (m: ComponentOptions): any => {
31 mock._mixin = m
32 },
33 } as unknown as TestApp
34​
35 return mock
37​
38describe('VueQueryPlugin', () => {
39 beforeEach(() => {
40 window.__VUE_QUERY_CONTEXT__ = undefined
41 })
42​
43 describe('devtools', () => {
44 test('should NOT setup devtools', () => {
45 const setupDevtoolsMock = setupDevtools as jest.Mock
46 const appMock = getAppMock()
47 VueQueryPlugin.install(appMock)
48​
49 expect(setupDevtoolsMock).toHaveBeenCalledTimes(0)
50 })
51​
52 testIf(isVue2)('should setup devtools', () => {
53 const envCopy = process.env.NODE_ENV
54 process.env.NODE_ENV = 'development'
55 const setupDevtoolsMock = setupDevtools as jest.Mock
56 const appMock = getAppMock()
57 VueQueryPlugin.install(appMock)
58​
59 appMock.$root = appMock
60 appMock._mixin.beforeCreate?.call(appMock)
61 process.env.NODE_ENV = envCopy
62​
63 expect(setupDevtoolsMock).toHaveBeenCalledTimes(1)
64 })
65​
66 testIf(isVue3)('should setup devtools', () => {
67 const envCopy = process.env.NODE_ENV
68 process.env.NODE_ENV = 'development'
69 const setupDevtoolsMock = setupDevtools as jest.Mock
70 const appMock = getAppMock()
71 VueQueryPlugin.install(appMock)
72 process.env.NODE_ENV = envCopy
73​
74 expect(setupDevtoolsMock).toHaveBeenCalledTimes(1)
75 })
76 })
77​
78 describe('when app unmounts', () => {
79 test('should call unmount on each client when onUnmount is missing', () => {
80 const appMock = getAppMock()
81 const customClient = {
82 mount: jest.fn(),
83 unmount: jest.fn(),
84 } as unknown as QueryClient
85 const originalUnmount = appMock.unmount
86 VueQueryPlugin.install(appMock, {
87 queryClient: customClient,
88 })
89​
90 appMock.unmount()
91​
92 expect(appMock.unmount).not.toEqual(originalUnmount)
93 expect(customClient.unmount).toHaveBeenCalledTimes(1)
94 expect(originalUnmount).toHaveBeenCalledTimes(1)
95 })
96​
97 test('should call onUnmount if present', () => {
98 const appMock = getAppMock(true)
99 const customClient = {
100 mount: jest.fn(),
101 unmount: jest.fn(),
102 } as unknown as QueryClient
103 const originalUnmount = appMock.unmount
104 VueQueryPlugin.install(appMock, { queryClient: customClient })
105​
106 appMock._unmount()
107​
108 expect(appMock.unmount).toEqual(originalUnmount)
109 expect(customClient.unmount).toHaveBeenCalledTimes(1)
110 })
111 })
112​
113 describe('when called without additional options', () => {
114 testIf(isVue2)('should provide a client with default clientKey', () => {
115 const appMock = getAppMock()
116 VueQueryPlugin.install(appMock)
117​
118 appMock._mixin.beforeCreate?.call(appMock)
119​
120 expect(appMock._provided).toMatchObject({
121 VUE_QUERY_CLIENT: expect.objectContaining({ defaultOptions: {} }),
122 })
123 })
124​
125 testIf(isVue3)('should provide a client with default clientKey', () => {
126 const appMock = getAppMock()
127 VueQueryPlugin.install(appMock)
128​
129 expect(appMock.provide).toHaveBeenCalledWith(
130 VUE_QUERY_CLIENT,
131 expect.objectContaining({ defaultOptions: {} }),
132 )
133 })
134 })
135​
136 describe('when called with custom clientKey', () => {
137 testIf(isVue2)('should provide a client with customized clientKey', () => {
138 const appMock = getAppMock()
139 VueQueryPlugin.install(appMock, { queryClientKey: 'CUSTOM' })
140​
141 appMock._mixin.beforeCreate?.call(appMock)
142​
143 expect(appMock._provided).toMatchObject({
144 [VUE_QUERY_CLIENT + ':CUSTOM']: expect.objectContaining({
145 defaultOptions: {},
146 }),
147 })
148 })
149​
150 testIf(isVue3)('should provide a client with customized clientKey', () => {
151 const appMock = getAppMock()
152 VueQueryPlugin.install(appMock, { queryClientKey: 'CUSTOM' })
153​
154 expect(appMock.provide).toHaveBeenCalledWith(
155 VUE_QUERY_CLIENT + ':CUSTOM',
156 expect.objectContaining({ defaultOptions: {} }),
157 )
158 })
159 })
160​
161 describe('when called with custom client', () => {
162 testIf(isVue2)('should provide that custom client', () => {
163 const appMock = getAppMock()
164 const customClient = { mount: jest.fn() } as unknown as QueryClient
165 VueQueryPlugin.install(appMock, { queryClient: customClient })
166​
167 appMock._mixin.beforeCreate?.call(appMock)
168​
169 expect(customClient.mount).toHaveBeenCalled()
170 expect(appMock._provided).toMatchObject({
171 VUE_QUERY_CLIENT: customClient,
172 })
173 })
174​
175 testIf(isVue3)('should provide that custom client', () => {
176 const appMock = getAppMock()
177 const customClient = { mount: jest.fn() } as unknown as QueryClient
178 VueQueryPlugin.install(appMock, { queryClient: customClient })
179​
180 expect(customClient.mount).toHaveBeenCalled()
181 expect(appMock.provide).toHaveBeenCalledWith(
182 VUE_QUERY_CLIENT,
183 customClient,
184 )
185 })
186 })
187​
188 describe('when called with custom client config', () => {
189 testIf(isVue2)(
190 'should instantiate a client with the provided config',
191 () => {
192 const appMock = getAppMock()
193 const config = {
194 defaultOptions: { queries: { enabled: true } },
195 }
196 VueQueryPlugin.install(appMock, {
197 queryClientConfig: config,
198 })
199​
200 appMock._mixin.beforeCreate?.call(appMock)
201​
202 expect(appMock._provided).toMatchObject({
203 VUE_QUERY_CLIENT: expect.objectContaining(config),
204 })
205 },
206 )
207​
208 testIf(isVue3)(
209 'should instantiate a client with the provided config',
210 () => {
211 const appMock = getAppMock()
212 const config = {
213 defaultOptions: { queries: { enabled: true } },
214 }
215 VueQueryPlugin.install(appMock, {
216 queryClientConfig: config,
217 })
218​
219 expect(appMock.provide).toHaveBeenCalledWith(
220 VUE_QUERY_CLIENT,
221 expect.objectContaining(config),
222 )
223 },
224 )
225 })
226​
227 describe('when context sharing is enabled', () => {
228 test('should create context if it does not exist', () => {
229 const appMock = getAppMock()
230 VueQueryPlugin.install(appMock, { contextSharing: true })
231​
232 expect(window.__VUE_QUERY_CONTEXT__).toBeTruthy()
233 })
234​
235 test('should create context with options if it does not exist', () => {
236 const appMock = getAppMock()
237 VueQueryPlugin.install(appMock, {
238 contextSharing: true,
239 queryClientConfig: { defaultOptions: { queries: { staleTime: 5000 } } },
240 })
241​
242 expect(
243 window.__VUE_QUERY_CONTEXT__?.getDefaultOptions().queries?.staleTime,
244 ).toEqual(5000)
245 })
246​
247 test('should use existing context', () => {
248 const customClient = { mount: jest.fn() } as unknown as QueryClient
249 window.__VUE_QUERY_CONTEXT__ = customClient
250 const appMock = getAppMock()
251 VueQueryPlugin.install(appMock, { contextSharing: true })
252​
253 expect(customClient.mount).toHaveBeenCalledTimes(1)
254 })
255 })
256})