From c27f01bc744658f2584c954cfc8948b19f972603 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 28 Apr 2021 12:29:51 -0400 Subject: [PATCH] wip: tests for global config compat --- .../__tests__/apiCreateApp.spec.ts | 3 + packages/runtime-core/src/apiCreateApp.ts | 11 +-- .../src/compat/__tests__/global.spec.ts | 1 + .../src/compat/__tests__/globalConfig.spec.ts | 77 +++++++++++++++++++ packages/runtime-core/src/compat/global.ts | 18 ++++- .../runtime-core/src/compat/globalConfig.ts | 23 +----- 6 files changed, 102 insertions(+), 31 deletions(-) create mode 100644 packages/runtime-core/src/compat/__tests__/globalConfig.spec.ts diff --git a/packages/runtime-core/__tests__/apiCreateApp.spec.ts b/packages/runtime-core/__tests__/apiCreateApp.spec.ts index 82fd44d40..bed6c0b11 100644 --- a/packages/runtime-core/__tests__/apiCreateApp.spec.ts +++ b/packages/runtime-core/__tests__/apiCreateApp.spec.ts @@ -481,4 +481,7 @@ describe('api: createApp', () => { app.mount(root) expect(serializeInner(root)).toBe('hello') }) + + // config.compilerOptions is tested in packages/vue since it is only + // supported in the full build. }) diff --git a/packages/runtime-core/src/apiCreateApp.ts b/packages/runtime-core/src/apiCreateApp.ts index a2987e388..e6fa32991 100644 --- a/packages/runtime-core/src/apiCreateApp.ts +++ b/packages/runtime-core/src/apiCreateApp.ts @@ -4,7 +4,7 @@ import { validateComponentName, Component } from './component' -import { ComponentOptions } from './componentOptions' +import { ComponentOptions, RuntimeCompilerOptions } from './componentOptions' import { ComponentPublicInstance } from './componentPublicInstance' import { Directive, validateDirectiveName } from './directives' import { RootRenderFunction } from './renderer' @@ -87,14 +87,9 @@ export interface AppConfig { /** * Options to pass to @vue/compiler-dom. - * *Only supported in runtime compiler build.* + * Only supported in runtime compiler build. */ - compilerOptions: { - isCustomElement: (tag: string) => boolean - whitespace?: 'preserve' | 'condense' - comments?: boolean - delimiters?: [string, string] - } + compilerOptions: RuntimeCompilerOptions } export interface AppContext { diff --git a/packages/runtime-core/src/compat/__tests__/global.spec.ts b/packages/runtime-core/src/compat/__tests__/global.spec.ts index 1ce38e416..cf1bb5143 100644 --- a/packages/runtime-core/src/compat/__tests__/global.spec.ts +++ b/packages/runtime-core/src/compat/__tests__/global.spec.ts @@ -7,6 +7,7 @@ import { } from '../compatConfig' beforeEach(() => { + toggleDeprecationWarning(false) Vue.configureCompat({ MODE: 2 }) }) diff --git a/packages/runtime-core/src/compat/__tests__/globalConfig.spec.ts b/packages/runtime-core/src/compat/__tests__/globalConfig.spec.ts new file mode 100644 index 000000000..d7a7bc262 --- /dev/null +++ b/packages/runtime-core/src/compat/__tests__/globalConfig.spec.ts @@ -0,0 +1,77 @@ +import Vue from '@vue/compat' +import { toggleDeprecationWarning } from '../compatConfig' + +beforeEach(() => { + toggleDeprecationWarning(false) + Vue.configureCompat({ MODE: 2 }) +}) + +afterEach(() => { + Vue.configureCompat({ MODE: 3 }) + toggleDeprecationWarning(false) +}) + +function triggerEvent( + target: Element, + event: string, + process?: (e: any) => any +) { + const e = document.createEvent('HTMLEvents') + e.initEvent(event, true, true) + if (process) process(e) + target.dispatchEvent(e) + return e +} + +// only testing config options that affect runtime behavior. + +test('GLOBAL_KEY_CODES', () => { + Vue.config.keyCodes = { + foo: 86, + bar: [38, 87] + } + + const onFoo = jest.fn() + const onBar = jest.fn() + + const el = document.createElement('div') + new Vue({ + el, + template: ``, + methods: { + onFoo, + onBar + } + }) + + triggerEvent(el.children[0], 'keyup', e => { + e.key = '_' + e.keyCode = 86 + }) + expect(onFoo).toHaveBeenCalledTimes(1) + expect(onBar).toHaveBeenCalledTimes(0) + + triggerEvent(el.children[0], 'keyup', e => { + e.key = '_' + e.keyCode = 38 + }) + expect(onFoo).toHaveBeenCalledTimes(1) + expect(onBar).toHaveBeenCalledTimes(1) + + triggerEvent(el.children[0], 'keyup', e => { + e.key = '_' + e.keyCode = 87 + }) + expect(onFoo).toHaveBeenCalledTimes(1) + expect(onBar).toHaveBeenCalledTimes(2) +}) + +test('GLOBAL_IGNORED_ELEMENTS', () => { + Vue.config.ignoredElements = [/^v-/, 'foo'] + const el = document.createElement('div') + new Vue({ + el, + template: `` + }) + expect(el.innerHTML).toBe(``) +}) diff --git a/packages/runtime-core/src/compat/global.ts b/packages/runtime-core/src/compat/global.ts index b8db41865..5dba5f4b1 100644 --- a/packages/runtime-core/src/compat/global.ts +++ b/packages/runtime-core/src/compat/global.ts @@ -12,7 +12,8 @@ import { NOOP, EMPTY_OBJ, isArray, - isObject + isObject, + isString } from '@vue/shared' import { warn } from '../warning' import { cloneVNode, createVNode } from '../vnode' @@ -152,8 +153,21 @@ export function createCompatVue( ) { continue } + const val = singletonApp.config[key as keyof AppConfig] // @ts-ignore - app.config[key] = singletonApp.config[key] + app.config[key] = val + + // compat for runtime ignoredElements -> isCustomElement + if ( + key === 'ignoredElements' && + isCompatEnabled(DeprecationTypes.CONFIG_IGNORED_ELEMENTS, null) && + !isRuntimeOnly() && + isArray(val) + ) { + app.config.compilerOptions.isCustomElement = tag => { + return val.some(v => (isString(v) ? v === tag : v.test(tag))) + } + } } isCopyingConfig = false diff --git a/packages/runtime-core/src/compat/globalConfig.ts b/packages/runtime-core/src/compat/globalConfig.ts index ae6664f23..cd5b4194c 100644 --- a/packages/runtime-core/src/compat/globalConfig.ts +++ b/packages/runtime-core/src/compat/globalConfig.ts @@ -1,12 +1,7 @@ -import { extend, isArray, isString } from '@vue/shared' +import { extend, isArray } from '@vue/shared' import { AppConfig } from '../apiCreateApp' -import { isRuntimeOnly } from '../component' import { mergeDataOption } from './data' -import { - DeprecationTypes, - warnDeprecation, - isCompatEnabled -} from './compatConfig' +import { DeprecationTypes, warnDeprecation } from './compatConfig' import { isCopyingConfig } from './global' // legacy config warnings @@ -59,20 +54,6 @@ export function installLegacyConfigProperties(config: AppConfig) { warnDeprecation(legacyConfigOptions[key], null) } val = newVal - - // compat for runtime ignoredElements -> isCustomElement - if ( - key === 'ignoredElements' && - isCompatEnabled(DeprecationTypes.CONFIG_IGNORED_ELEMENTS, null) && - !isRuntimeOnly() && - isArray(newVal) - ) { - config.isCustomElement = tag => { - return newVal.some( - val => (isString(val) ? val === tag : val.test(tag)) - ) - } - } } }) })