Skip to content

fix(types): improve types #783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Vue Test Utils includes a config object to defined options used by Vue Test Util

### `stubs`

- type: `Object`
- type: `{ [name: string]: Component | boolean | string }`
- default: `{
transition: TransitionStub,
'transition-group': TransitionGroupStub
Expand Down Expand Up @@ -46,7 +46,7 @@ VueTestUtils.config.mocks['$store'] = {

### `methods`

- type: `Object`
- type: `{ [name: string]: Function }`
- default: `{}`

You can configure default methods using the `config` object. This can be useful for plugins that inject methods to components, like [VeeValidate](https://vee-validate.logaretm.com/). You can override methods set in `config` by passing `methods` in the mounting options.
Expand Down
8 changes: 8 additions & 0 deletions flow/config.flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare type Config = {
stubs?: { [name: string]: Component | boolean | string },
mocks?: Object,
methods?: { [name: string]: Function },
provide?: Object,
logModifiedComponents?: boolean,
silent?: boolean
};
14 changes: 9 additions & 5 deletions flow/options.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ declare type Options = {
attachToDocument?: boolean,
propsData?: Object,
mocks?: Object,
methods?: Object,
methods?: { [key: string]: Function },
slots?: SlotsObject,
scopedSlots?: Object,
scopedSlots?: { [key: string]: string },
localVue?: Component,
provide?: Object,
stubs?: Object,
stubs?: Stubs,
context?: Object,
attrs?: Object,
listeners?: Object,
attrs?: { [key: string]: string },
listeners?: { [key: string]: Function | Array<Function> },
logModifiedComponents?: boolean,
sync?: boolean
};

declare type SlotValue = Component | string | Array<Component | string>;

declare type SlotsObject = { [name: string]: SlotValue };

declare type Stubs = {
[name: string]: Component | true | string
} | Array<string>
1 change: 1 addition & 0 deletions flow/wrapper.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type Wrapper from '~src/Wrapper'
import type WrapperArray from '~src/WrapperArray'

declare type Selector = any;
declare type Components = { [name: string]: Component };

declare interface BaseWrapper {
// eslint-disable-line no-undef
Expand Down
36 changes: 20 additions & 16 deletions packages/shared/merge-options.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
// @flow

function getOptions (key, options, config) {
if (options || (config[key] && Object.keys(config[key]).length > 0)) {
if (options instanceof Function) {
return options
} else if (Array.isArray(options)) {
return [...options, ...Object.keys(config[key] || {})]
} else if (!(config[key] instanceof Function)) {
function getOption (option, config?: Object): any {
if (option || (config && Object.keys(config).length > 0)) {
if (option instanceof Function) {
return option
} else if (Array.isArray(option)) {
return [...option, ...Object.keys(config || {})]
} else if (config instanceof Function) {
throw new Error(`Config can't be a Function.`)
} else {
return {
...config[key],
...options
...config,
...option
}
} else {
throw new Error(`Config can't be a Function.`)
}
}
}

