Skip to content

Commit c27f01b

Browse files
committed
wip: tests for global config compat
1 parent ce0bbe0 commit c27f01b

File tree

6 files changed

+102
-31
lines changed

6 files changed

+102
-31
lines changed

packages/runtime-core/__tests__/apiCreateApp.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,7 @@ describe('api: createApp', () => {
481481
app.mount(root)
482482
expect(serializeInner(root)).toBe('hello')
483483
})
484+
485+
// config.compilerOptions is tested in packages/vue since it is only
486+
// supported in the full build.
484487
})

packages/runtime-core/src/apiCreateApp.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
validateComponentName,
55
Component
66
} from './component'
7-
import { ComponentOptions } from './componentOptions'
7+
import { ComponentOptions, RuntimeCompilerOptions } from './componentOptions'
88
import { ComponentPublicInstance } from './componentPublicInstance'
99
import { Directive, validateDirectiveName } from './directives'
1010
import { RootRenderFunction } from './renderer'
@@ -87,14 +87,9 @@ export interface AppConfig {
8787

8888
/**
8989
* Options to pass to @vue/compiler-dom.
90-
* *Only supported in runtime compiler build.*
90+
* Only supported in runtime compiler build.
9191
*/
92-
compilerOptions: {
93-
isCustomElement: (tag: string) => boolean
94-
whitespace?: 'preserve' | 'condense'
95-
comments?: boolean
96-
delimiters?: [string, string]
97-
}
92+
compilerOptions: RuntimeCompilerOptions
9893
}
9994

10095
export interface AppContext {

packages/runtime-core/src/compat/__tests__/global.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from '../compatConfig'
88

99
beforeEach(() => {
10+
toggleDeprecationWarning(false)
1011
Vue.configureCompat({ MODE: 2 })
1112
})
1213

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import Vue from '@vue/compat'
2+
import { toggleDeprecationWarning } from '../compatConfig'
3+
4+
beforeEach(() => {
5+
toggleDeprecationWarning(false)
6+
Vue.configureCompat({ MODE: 2 })
7+
})
8+
9+
afterEach(() => {
10+
Vue.configureCompat({ MODE: 3 })
11+
toggleDeprecationWarning(false)
12+
})
13+
14+
function triggerEvent(
15+
target: Element,
16+
event: string,
17+
process?: (e: any) => any
18+
) {
19+
const e = document.createEvent('HTMLEvents')
20+
e.initEvent(event, true, true)
21+
if (process) process(e)
22+
target.dispatchEvent(e)
23+
return e
24+
}
25+
26+
// only testing config options that affect runtime behavior.
27+
28+
test('GLOBAL_KEY_CODES', () => {
29+
Vue.config.keyCodes = {
30+
foo: 86,
31+
bar: [38, 87]
32+
}
33+
34+
const onFoo = jest.fn()
35+
const onBar = jest.fn()
36+
37+
const el = document.createElement('div')
38+
new Vue({
39+
el,
40+
template: `<input type="text" @keyup.foo="onFoo" @keyup.bar="onBar">`,
41+
methods: {
42+
onFoo,
43+
onBar
44+
}
45+
})
46+
47+
triggerEvent(el.children[0], 'keyup', e => {
48+
e.key = '_'
49+
e.keyCode = 86
50+
})
51+
expect(onFoo).toHaveBeenCalledTimes(1)
52+
expect(onBar).toHaveBeenCalledTimes(0)
53+
54+
triggerEvent(el.children[0], 'keyup', e => {
55+
e.key = '_'
56+
e.keyCode = 38
57+
})
58+
expect(onFoo).toHaveBeenCalledTimes(1)
59+
expect(onBar).toHaveBeenCalledTimes(1)
60+
61+
triggerEvent(el.children[0], 'keyup', e => {
62+
e.key = '_'
63+
e.keyCode = 87
64+
})
65+
expect(onFoo).toHaveBeenCalledTimes(1)
66+
expect(onBar).toHaveBeenCalledTimes(2)
67+
})
68+
69+
test('GLOBAL_IGNORED_ELEMENTS', () => {
70+
Vue.config.ignoredElements = [/^v-/, 'foo']
71+
const el = document.createElement('div')
72+
new Vue({
73+
el,
74+
template: `<v-foo/><foo/>`
75+
})
76+
expect(el.innerHTML).toBe(`<v-foo></v-foo><foo></foo>`)
77+
})

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
NOOP,
1313
EMPTY_OBJ,
1414
isArray,
15-
isObject
15+
isObject,
16+
isString
1617
} from '@vue/shared'
1718
import { warn } from '../warning'
1819
import { cloneVNode, createVNode } from '../vnode'
@@ -152,8 +153,21 @@ export function createCompatVue(
152153
) {
153154
continue
154155
}
156+
const val = singletonApp.config[key as keyof AppConfig]
155157
// @ts-ignore
156-
app.config[key] = singletonApp.config[key]
158+
app.config[key] = val
159+
160+
// compat for runtime ignoredElements -> isCustomElement
161+
if (
162+
key === 'ignoredElements' &&
163+
isCompatEnabled(DeprecationTypes.CONFIG_IGNORED_ELEMENTS, null) &&
164+
!isRuntimeOnly() &&
165+
isArray(val)
166+
) {
167+
app.config.compilerOptions.isCustomElement = tag => {
168+
return val.some(v => (isString(v) ? v === tag : v.test(tag)))
169+
}
170+
}
157171
}
158172
isCopyingConfig = false
159173

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import { extend, isArray, isString } from '@vue/shared'
1+
import { extend, isArray } from '@vue/shared'
22
import { AppConfig } from '../apiCreateApp'
3-
import { isRuntimeOnly } from '../component'
43
import { mergeDataOption } from './data'
5-
import {
6-
DeprecationTypes,
7-
warnDeprecation,
8-
isCompatEnabled
9-
} from './compatConfig'
4+
import { DeprecationTypes, warnDeprecation } from './compatConfig'
105
import { isCopyingConfig } from './global'
116

127
// legacy config warnings
@@ -59,20 +54,6 @@ export function installLegacyConfigProperties(config: AppConfig) {
5954
warnDeprecation(legacyConfigOptions[key], null)
6055
}
6156
val = newVal
62-
63-
// compat for runtime ignoredElements -> isCustomElement
64-
if (
65-
key === 'ignoredElements' &&
66-
isCompatEnabled(DeprecationTypes.CONFIG_IGNORED_ELEMENTS, null) &&
67-
!isRuntimeOnly() &&
68-
isArray(newVal)
69-
) {
70-
config.isCustomElement = tag => {
71-
return newVal.some(
72-
val => (isString(val) ? val === tag : val.test(tag))
73-
)
74-
}
75-
}
7657
}
7758
})
7859
})

0 commit comments

Comments
 (0)