Skip to content

Commit f224048

Browse files
authored
fix: accept all valid components in stubs option (#389)
1 parent 1690ed2 commit f224048

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/lib/stub-components.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import Vue from 'vue'
44
import { compileToFunctions } from 'vue-template-compiler'
55
import { throwError } from './util'
66

7+
function isVueComponent (comp) {
8+
return comp && (comp.render || comp.template || comp.options)
9+
}
10+
711
function isValidStub (stub: any) {
812
return !!stub &&
9-
(typeof stub === 'string' ||
13+
typeof stub === 'string' ||
1014
(stub === true) ||
11-
(typeof stub === 'object' &&
12-
typeof stub.render === 'function'))
15+
(isVueComponent(stub))
1316
}
1417

1518
function isRequiredComponent (name) {

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

+16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { mount, config } from '~vue-test-utils'
22
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'
5+
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
56
import { createLocalVue } from '~vue-test-utils'
7+
import Vue from 'vue'
68

79
describe('mount.stub', () => {
810
let info
@@ -22,6 +24,20 @@ describe('mount.stub', () => {
2224
config.stubs = configStubsSave
2325
})
2426

27+
it('accepts valid component stubs', () => {
28+
const ComponentWithRender = { render: h => h('div') }
29+
const ComponentWithoutRender = { template: '<div></div>' }
30+
const ExtendedComponent = Vue.extend({ template: '<div></div>' })
31+
mount(ComponentWithChild, {
32+
stubs: {
33+
ChildComponent: ComponentAsAClass,
34+
ChildComponent2: ComponentWithRender,
35+
ChildComponent3: ComponentWithoutRender,
36+
ChildComponent4: ExtendedComponent
37+
}
38+
})
39+
})
40+
2541
it('replaces component with template string ', () => {
2642
const wrapper = mount(ComponentWithChild, {
2743
stubs: {

0 commit comments

Comments
 (0)