Skip to content

Commit a2b1384

Browse files
committed
refactor: move extractOptions to seperate file
1 parent a7641b4 commit a2b1384

File tree

5 files changed

+85
-28
lines changed

5 files changed

+85
-28
lines changed

flow/options.flow.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ declare type Options = { // eslint-disable-line no-undef
33
mocks?: Object,
44
slots?: Object,
55
localVue?: Component,
6+
provide?: Object,
67
stubs?: Object,
78
context?: Object,
89
clone?: boolean,

src/lib/add-provide.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
function addProvide (component, options) {
2-
const provide = typeof options.provide === 'function'
3-
? options.provide
4-
: Object.assign({}, options.provide)
5-
6-
delete options.provide
1+
function addProvide (component, optionProvide, options) {
2+
const provide = typeof optionProvide === 'function'
3+
? optionProvide
4+
: Object.assign({}, optionProvide)
75

86
options.beforeCreate = function vueTestUtilBeforeCreate () {
97
this._provided = typeof provide === 'function'

src/lib/create-instance.js

+17-22
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,45 @@ import { throwError } from './util'
1010
import cloneDeep from 'lodash/cloneDeep'
1111
import { compileTemplate } from './compile-template'
1212
import createLocalVue from '../create-local-vue'
13-
import config from '../config'
13+
import extractOptions from '../options/extract-options'
1414

1515
export default function createConstructor (
1616
component: Component,
1717
options: Options
1818
): Component {
19-
const vue = options.localVue || createLocalVue()
19+
const mountingOptions = extractOptions(options)
2020

21-
if (options.mocks) {
22-
addMocks(options.mocks, vue)
21+
const vue = mountingOptions.localVue || createLocalVue()
22+
23+
if (mountingOptions.mocks) {
24+
addMocks(mountingOptions.mocks, vue)
2325
}
2426

2527
if (component.functional) {
26-
if (options.context && typeof options.context !== 'object') {
28+
if (mountingOptions.context && typeof mountingOptions.context !== 'object') {
2729
throwError('mount.context must be an object')
2830
}
2931
const clonedComponent = cloneDeep(component)
3032
component = {
3133
render (h) {
3234
return h(
3335
clonedComponent,
34-
options.context || component.FunctionalRenderContext
36+
mountingOptions.context || component.FunctionalRenderContext
3537
)
3638
}
3739
}
38-
} else if (options.context) {
40+
} else if (mountingOptions.context) {
3941
throwError(
4042
'mount.context can only be used when mounting a functional component'
4143
)
4244
}
4345

44-
if (options.provide) {
45-
addProvide(component, options)
46+
if (mountingOptions.provide) {
47+
addProvide(component, mountingOptions.provide, options)
4648
}
4749

48-
if (options.stubs || Object.keys(config.stubs).length > 0) {
49-
if (Array.isArray(options.stubs)) {
50-
stubComponents(component, options.stubs)
51-
} else {
52-
stubComponents(component, {
53-
...config.stubs,
54-
...options.stubs
55-
})
56-
}
50+
if (mountingOptions.stubs) {
51+
stubComponents(component, mountingOptions.stubs)
5752
}
5853

5954
if (!component.render && component.template && !component.functional) {
@@ -64,11 +59,11 @@ export default function createConstructor (
6459

6560
const vm = new Constructor(options)
6661

67-
addAttrs(vm, options.attrs)
68-
addListeners(vm, options.listeners)
62+
addAttrs(vm, mountingOptions.attrs)
63+
addListeners(vm, mountingOptions.listeners)
6964

70-
if (options.slots) {
71-
addSlots(vm, options.slots)
65+
if (mountingOptions.slots) {
66+
addSlots(vm, mountingOptions.slots)
7267
}
7368

7469
return vm

src/options/extract-options.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @flow
2+
import config from '../config'
3+
4+
function getStubs (optionStubs) {
5+
if (optionStubs || Object.keys(config.stubs).length > 0) {
6+
if (Array.isArray(optionStubs)) {
7+
return optionStubs
8+
} else {
9+
return {
10+
...config.stubs,
11+
...optionStubs
12+
}
13+
}
14+
}
15+
}
16+
17+
export default function extractOptions (
18+
options: Options
19+
): Options {
20+
return {
21+
mocks: options.mocks,
22+
context: options.context,
23+
provide: options.provide,
24+
stubs: getStubs(options.stubs),
25+
attrs: options.attrs,
26+
listeners: options.listeners,
27+
slots: options.slots,
28+
localVue: options.localVue
29+
}
30+
}

test/unit/specs/config.spec.js

+33
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe('config', () => {
1313
TransitionGroupStub.name = 'transition-group'
1414
TransitionStub.name = 'transition'
1515
})
16+
1617
it('stubs transition and transition-group by default', () => {
1718
const testComponent = {
1819
template: `
@@ -52,4 +53,36 @@ describe('config', () => {
5253
const wrapper = mount(testComponent)
5354
expect(wrapper.contains(TransitionGroupStub)).to.equal(false)
5455
})
56+
57+
it('doesn\'t stub transition when config.stubs is set to false', () => {
58+
const configStubsSave = config.stubs
59+
config.stubs = false
60+
const testComponent = {
61+
template: `
62+
<div>
63+
<transition-group><p /><p /></transition-group>
64+
</div>
65+
`
66+
}
67+
const wrapper = mount(testComponent)
68+
expect(wrapper.contains(TransitionGroupStub)).to.equal(false)
69+
expect(wrapper.contains(TransitionStub)).to.equal(false)
70+
config.stubs = configStubsSave
71+
})
72+
73+
it('doesn\'t stub transition when config.stubs is set to a string', () => {
74+
const configStubsSave = config.stubs
75+
config.stubs = 'a string'
76+
const testComponent = {
77+
template: `
78+
<div>
79+
<transition-group><p /><p /></transition-group>
80+
</div>
81+
`
82+
}
83+
const wrapper = mount(testComponent)
84+
expect(wrapper.contains(TransitionGroupStub)).to.equal(false)
85+
expect(wrapper.contains(TransitionStub)).to.equal(false)
86+
config.stubs = configStubsSave
87+
})
5588
})

0 commit comments

Comments
 (0)