Skip to content

Commit 1d1af40

Browse files
committed
wip: tests for global api compat
1 parent 86703c2 commit 1d1af40

File tree

7 files changed

+466
-67
lines changed

7 files changed

+466
-67
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,335 @@
11
import Vue from '@vue/compat'
2+
import { effect, isReactive } from '@vue/reactivity'
3+
import {
4+
DeprecationTypes,
5+
deprecationData,
6+
toggleDeprecationWarning
7+
} from '../compatConfig'
28

3-
describe('compat: global API', () => {
4-
beforeEach(() => Vue.configureCompat({ MODE: 2 }))
5-
afterEach(() => Vue.configureCompat({ MODE: 3 }))
9+
beforeEach(() => {
10+
Vue.configureCompat({ MODE: 2 })
11+
})
12+
13+
afterEach(() => {
14+
Vue.configureCompat({ MODE: 3 })
15+
toggleDeprecationWarning(false)
16+
})
17+
18+
describe('GLOBAL_MOUNT', () => {
19+
test('new Vue() with el', () => {
20+
toggleDeprecationWarning(true)
621

7-
test('should work', () => {
822
const el = document.createElement('div')
923
el.innerHTML = `{{ msg }}`
1024
new Vue({
1125
el,
26+
compatConfig: { GLOBAL_MOUNT: true },
1227
data() {
1328
return {
1429
msg: 'hello'
1530
}
1631
}
1732
})
18-
expect('global app bootstrapping API has changed').toHaveBeenWarned()
33+
expect(
34+
deprecationData[DeprecationTypes.GLOBAL_MOUNT].message
35+
).toHaveBeenWarned()
1936
expect(el.innerHTML).toBe('hello')
2037
})
38+
39+
test('new Vue() + $mount', () => {
40+
const el = document.createElement('div')
41+
el.innerHTML = `{{ msg }}`
42+
new Vue({
43+
data() {
44+
return {
45+
msg: 'hello'
46+
}
47+
}
48+
}).$mount(el)
49+
expect(el.innerHTML).toBe('hello')
50+
})
51+
})
52+
53+
describe('GLOBAL_MOUNT_CONTAINER', () => {
54+
test('should warn', () => {
55+
toggleDeprecationWarning(true)
56+
57+
const el = document.createElement('div')
58+
el.innerHTML = `test`
59+
el.setAttribute('v-bind:id', 'foo')
60+
new Vue().$mount(el)
61+
// warning only
62+
expect(
63+
deprecationData[DeprecationTypes.GLOBAL_MOUNT].message
64+
).toHaveBeenWarned()
65+
expect(
66+
deprecationData[DeprecationTypes.GLOBAL_MOUNT_CONTAINER].message
67+
).toHaveBeenWarned()
68+
})
69+
})
70+
71+
describe('GLOBAL_EXTEND', () => {
72+
// https://github.com/vuejs/vue/blob/dev/test/unit/features/global-api/extend.spec.js
73+
it('should correctly merge options', () => {
74+
toggleDeprecationWarning(true)
75+
76+
const Test = Vue.extend({
77+
name: 'test',
78+
a: 1,
79+
b: 2
80+
})
81+
expect(Test.options.a).toBe(1)
82+
expect(Test.options.b).toBe(2)
83+
expect(Test.super).toBe(Vue)
84+
const t = new Test({
85+
a: 2
86+
})
87+
expect(t.$options.a).toBe(2)
88+
expect(t.$options.b).toBe(2)
89+
// inheritance
90+
const Test2 = Test.extend({
91+
a: 2
92+
})
93+
expect(Test2.options.a).toBe(2)
94+
expect(Test2.options.b).toBe(2)
95+
const t2 = new Test2({
96+
a: 3
97+
})
98+
expect(t2.$options.a).toBe(3)
99+
expect(t2.$options.b).toBe(2)
100+
101+
expect(
102+
deprecationData[DeprecationTypes.GLOBAL_MOUNT].message
103+
).toHaveBeenWarned()
104+
expect(
105+
deprecationData[DeprecationTypes.GLOBAL_EXTEND].message
106+
).toHaveBeenWarned()
107+
})
108+
109+
it('should work when used as components', () => {
110+
const foo = Vue.extend({
111+
template: '<span>foo</span>'
112+
})
113+
const bar = Vue.extend({
114+
template: '<span>bar</span>'
115+
})
116+
const vm = new Vue({
117+
template: '<div><foo></foo><bar></bar></div>',
118+
components: { foo, bar }
119+
}).$mount()
120+
expect(vm.$el.innerHTML).toBe('<span>foo</span><span>bar</span>')
121+
})
122+
123+
it('should merge lifecycle hooks', () => {
124+
const calls: number[] = []
125+
const A = Vue.extend({
126+
created() {
127+
calls.push(1)
128+
}
129+
})
130+
const B = A.extend({
131+
created() {
132+
calls.push(2)
133+
}
134+
})
135+
new B({
136+
created() {
137+
calls.push(3)
138+
}
139+
})
140+
expect(calls).toEqual([1, 2, 3])
141+
})
142+
143+
it('should not merge nested mixins created with Vue.extend', () => {
144+
const A = Vue.extend({
145+
created: () => {}
146+
})
147+
const B = Vue.extend({
148+
mixins: [A],
149+
created: () => {}
150+
})
151+
const C = Vue.extend({
152+
extends: B,
153+
created: () => {}
154+
})
155+
const D = Vue.extend({
156+
mixins: [C],
157+
created: () => {}
158+
})
159+
expect(D.options.created!.length).toBe(4)
160+
})
161+
162+
it('should merge methods', () => {
163+
const A = Vue.extend({
164+
methods: {
165+
a() {
166+
return this.n
167+
}
168+
}
169+
})
170+
const B = A.extend({
171+
methods: {
172+
b() {
173+
return this.n + 1
174+
}
175+
}
176+
})
177+
const b = new B({
178+
data: () => ({ n: 0 }),
179+
methods: {
180+
c() {
181+
return this.n + 2
182+
}
183+
}
184+
}) as any
185+
expect(b.a()).toBe(0)
186+
expect(b.b()).toBe(1)
187+
expect(b.c()).toBe(2)
188+
})
189+
190+
it('should merge assets', () => {
191+
const A = Vue.extend({
192+
components: {
193+
aa: {
194+
template: '<div>A</div>'
195+
}
196+
}
197+
})
198+
const B = A.extend({
199+
components: {
200+
bb: {
201+
template: '<div>B</div>'
202+
}
203+
}
204+
})
205+
const b = new B({
206+
template: '<div><aa></aa><bb></bb></div>'
207+
}).$mount()
208+
expect(b.$el.innerHTML).toBe('<div>A</div><div>B</div>')
209+
})
210+
211+
it('caching', () => {
212+
const options = {
213+
template: '<div></div>'
214+
}
215+
const A = Vue.extend(options)
216+
const B = Vue.extend(options)
217+
expect(A).toBe(B)
218+
})
219+
220+
it('extended options should use different identify from parent', () => {
221+
const A = Vue.extend({ computed: {} })
222+
const B = A.extend()
223+
B.options.computed.b = () => 'foo'
224+
expect(B.options.computed).not.toBe(A.options.computed)
225+
expect(A.options.computed.b).toBeUndefined()
226+
})
227+
})
228+
229+
describe('GLOBAL_PROTOTYPE', () => {
230+
test('plain properties', () => {
231+
toggleDeprecationWarning(true)
232+
Vue.prototype.$test = 1
233+
const vm = new Vue() as any
234+
expect(vm.$test).toBe(1)
235+
delete Vue.prototype.$test
236+
expect(
237+
deprecationData[DeprecationTypes.GLOBAL_MOUNT].message
238+
).toHaveBeenWarned()
239+
expect(
240+
deprecationData[DeprecationTypes.GLOBAL_PROTOTYPE].message
241+
).toHaveBeenWarned()
242+
})
243+
244+
test('method this context', () => {
245+
Vue.prototype.$test = function() {
246+
return this.msg
247+
}
248+
const vm = new Vue({
249+
data() {
250+
return { msg: 'method' }
251+
}
252+
}) as any
253+
expect(vm.$test()).toBe('method')
254+
delete Vue.prototype.$test
255+
})
256+
257+
test('defined properties', () => {
258+
Object.defineProperty(Vue.prototype, '$test', {
259+
configurable: true,
260+
get() {
261+
return this.msg
262+
}
263+
})
264+
const vm = new Vue({
265+
data() {
266+
return { msg: 'getter' }
267+
}
268+
}) as any
269+
expect(vm.$test).toBe('getter')
270+
delete Vue.prototype.$test
271+
})
272+
273+
test('extended prototype', async () => {
274+
const Foo = Vue.extend()
275+
Foo.prototype.$test = 1
276+
const vm = new Foo() as any
277+
expect(vm.$test).toBe(1)
278+
const plain = new Vue() as any
279+
expect(plain.$test).toBeUndefined()
280+
})
281+
})
282+
283+
describe('GLOBAL_SET/DELETE', () => {
284+
test('set', () => {
285+
toggleDeprecationWarning(true)
286+
const obj: any = {}
287+
Vue.set(obj, 'foo', 1)
288+
expect(obj.foo).toBe(1)
289+
expect(
290+
deprecationData[DeprecationTypes.GLOBAL_SET].message
291+
).toHaveBeenWarned()
292+
})
293+
294+
test('delete', () => {
295+
toggleDeprecationWarning(true)
296+
const obj: any = { foo: 1 }
297+
Vue.delete(obj, 'foo')
298+
expect('foo' in obj).toBe(false)
299+
expect(
300+
deprecationData[DeprecationTypes.GLOBAL_DELETE].message
301+
).toHaveBeenWarned()
302+
})
303+
})
304+
305+
describe('GLOBAL_OBSERVABLE', () => {
306+
test('should work', () => {
307+
toggleDeprecationWarning(true)
308+
const obj = Vue.observable({})
309+
expect(isReactive(obj)).toBe(true)
310+
expect(
311+
deprecationData[DeprecationTypes.GLOBAL_OBSERVABLE].message
312+
).toHaveBeenWarned()
313+
})
314+
})
315+
316+
describe('GLOBAL_PRIVATE_UTIL', () => {
317+
test('defineReactive', () => {
318+
toggleDeprecationWarning(true)
319+
const obj: any = {}
320+
// @ts-ignore
321+
Vue.util.defineReactive(obj, 'test', 1)
322+
323+
let n
324+
effect(() => {
325+
n = obj.test
326+
})
327+
expect(n).toBe(1)
328+
obj.test++
329+
expect(n).toBe(2)
330+
331+
expect(
332+
deprecationData[DeprecationTypes.GLOBAL_PRIVATE_UTIL].message
333+
).toHaveBeenWarned()
334+
})
21335
})

0 commit comments

Comments
 (0)