Skip to content

Commit 9fb79e1

Browse files
committed
fix(types): fix the type of stubs
1 parent 4c9fe3c commit 9fb79e1

File tree

9 files changed

+106
-73
lines changed

9 files changed

+106
-73
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

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ declare type Options = {
33
attachToDocument?: boolean,
44
propsData?: Object,
55
mocks?: Object,
6-
methods?: Object,
6+
methods?: { [key: string]: Function },
77
slots?: SlotsObject,
88
scopedSlots?: Object,
99
localVue?: Component,
1010
provide?: Object,
11-
stubs?: Object,
11+
stubs?: Stubs,
1212
context?: Object,
1313
attrs?: Object,
1414
listeners?: Object,
@@ -19,3 +19,7 @@ declare type Options = {
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
@@ -107,8 +111,8 @@ function createBlankStub (originalComponent: Component, name: string) {
107111

108112
export function createComponentStubs (
109113
originalComponents: Object = {},
110-
stubs: Object
111-
): Object {
114+
stubs: Stubs
115+
): Components {
112116
const components = {}
113117
if (!stubs) {
114118
return components
@@ -127,55 +131,61 @@ export function createComponentStubs (
127131
components[stub] = createBlankStub(component, stub)
128132
})
129133
} else {
130-
Object.keys(stubs).forEach(stub => {
131-
if (stubs[stub] === false) {
134+
const stubsObject = (stubs: { [name: string]: Component | string | true })
135+
Object.keys(stubsObject).forEach(stubName => {
136+
const stub = stubsObject[stubName]
137+
if (stub === false) {
132138
return
133139
}
134-
if (!isValidStub(stubs[stub])) {
140+
141+
if (!isValidStub(stub)) {
135142
throwError(
136143
`options.stub values must be passed a string or ` + `component`
137144
)
138145
}
139-
if (stubs[stub] === true) {
140-
const component = resolveComponent(originalComponents, stub)
141-
components[stub] = createBlankStub(component, stub)
146+
147+
if (stub === true) {
148+
const component = resolveComponent(originalComponents, stubName)
149+
components[stubName] = createBlankStub(component, stubName)
142150
return
143151
}
144152

145-
if (componentNeedsCompiling(stubs[stub])) {
146-
compileTemplate(stubs[stub])
153+
if (typeof stub !== 'string' && componentNeedsCompiling(stub)) {
154+
compileTemplate(stub)
147155
}
148156

149-
if (originalComponents[stub]) {
157+
if (originalComponents[stubName]) {
150158
// Remove cached constructor
151-
delete originalComponents[stub]._Ctor
152-
if (typeof stubs[stub] === 'string') {
153-
components[stub] = createStubFromString(
154-
stubs[stub],
155-
originalComponents[stub],
156-
stub
159+
delete originalComponents[stubName]._Ctor
160+
if (typeof stub === 'string') {
161+
components[stubName] = createStubFromString(
162+
stub,
163+
originalComponents[stubName],
164+
stubName
157165
)
158166
} else {
159-
components[stub] = {
160-
...stubs[stub],
161-
name: originalComponents[stub].name
167+
const stubObject = (stub: Object)
168+
components[stubName] = {
169+
...stubObject,
170+
name: originalComponents[stubName].name
162171
}
163172
}
164173
} else {
165-
if (typeof stubs[stub] === 'string') {
174+
if (typeof stub === 'string') {
166175
if (!compileToFunctions) {
167176
throwError(
168177
`vueTemplateCompiler is undefined, you must pass ` +
169178
`precompiled components if vue-template-compiler is ` +
170179
`undefined`
171180
)
172181
}
173-
components[stub] = {
174-
...compileToFunctions(stubs[stub])
182+
components[stubName] = {
183+
...compileToFunctions(stub)
175184
}
176185
} else {
177-
components[stub] = {
178-
...stubs[stub]
186+
const stubObject = (stub: Object)
187+
components[stubName] = {
188+
...stubObject
179189
}
180190
}
181191
}
@@ -184,7 +194,10 @@ export function createComponentStubs (
184194
return components
185195
}
186196

187-
function stubComponents (components: Object, stubbedComponents: Object) {
197+
function stubComponents (
198+
components: Components,
199+
stubbedComponents: Components
200+
): void {
188201
Object.keys(components).forEach(component => {
189202
const cmp = components[component]
190203
const componentOptions = typeof cmp === 'function'
@@ -199,7 +212,7 @@ function stubComponents (components: Object, stubbedComponents: Object) {
199212
})
200213
}
201214

202-
export function createComponentStubsForAll (component: Component): Object {
215+
export function createComponentStubsForAll (component: Component): Components {
203216
const stubbedComponents = {}
204217

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

228-
export function createComponentStubsForGlobals (instance: Component): Object {
241+
export function createComponentStubsForGlobals (
242+
instance: Component
243+
): Components {
229244
const components = {}
230245
Object.keys(instance.options.components).forEach(c => {
231246
if (isRequiredComponent(c)) {

0 commit comments

Comments
 (0)