Skip to content

Commit eb7e854

Browse files
38elementseddyerburgh
authored andcommitted
refactor(types): improve types (#783)
1 parent 221038b commit eb7e854

File tree

9 files changed

+109
-76
lines changed

9 files changed

+109
-76
lines changed

Diff for: docs/api/config.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Vue Test Utils includes a config object to defined options used by Vue Test Util
66

77
### `stubs`
88

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

4747
### `methods`
4848

49-
- type: `Object`
49+
- type: `{ [name: string]: Function }`
5050
- default: `{}`
5151

5252
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.

Diff for: flow/config.flow.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare type Config = {
2+
stubs?: { [name: string]: Component | boolean | string },
3+
mocks?: Object,
4+
methods?: { [name: string]: Function },
5+
provide?: Object,
6+
logModifiedComponents?: boolean,
7+
silent?: boolean
8+
};

Diff for: flow/options.flow.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@ declare type Options = {
33
attachToDocument?: boolean,
44
propsData?: Object,
55
mocks?: Object,
6-
methods?: Object,
6+
methods?: { [key: string]: Function },
77
slots?: SlotsObject,
8-
scopedSlots?: Object,
8+
scopedSlots?: { [key: string]: string },
99
localVue?: Component,
1010
provide?: Object,
11-
stubs?: Object,
11+
stubs?: Stubs,
1212
context?: Object,
13-
attrs?: Object,
14-
listeners?: Object,
13+
attrs?: { [key: string]: string },
14+
listeners?: { [key: string]: Function | Array<Function> },
1515
logModifiedComponents?: boolean,
1616
sync?: boolean
1717
};
1818

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

2121
declare type SlotsObject = { [name: string]: SlotValue };
22+
23+
declare type Stubs = {
24+
[name: string]: Component | true | string
25+
} | Array<string>

Diff for: flow/wrapper.flow.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type Wrapper from '~src/Wrapper'
44
import type WrapperArray from '~src/WrapperArray'
55

66
declare type Selector = any;
7+
declare type Components = { [name: string]: Component };
78

89
declare interface BaseWrapper {
910
// eslint-disable-line no-undef

Diff for: packages/shared/merge-options.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
// @flow
22

3-
function getOptions (key, options, config) {
4-
if (options || (config[key] && Object.keys(config[key]).length > 0)) {
5-
if (options instanceof Function) {
6-
return options
7-
} else if (Array.isArray(options)) {
8-
return [...options, ...Object.keys(config[key] || {})]
9-
} else if (!(config[key] instanceof Function)) {
3+
function getOption (option, config?: Object): any {
4+
if (option || (config && Object.keys(config).length > 0)) {
5+
if (option instanceof Function) {
6+
return option
7+
} else if (Array.isArray(option)) {
8+
return [...option, ...Object.keys(config || {})]
9+
} else if (config instanceof Function) {
10+
throw new Error(`Config can't be a Function.`)
11+
} else {
1012
return {
11-
...config[key],
12-
...options
13+
...config,
14+
...option
1315
}
14-
} else {
15-
throw new Error(`Config can't be a Function.`)
1616
}
1717
}
1818
}
1919

20-
export function mergeOptions (options: Options, config: Options): Options {
20+
export function mergeOptions (options: Options, config: Config): Options {
21+
const mocks = (getOption(options.mocks, config.mocks): Object)
22+
const methods = (
23+
(getOption(options.methods, config.methods)): { [key: string]: Function })
24+
const provide = ((getOption(options.provide, config.provide)): Object)
2125
return {
2226
...options,
2327
logModifiedComponents: config.logModifiedComponents,
24-
stubs: getOptions('stubs', options.stubs, config),
25-
mocks: getOptions('mocks', options.mocks, config),
26-
methods: getOptions('methods', options.methods, config),
27-
provide: getOptions('provide', options.provide, config),
28+
stubs: getOption(options.stubs, config.stubs),
29+
mocks,
30+
methods,
31+
provide,
2832
sync: !!(options.sync || options.sync === undefined)
2933
}
3034
}

Diff for: packages/shared/stub-components.js

+49-34
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ import {
1414
} from './validators'
1515
import { compileTemplate } from './compile-template'
1616

17-
function isVueComponent (comp) {
17+
function isVueComponent (comp): boolean {
1818
return comp && (comp.render || comp.template || comp.options)
1919
}
2020

21-
function isValidStub (stub: any) {
21+
function isValidStub (stub: any): boolean {
2222
return (
2323
(!!stub && typeof stub === 'string') ||
2424
stub === true ||
2525
isVueComponent(stub)
2626
)
2727
}
2828

29-
function resolveComponent (obj, component) {
29+
function resolveComponent (obj: Object, component: string): Object {
3030
return obj[component] ||
3131
obj[hyphenate(component)] ||
3232
obj[camelize(component)] ||
@@ -35,7 +35,7 @@ function resolveComponent (obj, component) {
3535
{}
3636
}
3737

38-
function isRequiredComponent (name) {
38+
function isRequiredComponent (name): boolean {
3939
return (
4040
name === 'KeepAlive' || name === 'Transition' || name === 'TransitionGroup'
4141
)
@@ -59,11 +59,12 @@ function getCoreProperties (componentOptions: Component): Object {
5959
functional: componentOptions.functional
6060
}
6161
}
62+
6263
function createStubFromString (
6364
templateString: string,
6465
originalComponent: Component,
6566
name: string
66-
): Object {
67+
): Component {
6768
if (!compileToFunctions) {
6869
throwError(
6970
`vueTemplateCompiler is undefined, you must pass ` +
@@ -86,7 +87,10 @@ function createStubFromString (
8687
}
8788
}
8889

89-
function createBlankStub (originalComponent: Component, name: string) {
90+
function createBlankStub (
91+
originalComponent: Component,
92+
name: string
93+
): Component {
9094
const componentOptions = typeof originalComponent === 'function'
9195
? originalComponent.extendOptions
9296
: originalComponent
@@ -110,8 +114,8 @@ function createBlankStub (originalComponent: Component, name: string) {
110114

111115
export function createComponentStubs (
112116
originalComponents: Object = {},
113-
stubs: Object
114-
): Object {
117+
stubs: Stubs
118+
): Components {
115119
const components = {}
116120
if (!stubs) {
117121
return components
@@ -130,55 +134,61 @@ export function createComponentStubs (
130134
components[stub] = createBlankStub(component, stub)
131135
})
132136
} else {
133-
Object.keys(stubs).forEach(stub => {
134-
if (stubs[stub] === false) {
137+
const stubsObject = (stubs: { [name: string]: Component | string | true })
138+
Object.keys(stubsObject).forEach(stubName => {
139+
const stub = stubsObject[stubName]
140+
if (stub === false) {
135141
return
136142
}
137-
if (!isValidStub(stubs[stub])) {
143+
144+
if (!isValidStub(stub)) {
138145
throwError(
139146
`options.stub values must be passed a string or ` + `component`
140147
)
141148
}
142-
if (stubs[stub] === true) {
143-
const component = resolveComponent(originalComponents, stub)
144-
components[stub] = createBlankStub(component, stub)
149+
150+
if (stub === true) {
151+
const component = resolveComponent(originalComponents, stubName)
152+
components[stubName] = createBlankStub(component, stubName)
145153
return
146154
}
147155

148-
if (componentNeedsCompiling(stubs[stub])) {
149-
compileTemplate(stubs[stub])
156+
if (typeof stub !== 'string' && componentNeedsCompiling(stub)) {
157+
compileTemplate(stub)
150158
}
151159

152-
if (originalComponents[stub]) {
160+
if (originalComponents[stubName]) {
153161
// Remove cached constructor
154-
delete originalComponents[stub]._Ctor
155-
if (typeof stubs[stub] === 'string') {
156-
components[stub] = createStubFromString(
157-
stubs[stub],
158-
originalComponents[stub],
159-
stub
162+
delete originalComponents[stubName]._Ctor
163+
if (typeof stub === 'string') {
164+
components[stubName] = createStubFromString(
165+
stub,
166+
originalComponents[stubName],
167+
stubName
160168
)
161169
} else {
162-
components[stub] = {
163-
...stubs[stub],
164-
name: originalComponents[stub].name
170+
const stubObject = (stub: Object)
171+
components[stubName] = {
172+
...stubObject,
173+
name: originalComponents[stubName].name
165174
}
166175
}
167176
} else {
168-
if (typeof stubs[stub] === 'string') {
177+
if (typeof stub === 'string') {
169178
if (!compileToFunctions) {
170179
throwError(
171180
`vueTemplateCompiler is undefined, you must pass ` +
172181
`precompiled components if vue-template-compiler is ` +
173182
`undefined`
174183
)
175184
}
176-
components[stub] = {
177-
...compileToFunctions(stubs[stub])
185+
components[stubName] = {
186+
...compileToFunctions(stub)
178187
}
179188
} else {
180-
components[stub] = {
181-
...stubs[stub]
189+
const stubObject = (stub: Object)
190+
components[stubName] = {
191+
...stubObject
182192
}
183193
}
184194
}
@@ -187,7 +197,10 @@ export function createComponentStubs (
187197
return components
188198
}
189199

190-
function stubComponents (components: Object, stubbedComponents: Object) {
200+
function stubComponents (
201+
components: Components,
202+
stubbedComponents: Components
203+
): void {
191204
Object.keys(components).forEach(component => {
192205
const cmp = components[component]
193206
const componentOptions = typeof cmp === 'function'
@@ -202,7 +215,7 @@ function stubComponents (components: Object, stubbedComponents: Object) {
202215
})
203216
}
204217

205-
export function createComponentStubsForAll (component: Component): Object {
218+
export function createComponentStubsForAll (component: Component): Components {
206219
const stubbedComponents = {}
207220

208221
if (component.components) {
@@ -228,7 +241,9 @@ export function createComponentStubsForAll (component: Component): Object {
228241
return stubbedComponents
229242
}
230243

231-
export function createComponentStubsForGlobals (instance: Component): Object {
244+
export function createComponentStubsForGlobals (
245+
instance: Component
246+
): Components {
232247
const components = {}
233248
Object.keys(instance.options.components).forEach(c => {
234249
if (isRequiredComponent(c)) {

0 commit comments

Comments
 (0)