export function mergeOptions (options: Options, config: Options): Options {
export function mergeOptions (options: Options, config: Config): Options {
const mocks = (getOption(options.mocks, config.mocks): Object)
const methods = (
(getOption(options.methods, config.methods)): { [key: string]: Function })
const provide = ((getOption(options.provide, config.provide)): Object)
return {
...options,
logModifiedComponents: config.logModifiedComponents,
stubs: getOptions('stubs', options.stubs, config),
mocks: getOptions('mocks', options.mocks, config),
methods: getOptions('methods', options.methods, config),
provide: getOptions('provide', options.provide, config),
stubs: getOption(options.stubs, config.stubs),
mocks,
methods,
provide,
sync: !!(options.sync || options.sync === undefined)
}
}
83 changes: 49 additions & 34 deletions packages/shared/stub-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ import {
} from './validators'
import { compileTemplate } from './compile-template'

function isVueComponent (comp) {
function isVueComponent (comp): boolean {
return comp && (comp.render || comp.template || comp.options)
}

function isValidStub (stub: any) {
function isValidStub (stub: any): boolean {
return (
(!!stub && typeof stub === 'string') ||
stub === true ||
isVueComponent(stub)
)
}

function resolveComponent (obj, component) {
function resolveComponent (obj: Object, component: string): Object {
return obj[component] ||
obj[hyphenate(component)] ||
obj[camelize(component)] ||
Expand All @@ -35,7 +35,7 @@ function resolveComponent (obj, component) {
{}
}

function isRequiredComponent (name) {
function isRequiredComponent (name): boolean {
return (
name === 'KeepAlive' || name === 'Transition' || name === 'TransitionGroup'
)
Expand All @@ -59,11 +59,12 @@ function getCoreProperties (componentOptions: Component): Object {
functional: componentOptions.functional
}
}

function createStubFromString (
templateString: string,
originalComponent: Component,
name: string
): Object {
): Component {
if (!compileToFunctions) {
throwError(
`vueTemplateCompiler is undefined, you must pass ` +
Expand All @@ -86,7 +87,10 @@ function createStubFromString (
}
}

function createBlankStub (originalComponent: Component, name: string) {
function createBlankStub (
originalComponent: Component,
name: string
): Component {
const componentOptions = typeof originalComponent === 'function'
? originalComponent.extendOptions
: originalComponent
Expand All @@ -107,8 +111,8 @@ function createBlankStub (originalComponent: Component, name: string) {

export function createComponentStubs (
originalComponents: Object = {},
stubs: Object
): Object {
stubs: Stubs
): Components {
const components = {}
if (!stubs) {
return components
Expand All @@ -127,55 +131,61 @@ export function createComponentStubs (
components[stub] = createBlankStub(component, stub)
})
} else {
Object.keys(stubs).forEach(stub => {
if (stubs[stub] === false) {
const stubsObject = (stubs: { [name: string]: Component | string | true })
Object.keys(stubsObject).forEach(stubName => {
const stub = stubsObject[stubName]
if (stub === false) {
return
}
if (!isValidStub(stubs[stub])) {

if (!isValidStub(stub)) {
throwError(
`options.stub values must be passed a string or ` + `component`
)
}
if (stubs[stub] === true) {
const component = resolveComponent(originalComponents, stub)
components[stub] = createBlankStub(component, stub)

if (stub === true) {
const component = resolveComponent(originalComponents, stubName)
components[stubName] = createBlankStub(component, stubName)
return
}

if (componentNeedsCompiling(stubs[stub])) {
compileTemplate(stubs[stub])
if (typeof stub !== 'string' && componentNeedsCompiling(stub)) {
compileTemplate(stub)
}

if (originalComponents[stub]) {
if (originalComponents[stubName]) {
// Remove cached constructor
delete originalComponents[stub]._Ctor
if (typeof stubs[stub] === 'string') {
components[stub] = createStubFromString(
stubs[stub],
originalComponents[stub],
stub
delete originalComponents[stubName]._Ctor
if (typeof stub === 'string') {
components[stubName] = createStubFromString(
stub,
originalComponents[stubName],
stubName
)
} else {
components[stub] = {
...stubs[stub],
name: originalComponents[stub].name
const stubObject = (stub: Object)
components[stubName] = {
...stubObject,
name: originalComponents[stubName].name
}
}
} else {
if (typeof stubs[stub] === 'string') {
if (typeof stub === 'string') {
if (!compileToFunctions) {
throwError(
`vueTemplateCompiler is undefined, you must pass ` +
`precompiled components if vue-template-compiler is ` +
`undefined`
)
}
components[stub] = {
...compileToFunctions(stubs[stub])
components[stubName] = {
...compileToFunctions(stub)
}
} else {
components[stub] = {
...stubs[stub]
const stubObject = (stub: Object)
components[stubName] = {
...stubObject
}
}
}
Expand All @@ -184,7 +194,10 @@ export function createComponentStubs (
return components
}

function stubComponents (components: Object, stubbedComponents: Object) {
function stubComponents (
components: Components,
stubbedComponents: Components
): void {
Object.keys(components).forEach(component => {
const cmp = components[component]
const componentOptions = typeof cmp === 'function'
Expand All @@ -199,7 +212,7 @@ function stubComponents (components: Object, stubbedComponents: Object) {
})
}

export function createComponentStubsForAll (component: Component): Object {
export function createComponentStubsForAll (component: Component): Components {
const stubbedComponents = {}

if (component.components) {
Expand All @@ -225,7 +238,9 @@ export function createComponentStubsForAll (component: Component): Object {
return stubbedComponents
}

export function createComponentStubsForGlobals (instance: Component): Object {
export function createComponentStubsForGlobals (
instance: Component
): Components {
const components = {}
Object.keys(instance.options.components).forEach(c => {
if (isRequiredComponent(c)) {
Expand Down
Loading