Skip to content

Commit 7962b88

Browse files
committed
fix: add config stubs to array when stubs is an array
1 parent 5fc1c57 commit 7962b88

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

src/options/extract-options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import config from '../config'
44
function getStubs (optionStubs) {
55
if (optionStubs || Object.keys(config.stubs).length > 0) {
66
if (Array.isArray(optionStubs)) {
7-
return optionStubs
7+
return [...optionStubs, ...Object.keys(config.stubs)]
88
} else {
99
return {
1010
...config.stubs,

test/unit/specs/mount/options/context.spec.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ describe('context', () => {
6767
})
6868

6969
it('mounts functional component with a defined context.children', () => {
70-
const Component = {
71-
functional: true,
72-
render: (h, {children}) => {
73-
return h('div', children)
74-
}
75-
}
76-
const wrapper = mount(Component, {
77-
context: {
78-
children: ['hello']
70+
const Component = {
71+
functional: true,
72+
render: (h, { children }) => {
73+
return h('div', children)
74+
}
7975
}
76+
const wrapper = mount(Component, {
77+
context: {
78+
children: ['hello']
79+
}
80+
})
81+
expect(wrapper.text()).to.equal('hello')
8082
})
81-
expect(wrapper.text()).to.equal('hello')
82-
})
8383
})

test/unit/specs/mount/options/stubs.spec.js

+72
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ComponentWithChild from '~resources/components/component-with-child.vue'
33
import ComponentWithNestedChildren from '~resources/components/component-with-nested-children.vue'
44
import Component from '~resources/components/component.vue'
55
import config from '~src/config'
6+
import createLocalVue from '~src/create-local-vue'
67

78
describe('mount.stub', () => {
89
let info
@@ -134,6 +135,7 @@ describe('mount.stub', () => {
134135
}
135136
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = compilerSave
136137
})
138+
137139
it('does not stub component when set to false', () => {
138140
const wrapper = mount(ComponentWithChild, {
139141
stubs: {
@@ -142,6 +144,76 @@ describe('mount.stub', () => {
142144
expect(wrapper.find('span').contains('div')).to.equal(true)
143145
})
144146

147+
it('combines with stubs from config', () => {
148+
const localVue = createLocalVue()
149+
config.stubs['time-component'] = '<p />'
150+
const SpanComponent = {
151+
render: h => h('span')
152+
}
153+
const TimeComponent = {
154+
render: h => h('time')
155+
}
156+
localVue.component('span-component', SpanComponent)
157+
localVue.component('time-component', TimeComponent)
158+
const TestComponent = {
159+
render: h => h('div', [
160+
h('span-component'),
161+
h('time-component')
162+
])
163+
}
164+
165+
const wrapper = mount(TestComponent, {
166+
stubs: {
167+
'span-component': '<p />'
168+
},
169+
localVue
170+
})
171+
expect(wrapper.findAll('p').length).to.equal(2)
172+
})
173+
174+
it('prioritize mounting options over config', () => {
175+
const localVue = createLocalVue()
176+
config.stubs['time-component'] = '<p />'
177+
const TimeComponent = {
178+
render: h => h('time')
179+
}
180+
localVue.component('time-component', TimeComponent)
181+
const TestComponent = {
182+
render: h => h('div', [
183+
h('time-component')
184+
])
185+
}
186+
187+
const wrapper = mount(TestComponent, {
188+
stubs: {
189+
'time-component': '<span />'
190+
},
191+
localVue
192+
})
193+
expect(wrapper.contains('span')).to.equal(true)
194+
})
195+
196+
it('converts config to array if stubs is an array', () => {
197+
const localVue = createLocalVue()
198+
config.stubs['time-component'] = '<p />'
199+
const TimeComponent = {
200+
render: h => h('time')
201+
}
202+
localVue.component('time-component', TimeComponent)
203+
const TestComponent = {
204+
render: h => h('div', [
205+
h('time-component')
206+
])
207+
}
208+
209+
const wrapper = mount(TestComponent, {
210+
stubs: ['a-component'],
211+
localVue
212+
})
213+
expect(wrapper.contains('time')).to.equal(false)
214+
expect(wrapper.contains('p')).to.equal(false)
215+
})
216+
145217
it('throws an error when passed an invalid value as stub', () => {
146218
const error = '[vue-test-utils]: options.stub values must be passed a string or component'
147219
const invalidValues = [1, null, [], {}, NaN]

0 commit comments

Comments
 (0)