]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: tests for global config compat
authorEvan You <yyx990803@gmail.com>
Wed, 28 Apr 2021 16:29:51 +0000 (12:29 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 28 Apr 2021 16:29:51 +0000 (12:29 -0400)
packages/runtime-core/__tests__/apiCreateApp.spec.ts
packages/runtime-core/src/apiCreateApp.ts
packages/runtime-core/src/compat/__tests__/global.spec.ts
packages/runtime-core/src/compat/__tests__/globalConfig.spec.ts [new file with mode: 0644]
packages/runtime-core/src/compat/global.ts
packages/runtime-core/src/compat/globalConfig.ts

index 82fd44d40a0bfb20f22d0e4426363de08c939393..bed6c0b11fc09a7b1a3826c809df3bf84c9e6871 100644 (file)
@@ -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.
 })
index a2987e388ebd3abad3cad7e870fd4e581d11befd..e6fa3299165ac9bd41b7ce371f9e17ce2aa560b2 100644 (file)
@@ -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 {
index 1ce38e4166d3a0fbb3970f4d68a197d3112b431a..cf1bb5143f6aa6d6ba6b589efb8137425e59a715 100644 (file)
@@ -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 (file)
index 0000000..d7a7bc2
--- /dev/null
@@ -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: `<input type="text" @keyup.foo="onFoo" @keyup.bar="onBar">`,
+    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: `<v-foo/><foo/>`
+  })
+  expect(el.innerHTML).toBe(`<v-foo></v-foo><foo></foo>`)
+})
index b8db418655532961ed935a645973a5993e8b9f24..5dba5f4b1cfc4192113bea2faeaf973ef97e6f89 100644 (file)
@@ -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
 
index ae6664f23a6cb415a8c91748b462450c76201e5e..cd5b4194cc7ac450ac38ee44cca7aed8bb1f16c7 100644 (file)
@@ -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))
-            )
-          }
-        }
       }
     })
   })