Skip to content

Commit b792034

Browse files
authored
fix: compile stub components without render functions (#390)
* fix: compile stub components without render functions * test: fix flow error * test: fix test for Vue 2.0.x
1 parent fae3894 commit b792034

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/lib/create-instance.js

+2-3
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

+7
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

+7
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: any) {
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

+24
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,30 @@ 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+
248+
const wrapper = mount(TestComponent, {
249+
stubs: {
250+
'stub-component': StubComponent
251+
}
252+
})
253+
254+
expect(wrapper.text()).contains('No render function')
255+
})
256+
233257
it('throws an error when passed an invalid value as stub', () => {
234258
const error = '[vue-test-utils]: options.stub values must be passed a string or component'
235259
const invalidValues = [1, null, [], {}, NaN]

0 commit comments

Comments
 (0)