From 07e6a83f9722082f3499a02c1c612a50581bdb8b Mon Sep 17 00:00:00 2001 From: Simon He <13917107469@163.com> Date: Fri, 27 Jan 2023 13:15:38 +0800 Subject: [PATCH] refactor: tidy up --- packages/create-instance/add-mocks.js | 4 +-- .../create-instance/create-component-stubs.js | 2 +- packages/create-instance/create-instance.js | 5 ++-- packages/shared/compile-template.js | 6 ++-- packages/shared/create-local-vue.js | 3 +- packages/shared/merge-options.js | 6 ++-- packages/shared/validate-slots.js | 4 +-- packages/shared/validators.js | 4 +-- packages/test-utils/src/create-dom-event.js | 3 +- packages/test-utils/src/matches.js | 4 +-- .../test-utils/src/recursively-set-data.js | 5 ++-- packages/test-utils/src/wrapper.js | 28 +++++++++---------- 12 files changed, 38 insertions(+), 36 deletions(-) diff --git a/packages/create-instance/add-mocks.js b/packages/create-instance/add-mocks.js index 3675e4459..268d221fe 100644 --- a/packages/create-instance/add-mocks.js +++ b/packages/create-instance/add-mocks.js @@ -1,6 +1,6 @@ // @flow import $$Vue from 'vue' -import { warn } from 'shared/util' +import { warn, keys } from 'shared/util' export default function addMocks( _Vue: Component, @@ -9,7 +9,7 @@ export default function addMocks( if (mockedProperties === false) { return } - Object.keys(mockedProperties).forEach(key => { + keys(mockedProperties).forEach(key => { try { // $FlowIgnore _Vue.prototype[key] = mockedProperties[key] diff --git a/packages/create-instance/create-component-stubs.js b/packages/create-instance/create-component-stubs.js index 0b8f8d094..aa29e0012 100644 --- a/packages/create-instance/create-component-stubs.js +++ b/packages/create-instance/create-component-stubs.js @@ -195,7 +195,7 @@ export function createStubsFromStubsObject( stubs: Object, _Vue: Component ): Components { - return Object.keys(stubs || {}).reduce((acc, stubName) => { + return keys(stubs || {}).reduce((acc, stubName) => { let stub = stubs[stubName] validateStub(stub) diff --git a/packages/create-instance/create-instance.js b/packages/create-instance/create-instance.js index 3c0ba7d2e..654dfaa54 100644 --- a/packages/create-instance/create-instance.js +++ b/packages/create-instance/create-instance.js @@ -10,6 +10,7 @@ import { componentNeedsCompiling, isConstructor } from 'shared/validators' import createScopedSlots from './create-scoped-slots' import { createStubsFromStubsObject } from './create-component-stubs' import { patchCreateElement } from './patch-create-element' +import { keys } from 'shared/util' function createContext(options, scopedSlots, currentProps) { const on = { @@ -86,8 +87,8 @@ export default function createInstance( // watchers provided in mounting options should override preexisting ones if (componentOptions.watch && instanceOptions.watch) { - const componentWatchers = Object.keys(componentOptions.watch) - const instanceWatchers = Object.keys(instanceOptions.watch) + const componentWatchers = keys(componentOptions.watch) + const instanceWatchers = keys(instanceOptions.watch) for (let i = 0; i < instanceWatchers.length; i++) { const k = instanceWatchers[i] diff --git a/packages/shared/compile-template.js b/packages/shared/compile-template.js index 49bbdeb91..0d8936767 100644 --- a/packages/shared/compile-template.js +++ b/packages/shared/compile-template.js @@ -2,7 +2,7 @@ import { compileToFunctions } from 'vue-template-compiler' import { componentNeedsCompiling } from './validators' -import { throwError } from './util' +import { throwError, keys } from './util' export function compileTemplate(component: Component): void { if (component.template) { @@ -31,7 +31,7 @@ export function compileTemplate(component: Component): void { } if (component.components) { - Object.keys(component.components).forEach(c => { + keys(component.components).forEach(c => { const cmp = component.components[c] if (!cmp.render) { compileTemplate(cmp) @@ -49,7 +49,7 @@ export function compileTemplate(component: Component): void { } export function compileTemplateForSlots(slots: Object): void { - Object.keys(slots).forEach(key => { + keys(slots).forEach(key => { const slot = Array.isArray(slots[key]) ? slots[key] : [slots[key]] slot.forEach(slotValue => { if (componentNeedsCompiling(slotValue)) { diff --git a/packages/shared/create-local-vue.js b/packages/shared/create-local-vue.js index 6f64d7029..4eabf9811 100644 --- a/packages/shared/create-local-vue.js +++ b/packages/shared/create-local-vue.js @@ -2,6 +2,7 @@ import Vue from 'vue' import cloneDeep from 'lodash/cloneDeep' +import { keys } from './util' /** * Used internally by vue-server-test-utils and test-utils to propagate/create vue instances. @@ -17,7 +18,7 @@ function _createLocalVue( const instance = _Vue.extend() // clone global APIs - Object.keys(_Vue).forEach(key => { + keys(_Vue).forEach(key => { if (!instance.hasOwnProperty(key)) { const original = _Vue[key] // cloneDeep can fail when cloning Vue instances diff --git a/packages/shared/merge-options.js b/packages/shared/merge-options.js index bf8d97418..f70421103 100644 --- a/packages/shared/merge-options.js +++ b/packages/shared/merge-options.js @@ -1,12 +1,12 @@ // @flow import { normalizeStubs, normalizeProvide } from './normalize' -import { warnDeprecated } from 'shared/util' +import { warnDeprecated, keys } from 'shared/util' function getOption(option, config?: Object): any { if (option === false) { return false } - if (option || (config && Object.keys(config).length > 0)) { + if (option || (config && keys(config).length > 0)) { if (option instanceof Function) { return option } @@ -34,7 +34,7 @@ export function mergeOptions( const methods = (getOption(options.methods, config.methods): { [key: string]: Function }) - if (methods && Object.keys(methods).length) { + if (methods && keys(methods).length) { warnDeprecated( 'overwriting methods via the `methods` property', 'There is no clear migration path for the `methods` property - Vue does not support arbitrarily replacement of methods, nor should VTU. To stub a complex method extract it from the component and test it in isolation. Otherwise, the suggestion is to rethink those tests' diff --git a/packages/shared/validate-slots.js b/packages/shared/validate-slots.js index 9806b098d..71df38fd8 100644 --- a/packages/shared/validate-slots.js +++ b/packages/shared/validate-slots.js @@ -1,6 +1,6 @@ // @flow -import { throwError } from 'shared/util' +import { throwError, keys } from 'shared/util' import { compileToFunctions } from 'vue-template-compiler' import { isVueComponent } from './validators' @@ -19,7 +19,7 @@ function requiresTemplateCompiler(slot: any): void { } export function validateSlots(slots: SlotsObject): void { - Object.keys(slots).forEach(key => { + keys(slots).forEach(key => { const slot = Array.isArray(slots[key]) ? slots[key] : [slots[key]] slot.forEach(slotValue => { diff --git a/packages/shared/validators.js b/packages/shared/validators.js index 986049d33..4a2d892c8 100644 --- a/packages/shared/validators.js +++ b/packages/shared/validators.js @@ -1,5 +1,5 @@ // @flow -import { throwError, capitalize, camelize, hyphenate } from './util' +import { throwError, capitalize, camelize, hyphenate, keys } from './util' export function isDomSelector(selector: any): boolean { if (typeof selector !== 'string') { @@ -64,7 +64,7 @@ export function componentNeedsCompiling(component: Component): boolean { export function isRefSelector(refOptionsObject: any): boolean { if ( !isPlainObject(refOptionsObject) || - Object.keys(refOptionsObject || {}).length !== 1 + keys(refOptionsObject || {}).length !== 1 ) { return false } diff --git a/packages/test-utils/src/create-dom-event.js b/packages/test-utils/src/create-dom-event.js index 47632ff1e..0933f3f8a 100644 --- a/packages/test-utils/src/create-dom-event.js +++ b/packages/test-utils/src/create-dom-event.js @@ -1,4 +1,5 @@ import eventTypes from 'dom-event-types' +import { keys } from 'shared/util' const defaultEventType = { eventInterface: 'Event', @@ -108,7 +109,7 @@ export default function createDOMEvent(type, options) { : createOldEvent(eventParams) const eventPrototype = Object.getPrototypeOf(event) - Object.keys(options || {}).forEach(key => { + keys(options || {}).forEach(key => { const propertyDescriptor = Object.getOwnPropertyDescriptor( eventPrototype, key diff --git a/packages/test-utils/src/matches.js b/packages/test-utils/src/matches.js index 6acba7661..7a51885f4 100644 --- a/packages/test-utils/src/matches.js +++ b/packages/test-utils/src/matches.js @@ -4,7 +4,7 @@ import { FUNCTIONAL_OPTIONS } from 'shared/consts' import { isConstructor, isFunctionalComponent } from 'shared/validators' -import { capitalize, camelize } from 'shared/util' +import { capitalize, camelize, keys } from 'shared/util' function vmMatchesName(vm, name) { // We want to mirror how Vue resolves component names in SFCs: @@ -48,7 +48,7 @@ function vmCtorMatches(vm, component) { } if (component.functional) { - return Object.keys(vm._Ctor || {}).some(c => { + return keys(vm._Ctor || {}).some(c => { return component === vm._Ctor[c].extendOptions }) } diff --git a/packages/test-utils/src/recursively-set-data.js b/packages/test-utils/src/recursively-set-data.js index 99cf741e2..48e3dc66a 100644 --- a/packages/test-utils/src/recursively-set-data.js +++ b/packages/test-utils/src/recursively-set-data.js @@ -1,14 +1,15 @@ import { isPlainObject } from 'shared/validators' +import { keys } from 'shared/util' export function recursivelySetData(vm, target, data) { - Object.keys(data).forEach(key => { + keys(data).forEach(key => { const val = data[key] const targetVal = target[key] if ( isPlainObject(val) && isPlainObject(targetVal) && - Object.keys(val).length > 0 + keys(val).length > 0 ) { recursivelySetData(vm, targetVal, val) } else { diff --git a/packages/test-utils/src/wrapper.js b/packages/test-utils/src/wrapper.js index bbaae815a..b1dc5ee3e 100644 --- a/packages/test-utils/src/wrapper.js +++ b/packages/test-utils/src/wrapper.js @@ -17,7 +17,8 @@ import { nextTick, warn, warnDeprecated, - isVueWrapper + isVueWrapper, + keys } from 'shared/util' import { isPlainObject } from 'shared/validators' import { isElementVisible } from 'shared/is-visible' @@ -124,17 +125,14 @@ export default class Wrapper implements BaseWrapper { let classes = classAttribute ? classAttribute.split(' ') : [] // Handle converting cssmodules identifiers back to the original class name if (this.vm && this.vm.$style) { - const cssModuleIdentifiers = Object.keys(this.vm.$style).reduce( - (acc, key) => { - // $FlowIgnore - const moduleIdent = this.vm.$style[key] - if (moduleIdent) { - acc[moduleIdent.split(' ')[0]] = key - } - return acc - }, - {} - ) + const cssModuleIdentifiers = keys(this.vm.$style).reduce((acc, key) => { + // $FlowIgnore + const moduleIdent = this.vm.$style[key] + if (moduleIdent) { + acc[moduleIdent.split(' ')[0]] = key + } + return acc + }, {}) classes = classes.map(name => cssModuleIdentifiers[name] || name) } @@ -480,7 +478,7 @@ export default class Wrapper implements BaseWrapper { const computed = this.vm._computedWatchers ? formatJSON( // $FlowIgnore - ...Object.keys(this.vm._computedWatchers).map(computedKey => ({ + ...keys(this.vm._computedWatchers).map(computedKey => ({ // $FlowIgnore [computedKey]: this.vm[computedKey] })) @@ -680,7 +678,7 @@ export default class Wrapper implements BaseWrapper { } this.__warnIfDestroyed() - Object.keys(methods).forEach(key => { + keys(methods).forEach(key => { // $FlowIgnore : Problem with possibly null this.vm this.vm[key] = methods[key] // $FlowIgnore : Problem with possibly null this.vm @@ -717,7 +715,7 @@ export default class Wrapper implements BaseWrapper { this.__warnIfDestroyed() - Object.keys(data).forEach(key => { + keys(data).forEach(key => { // Don't let people set entire objects, because reactivity won't work if ( isPlainObject(data[key]) &&