-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathmerge-options.js
51 lines (47 loc) · 1.32 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
// @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')
}
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
}
}