Skip to content

Commit fd7e423

Browse files
committed
fix: compile stub components without render functions
1 parent fae3894 commit fd7e423

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

src/lib/create-instance.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import extractOptions from '../options/extract-options'
1414
import deleteMountingOptions from '../options/delete-mounting-options'
1515
import createFunctionalComponent from './create-functional-component'
1616
import cloneDeep from 'lodash/cloneDeep'
17+
import { componentNeedsCompiling } from './validators'
1718

1819
export default function createConstructor (
1920
component: Component,
@@ -39,9 +40,7 @@ export default function createConstructor (
3940
addProvide(component, mountingOptions.provide, options)
4041
}
4142

42-
if (!component.render &&
43-
(component.template || component.extends) &&
44-
!component.functional) {
43+
if (componentNeedsCompiling(component)) {
4544
compileTemplate(component)
4645
}
4746

src/lib/stub-components.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import Vue from 'vue'
44
import { compileToFunctions } from 'vue-template-compiler'
55
import { throwError } from './util'
6+
import { componentNeedsCompiling } from './validators'
7+
import { compileTemplate } from './compile-template'
68

79
function isVueComponent (comp) {
810
return comp && (comp.render || comp.template || comp.options)
@@ -81,6 +83,11 @@ export function createComponentStubs (originalComponents: Object = {}, stubs: Ob
8183
components[stub] = createBlankStub({})
8284
return
8385
}
86+
87+
if (componentNeedsCompiling(stubs[stub])) {
88+
compileTemplate(stubs[stub])
89+
}
90+
8491
if (originalComponents[stub]) {
8592
// Remove cached constructor
8693
delete originalComponents[stub]._Ctor

src/lib/validators.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ export function isVueComponent (component: any): boolean {
4646
return typeof component.render === 'function'
4747
}
4848

49+
export function componentNeedsCompiling (component) {
50+
return component &&
51+
!component.render &&
52+
(component.template || component.extends) &&
53+
!component.functional
54+
}
55+
4956
export function isValidSelector (selector: any): boolean {
5057
if (isDomSelector(selector)) {
5158
return true

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,28 @@ describe('mount.stub', () => {
230230
expect(wrapper.html()).to.equal('<div><!----></div>')
231231
})
232232

233+
it('handles components without a render function', () => {
234+
const TestComponent = {
235+
template: `
236+
<div>
237+
<stub-component />
238+
</div>
239+
`,
240+
components: {
241+
stubComponent: { template: '<div />' }
242+
}
243+
}
244+
const StubComponent = {
245+
template: '<div>No render function</div>'
246+
}
247+
const wrapper = mount(TestComponent, {
248+
stubs: {
249+
StubComponent
250+
}
251+
})
252+
expect(wrapper.text()).contains('No render function')
253+
})
254+
233255
it('throws an error when passed an invalid value as stub', () => {
234256
const error = '[vue-test-utils]: options.stub values must be passed a string or component'
235257
const invalidValues = [1, null, [], {}, NaN]

0 commit comments

Comments
 (0)