Skip to content

Commit 6576d6e

Browse files
committed
feat(options.stub): add option to pass stubs as an array
1 parent 1a4b19f commit 6576d6e

File tree

2 files changed

+68
-33
lines changed

2 files changed

+68
-33
lines changed

src/lib/stub-components.js

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,41 +61,50 @@ function createBlankStub (originalComponent: Component) {
6161
}
6262

6363
export function stubComponents (component: Component, stubs: Object): void {
64-
Object.keys(stubs).forEach(stub => {
65-
if (!isValidStub(stubs[stub])) {
66-
throwError('options.stub values must be passed a string or component')
67-
}
68-
69-
if (!component.components) {
70-
component.components = {}
71-
}
64+
if (!component.components) {
65+
component.components = {}
66+
}
7267

73-
if (component.components[stub]) {
74-
// Remove cached constructor
75-
delete component.components[stub]._Ctor
76-
if (typeof stubs[stub] === 'string') {
77-
component.components[stub] = createStubFromString(stubs[stub], component.components[stub])
78-
stubLifeCycleEvents(component.components[stub])
79-
} else {
80-
component.components[stub] = {
81-
...stubs[stub],
82-
name: component.components[stub].name
83-
}
68+
if (Array.isArray(stubs)) {
69+
stubs.forEach(stub => {
70+
if (typeof stub !== 'string') {
71+
throwError('each item in options.stub must be a string')
72+
}
73+
component.components[stub] = createBlankStub({})
74+
})
75+
} else {
76+
Object.keys(stubs).forEach(stub => {
77+
if (!isValidStub(stubs[stub])) {
78+
throwError('options.stub values must be passed a string or component')
8479
}
85-
} else {
86-
if (typeof stubs[stub] === 'string') {
87-
component.components[stub] = {
88-
...compileToFunctions(stubs[stub])
80+
81+
if (component.components[stub]) {
82+
// Remove cached constructor
83+
delete component.components[stub]._Ctor
84+
if (typeof stubs[stub] === 'string') {
85+
component.components[stub] = createStubFromString(stubs[stub], component.components[stub])
86+
stubLifeCycleEvents(component.components[stub])
87+
} else {
88+
component.components[stub] = {
89+
...stubs[stub],
90+
name: component.components[stub].name
91+
}
8992
}
90-
stubLifeCycleEvents(component.components[stub])
9193
} else {
92-
component.components[stub] = {
93-
...stubs[stub]
94+
if (typeof stubs[stub] === 'string') {
95+
component.components[stub] = {
96+
...compileToFunctions(stubs[stub])
97+
}
98+
stubLifeCycleEvents(component.components[stub])
99+
} else {
100+
component.components[stub] = {
101+
...stubs[stub]
102+
}
94103
}
95104
}
96-
}
97-
Vue.config.ignoredElements.push(stub)
98-
})
105+
Vue.config.ignoredElements.push(stub)
106+
})
107+
}
99108
}
100109

101110
export function stubAllComponents (component: Component): void {

test/integration/specs/mount/options/stub.spec.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,36 @@ describe('mount.stub', () => {
6262
expect(wrapper.findAll(Component).length).to.equal(1)
6363
})
6464

65-
const invalidValues = [1, null, [], {}, NaN]
66-
invalidValues.forEach(invalidValue => {
67-
it('throws an error when passed an invalid value as stub', () => {
68-
const error = '[vue-test-utils]: options.stub values must be passed a string or component'
65+
it('stubs components with dummy when passed as an array', () => {
66+
const warn = sinon.stub(console, 'error')
67+
const ComponentWithGlobalComponent = {
68+
render: h => h('registered-component')
69+
}
70+
mount(ComponentWithGlobalComponent, {
71+
stub: ['registered-component']
72+
})
73+
74+
expect(warn.called).to.equal(false)
75+
warn.restore()
76+
})
77+
it('stubs components with dummy when passed as an array', () => {
78+
const ComponentWithGlobalComponent = {
79+
render: h => h('registered-component')
80+
}
81+
const invalidValues = [{}, [], 3]
82+
const error = '[vue-test-utils]: each item in options.stub must be a string'
83+
invalidValues.forEach(invalidValue => {
84+
const fn = () => mount(ComponentWithGlobalComponent, {
85+
stub: [invalidValue]
86+
})
87+
expect(fn).to.throw().with.property('message', error)
88+
})
89+
})
90+
91+
it('throws an error when passed an invalid value as stub', () => {
92+
const error = '[vue-test-utils]: options.stub values must be passed a string or component'
93+
const invalidValues = [1, null, [], {}, NaN]
94+
invalidValues.forEach(invalidValue => {
6995
const fn = () => mount(ComponentWithChildComponent, {
7096
stub: {
7197
ChildComponent: invalidValue

0 commit comments

Comments
 (0)