/
merge-options.js
54 lines (50 loc) · 1.58 KB
/
merge-options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// @flow
import { normalizeStubs, normalizeProvide } from './normalize'
import { warnDeprecated } from 'shared/util'
function getOption(option, config?: Object): any {
if (option === false) {
return false
}
if (option || (config && Object.keys(config).length > 0)) {
if (option instanceof Function) {
return option
}
if (config instanceof Function) {
throw new Error(`Config can't be a Function.`)
}
return {
...config,
...option
}
}
}
function getStubs(stubs, configStubs): Object {
const normalizedStubs = normalizeStubs(stubs)
const normalizedConfigStubs = normalizeStubs(configStubs)
return getOption(normalizedStubs, normalizedConfigStubs)
}
export function mergeOptions(
options: Options,
config: Config
): NormalizedOptions {
const mocks = (getOption(options.mocks, config.mocks): Object)
const methods = (getOption(options.methods, config.methods): {
[key: string]: Function
})
if (methods && Object.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. If you need to stub out a method, extract it out and test it. Otherwise, the suggestion is to rethink those tests'
)
}
const provide = (getOption(options.provide, config.provide): Object)
const stubs = (getStubs(options.stubs, config.stubs): Object)
// $FlowIgnore
return {
...options,
provide: normalizeProvide(provide),
stubs,
mocks,
methods
}
}