-
Notifications
You must be signed in to change notification settings - Fork 244
/
config.spec.ts
351 lines (307 loc) · 9.48 KB
/
config.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { defineComponent, ComponentPublicInstance, h, inject } from 'vue'
import type { App, ComponentCustomProperties } from 'vue'
import { config, mount } from '../src'
import Hello from './components/Hello.vue'
import ComponentWithSlots from './components/ComponentWithSlots.vue'
import { Router } from 'vue-router'
describe('config', () => {
beforeEach(() => {
config.global = {
components: {},
directives: {},
mixins: [],
plugins: [],
mocks: {},
provide: {},
stubs: {},
config: {},
renderStubDefaultSlot: false
}
vi.clearAllMocks()
})
describe('config merger', () => {
it('should merge the configs in the correct order', () => {
config.global.config.globalProperties = {
myProp: 1
} as unknown as ComponentCustomProperties & Record<string, any>
config.global.components = { Hello }
const comp = mount(ComponentWithSlots, {
slots: {
default: '<div id="default-slot" /><Hello />'
},
shallow: true,
global: {
config: {
globalProperties: {
myProp: 2
} as unknown as ComponentCustomProperties & Record<string, any>
},
renderStubDefaultSlot: true
}
})
// mount config overrides user defined config
expect(comp.vm.$.appContext.config.globalProperties.myProp).toBe(2)
// mount config overrides default config
expect(comp.find('#default-slot').exists()).toBe(true)
// user defined config overrides default config
expect(comp.findComponent(Hello).exists()).toBe(true)
})
})
describe('config integrity', () => {
it('should not leak config when plugins overwrite globalProperties', async () => {
// test with a function because it's not an "easy to clone" primitive type
const globalRouterMock = { push: vi.fn() }
const pluginRouterMock = { push: vi.fn() }
const Component = defineComponent({ template: '<div />' })
class Plugin {
static install(_app: App) {
_app.config.globalProperties.$router =
pluginRouterMock as unknown as Router
}
}
config.global.config.globalProperties = {
$router: globalRouterMock as unknown as Router
} as unknown as ComponentCustomProperties & Record<string, any>
// first with plugin to overwrite globalRouterMock with pluginRouterMock
const wrapper1 = mount(Component, {
global: {
plugins: [Plugin]
}
})
// then without plugin to check if the plugin overwrite is gone
const wrapper2 = mount(Component)
wrapper1.vm.$router.push('/route-1')
wrapper2.vm.$router.push('/route-2')
expect(pluginRouterMock.push).toHaveBeenCalledTimes(1)
expect(pluginRouterMock.push).toHaveBeenCalledWith('/route-1')
expect(globalRouterMock.push).toHaveBeenCalledTimes(1)
expect(globalRouterMock.push).toHaveBeenCalledWith('/route-2')
})
})
describe('renderStubDefaultSlot', () => {
it('should override shallow option when set to true', () => {
const comp = mount(ComponentWithSlots, {
slots: {
default: '<div id="default-slot" />'
},
shallow: true,
global: {
renderStubDefaultSlot: true
}
})
expect(comp.find('#default-slot').exists()).toBe(true)
})
it('should evaluate given option as boolean', () => {
const Child = defineComponent({ template: '<div><slot></slot></div>' })
const Component = defineComponent({
components: { Child },
template: '<child><div id="default-slot" /></child>'
})
const comp = mount(Component, {
shallow: true,
global: {
// @ts-expect-error
renderStubDefaultSlot: 'truthy'
}
})
expect(comp.find('#default-slot').exists()).toBe(true)
const comp2 = mount(Component, {
shallow: true,
global: {
// @ts-expect-error
renderStubDefaultSlot: 0
}
})
expect(comp2.find('#default-slot').exists()).toBe(false)
})
})
describe('components', () => {
const Component = {
components: { Hello },
template: '<div>{{ msg }} <Hello /></div>',
props: ['msg']
}
it('allows setting components globally', () => {
const HelloLocal = {
name: 'Hello',
render() {
return h('div', 'Hello Local')
}
}
config.global.components = { Hello: HelloLocal }
const wrapper1 = mount(Component, {
props: { msg: 'Wrapper1' }
})
const wrapper2 = mount(Component, {
props: { msg: 'Wrapper2' }
})
expect(wrapper1.text()).toEqual('Wrapper1 Hello Local')
expect(wrapper2.text()).toEqual('Wrapper2 Hello Local')
})
it('allows overwriting globally set component config on a per mount instance', () => {
config.global.components = { Hello }
const HelloLocal = { template: '<div>Hello Overwritten</div>' }
const wrapper1 = mount(Component, { props: { msg: 'Wrapper1' } })
const wrapper2 = mount(Component, {
props: { msg: 'Wrapper2' },
global: { components: { Hello: HelloLocal } }
})
expect(wrapper1.text()).toEqual('Wrapper1 Hello world')
expect(wrapper2.text()).toEqual('Wrapper2 Hello Overwritten')
})
})
describe('directives', () => {
const Directive = {
beforeMount(el: Element) {
el.classList.add('DirectiveAdded')
}
}
const Component = { template: '<div v-directive>msg</div>' }
it('allows setting directives globally', () => {
config.global.directives = { Directive }
expect(mount(Component).classes()).toContain('DirectiveAdded')
expect(mount(Component).classes()).toContain('DirectiveAdded')
})
it('allows overwriting globally set directives', () => {
config.global.directives = { Directive }
const LocalDirective = {
beforeMount(el: Element) {
el.classList.add('LocallyDirectiveAdded')
}
}
expect(mount(Component).classes()).toContain('DirectiveAdded')
expect(
mount(Component, {
global: { directives: { Directive: LocalDirective } }
}).classes()
).toContain('LocallyDirectiveAdded')
})
})
describe('mocks', () => {
it('sets mock everywhere', () => {
config.global.mocks = {
foo: 'bar'
}
const Component = { template: '<div>{{ foo }}</div>' }
expect(mount(Component).text()).toEqual('bar')
expect(mount(Component).text()).toEqual('bar')
})
it('allows overwriting a global mock', () => {
config.global.mocks = {
foo: 'bar'
}
const Component = { template: '<div>{{ foo }}</div>' }
expect(mount(Component).text()).toEqual('bar')
expect(
mount(Component, { global: { mocks: { foo: 'baz' } } }).text()
).toEqual('baz')
})
})
describe('provide', () => {
const Comp = {
setup() {
const theme = inject<string>('theme')
return () => h('div', theme)
}
}
it('sets a provide everywhere', () => {
config.global.provide = {
theme: 'dark'
}
const wrapper = mount(Comp)
expect(wrapper.html()).toContain('dark')
})
it('overrides with a local provide', () => {
config.global.provide = {
theme: 'dark'
}
const wrapper = mount(Comp, {
global: {
provide: {
theme: 'light'
}
}
})
expect(wrapper.html()).toContain('light')
})
})
describe('mixins', () => {
const createdHook = vi.fn()
const mixin = {
created() {
createdHook()
}
}
const Component = {
render() {
return h('div')
}
}
it('sets a mixin everywhere', () => {
config.global.mixins = [mixin]
mount(Component)
// once on root, once in the mounted component
expect(createdHook).toHaveBeenCalledTimes(2)
})
it('concat with locally defined mixins', () => {
config.global.mixins = [mixin]
const localHook = vi.fn()
const localMixin = {
created() {
localHook(this.$options!.name)
}
} as Partial<ComponentPublicInstance>
mount(Component, {
global: {
mixins: [localMixin]
}
})
// once on root, once in the mounted component
expect(localHook).toHaveBeenCalledTimes(2)
expect(createdHook).toHaveBeenCalledTimes(2)
})
})
describe('stubs', () => {
const Foo = {
name: 'Foo',
render() {
return h('div', 'real foo')
}
}
const Component = {
render() {
return h('div', h(Foo))
}
}
beforeEach(() => {
config.global.stubs = {
Foo: {
name: 'Foo',
render() {
return h('div', 'config foo stub')
}
}
}
})
it('sets a stub globally', () => {
const wrapper = mount(Component)
// once on root, once in the mounted component
expect(wrapper.html()).toContain('config foo stub')
})
it('overrides config stub with locally defined stub', () => {
const wrapper = mount(Component, {
global: {
stubs: {
Foo: {
render() {
return h('div', 'local foo stub')
}
}
}
}
})
expect(wrapper.html()).toContain('local foo stub')
})
})
})