Skip to content

Commit 80303bc

Browse files
committed
wip: compat test coverage
1 parent 324a00c commit 80303bc

File tree

4 files changed

+93
-9
lines changed

4 files changed

+93
-9
lines changed

packages/runtime-core/src/compat/renderFn.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export function compatH(
113113
): VNode
114114
export function compatH(
115115
type: string | Component,
116-
props?: LegacyVNodeProps,
116+
props?: Data & LegacyVNodeProps,
117117
children?: LegacyVNodeChildren
118118
): VNode
119119

@@ -190,16 +190,12 @@ function convertLegacyProps(
190190
} else if (key === 'on' || key === 'nativeOn') {
191191
const listeners = legacyProps[key]
192192
for (const event in listeners) {
193-
const handlerKey = convertLegacyEventKey(event)
193+
let handlerKey = convertLegacyEventKey(event)
194+
if (key === 'nativeOn') handlerKey += `Native`
194195
const existing = converted[handlerKey]
195196
const incoming = listeners[event]
196197
if (existing !== incoming) {
197198
if (existing) {
198-
// for the rare case where the same handler is attached
199-
// twice with/without .native modifier...
200-
if (key === 'nativeOn' && String(existing) === String(incoming)) {
201-
continue
202-
}
203199
converted[handlerKey] = [].concat(existing as any, incoming as any)
204200
} else {
205201
converted[handlerKey] = incoming
@@ -307,6 +303,7 @@ function convertLegacySlots(vnode: VNode): VNode {
307303
}
308304

309305
export function defineLegacyVNodeProperties(vnode: VNode) {
306+
/* istanbul ignore if */
310307
if (
311308
isCompatEnabled(
312309
DeprecationTypes.RENDER_FUNCTION,

packages/vue-compat/__tests__/componentAsync.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,24 @@ describe('COMPONENT_ASYNC', () => {
5959
)
6060
).toHaveBeenWarned()
6161
})
62+
63+
test('object syntax', async () => {
64+
const comp = () => ({
65+
component: Promise.resolve({ template: 'foo' })
66+
})
67+
68+
const vm = new Vue({
69+
template: `<div><comp/></div>`,
70+
components: { comp }
71+
}).$mount()
72+
expect(vm.$el.innerHTML).toBe(`<!---->`)
73+
await timeout(0)
74+
expect(vm.$el.innerHTML).toBe(`foo`)
75+
76+
expect(
77+
(deprecationData[DeprecationTypes.COMPONENT_ASYNC].message as Function)(
78+
comp
79+
)
80+
).toHaveBeenWarned()
81+
})
6282
})

packages/vue-compat/__tests__/global.spec.ts

+47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Vue from '@vue/compat'
22
import { effect, isReactive } from '@vue/reactivity'
3+
import { nextTick } from '@vue/runtime-core'
34
import {
45
DeprecationTypes,
56
deprecationData,
@@ -333,4 +334,50 @@ describe('GLOBAL_PRIVATE_UTIL', () => {
333334
deprecationData[DeprecationTypes.GLOBAL_PRIVATE_UTIL].message
334335
).toHaveBeenWarned()
335336
})
337+
338+
test('defineReactive on instance', async () => {
339+
const vm = new Vue({
340+
beforeCreate() {
341+
// @ts-ignore
342+
Vue.util.defineReactive(this, 'foo', 1)
343+
},
344+
template: `<div>{{ foo }}</div>`
345+
}).$mount() as any
346+
expect(vm.$el.textContent).toBe('1')
347+
vm.foo = 2
348+
await nextTick()
349+
expect(vm.$el.textContent).toBe('2')
350+
})
351+
352+
test('defineReactive with object value', () => {
353+
const obj: any = {}
354+
const val = { a: 1 }
355+
// @ts-ignore
356+
Vue.util.defineReactive(obj, 'foo', val)
357+
358+
let n
359+
effect(() => {
360+
n = obj.foo.a
361+
})
362+
expect(n).toBe(1)
363+
// mutating original
364+
val.a++
365+
expect(n).toBe(2)
366+
})
367+
368+
test('defineReactive with array value', () => {
369+
const obj: any = {}
370+
const val = [1]
371+
// @ts-ignore
372+
Vue.util.defineReactive(obj, 'foo', val)
373+
374+
let n
375+
effect(() => {
376+
n = obj.foo.length
377+
})
378+
expect(n).toBe(1)
379+
// mutating original
380+
val.push(2)
381+
expect(n).toBe(2)
382+
})
336383
})

packages/vue-compat/__tests__/renderFn.spec.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,29 @@ describe('compat: render function', () => {
123123
})
124124
).toMatchObject({
125125
props: {
126-
onClick: fn, // should dedupe
126+
onClick: fn,
127+
onClickNative: fn,
127128
onFooBar: fn,
128-
'onBar-baz': fn
129+
'onBar-bazNative': fn
130+
}
131+
})
132+
})
133+
134+
test('v2 legacy event prefixes', () => {
135+
const fn = () => {}
136+
expect(
137+
h('div', {
138+
on: {
139+
'&click': fn,
140+
'~keyup': fn,
141+
'!touchend': fn
142+
}
143+
})
144+
).toMatchObject({
145+
props: {
146+
onClickPassive: fn,
147+
onKeyupOnce: fn,
148+
onTouchendCapture: fn
129149
}
130150
})
131151
})

0 commit comments

Comments
 (0)