/
compat.spec.ts
232 lines (189 loc) · 5.79 KB
/
compat.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
import { beforeEach, describe, expect, it, vi } from 'vitest'
import * as mockVue from '@vue/compat'
import { mount } from '../../src'
vi.mock('vue', () => mockVue)
const { configureCompat, defineComponent, h } = mockVue
// @ts-expect-error @vue/compat does not expose default export in types
const Vue = mockVue.default
describe('@vue/compat build', () => {
describe.each(['suppress-warning', false])(
'when RENDER_FUNCTION compat is %p',
(RENDER_FUNCTION) => {
beforeEach(() => {
configureCompat({ MODE: 3, RENDER_FUNCTION })
})
it('renders default slot content when renderStubDefaultSlot is true', () => {
const Foo = { template: '<div><slot></slot></div>' }
const Component = {
components: { Foo },
template: '<foo>test</foo>'
}
const wrapper = mount(Component, {
global: {
stubs: { Foo: true },
renderStubDefaultSlot: true
}
})
expect(wrapper.html()).toBe('<foo-stub>test</foo-stub>')
})
it('correctly renders transition', () => {
const Component = defineComponent({
template: '<transition><div class="hello"></div></transition>'
})
const wrapper = mount(Component)
expect(wrapper.find('.hello').exists()).toBe(true)
})
}
)
it('finds components declared with legacy Vue.extend', () => {
configureCompat({ MODE: 3, GLOBAL_EXTEND: 'suppress-warning' })
const LegacyComponent = Vue.extend({
template: '<div>LEGACY</div>'
})
const Component = defineComponent({
components: {
LegacyComponent
},
template: '<div><legacy-component /></div>'
})
const wrapper = mount(Component)
expect(wrapper.findComponent(LegacyComponent).exists()).toBe(true)
})
it('correctly mounts legacy functional component', () => {
configureCompat({ MODE: 3, COMPONENT_FUNCTIONAL: 'suppress-warning' })
const Component = defineComponent({
functional: true,
render: () => h('div', 'test')
})
const wrapper = mount(Component)
expect(wrapper.html()).toBe('<div>test</div>')
})
it('does not stub root legacy functional component when shallow', () => {
configureCompat({
MODE: 3,
GLOBAL_EXTEND: 'suppress-warning',
COMPONENT_FUNCTIONAL: 'suppress-warning'
})
const Foo = {
name: 'Foo',
functional: true,
render: () => h('div', 'test')
}
const wrapper = mount(Foo, { shallow: true })
expect(wrapper.html()).toBe('<div>test</div>')
})
it('correctly mounts legacy functional component wrapped in Vue.extend', () => {
configureCompat({
MODE: 3,
GLOBAL_EXTEND: 'suppress-warning',
COMPONENT_FUNCTIONAL: 'suppress-warning'
})
const Component = Vue.extend({
functional: true,
render: () => h('div', 'test')
})
const wrapper = mount(Component)
expect(wrapper.html()).toBe('<div>test</div>')
})
it('correctly stubs legacy component wrapped in Vue.extend', () => {
configureCompat({
MODE: 3,
GLOBAL_EXTEND: 'suppress-warning'
})
const Foo = Vue.extend({
name: 'Foo',
template: '<div>original</div>'
})
const FooStub = { template: '<div>stubbed</div>' }
const Component = {
components: { NamedAsNotFoo: Foo },
template: '<named-as-not-foo />'
}
const wrapper = mount(Component, {
global: {
stubs: {
Foo: FooStub
}
}
})
expect(wrapper.html()).toBe('<div>stubbed</div>')
})
it('correctly uses stubs when stub is legacy component', () => {
configureCompat({
MODE: 3,
GLOBAL_EXTEND: 'suppress-warning',
GLOBAL_MOUNT: 'suppress-warning'
})
const Foo = {
name: 'Foo',
template: '<div>original</div>'
}
const FooStub = Vue.extend({ template: '<div>stubbed</div>' })
const Component = {
components: { NamedAsNotFoo: Foo },
template: '<named-as-not-foo />'
}
const wrapper = mount(Component, {
global: {
stubs: {
Foo: FooStub
}
}
})
expect(wrapper.findComponent(Foo).html()).toBe('<div>stubbed</div>')
})
it('wrapper.vm points to correct instance when component is wrapped with Vue.extend', () => {
configureCompat({
MODE: 3,
GLOBAL_EXTEND: 'suppress-warning',
GLOBAL_MOUNT: 'suppress-warning'
})
const Component = Vue.extend({
data() {
return { foo: 'bar' }
},
template: '<div></div>'
})
const wrapper = mount(Component)
expect(wrapper.vm.foo).toBe('bar')
})
it('correctly passes all props to functional component', async () => {
configureCompat({
MODE: 3,
INSTANCE_LISTENERS: 'suppress-warning',
INSTANCE_ATTRS_CLASS_STYLE: 'suppress-warning'
})
const FunctionalComponent = {
functional: true,
render(h: any, context: any) {
return h('div', context.data, context.props.text)
}
}
const onClick = vi.fn()
const wrapper = mount(FunctionalComponent, {
props: {
class: 'foo',
text: 'message',
style: { color: 'red' },
onClick
}
})
expect(wrapper.text()).toBe('message')
await wrapper.trigger('click')
expect(onClick).toHaveBeenCalled()
expect(wrapper.html()).toBe(
'<div class="foo" text="message" style="color: red;">message</div>'
)
})
it('does not erase globalProperties added by messing with Vue.prototype', () => {
configureCompat({
MODE: 3,
GLOBAL_PROTOTYPE: 'suppress-warning'
})
Vue.prototype.$test = 1
const Component = { template: 'hello ' }
const wrapper = mount(Component)
// @ts-expect-error $test "magically" appears from "Vue.prototype" in compat build
expect(wrapper.vm.$test).toBe(1)
})
})