Skip to content

fix: compile stub components without render functions #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/lib/create-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import extractOptions from '../options/extract-options'
import deleteMountingOptions from '../options/delete-mounting-options'
import createFunctionalComponent from './create-functional-component'
import cloneDeep from 'lodash/cloneDeep'
import { componentNeedsCompiling } from './validators'

export default function createConstructor (
component: Component,
Expand All @@ -39,9 +40,7 @@ export default function createConstructor (
addProvide(component, mountingOptions.provide, options)
}

if (!component.render &&
(component.template || component.extends) &&
!component.functional) {
if (componentNeedsCompiling(component)) {
compileTemplate(component)
}

Expand Down
7 changes: 7 additions & 0 deletions src/lib/stub-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import Vue from 'vue'
import { compileToFunctions } from 'vue-template-compiler'
import { throwError } from './util'
import { componentNeedsCompiling } from './validators'
import { compileTemplate } from './compile-template'

function isVueComponent (comp) {
return comp && (comp.render || comp.template || comp.options)
Expand Down Expand Up @@ -81,6 +83,11 @@ export function createComponentStubs (originalComponents: Object = {}, stubs: Ob
components[stub] = createBlankStub({})
return
}

if (componentNeedsCompiling(stubs[stub])) {
compileTemplate(stubs[stub])
}

if (originalComponents[stub]) {
// Remove cached constructor
delete originalComponents[stub]._Ctor
Expand Down
7 changes: 7 additions & 0 deletions src/lib/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ export function isVueComponent (component: any): boolean {
return typeof component.render === 'function'
}

export function componentNeedsCompiling (component: any) {
return component &&
!component.render &&
(component.template || component.extends) &&
!component.functional
}

export function isValidSelector (selector: any): boolean {
if (isDomSelector(selector)) {
return true
Expand Down
24 changes: 24 additions & 0 deletions test/unit/specs/mount/options/stubs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,30 @@ describe('mount.stub', () => {
expect(wrapper.html()).to.equal('<div><!----></div>')
})

it('handles components without a render function', () => {
const TestComponent = {
template: `
<div>
<stub-component />
</div>
`,
components: {
stubComponent: { template: '<div />' }
}
}
const StubComponent = {
template: '<div>No render function</div>'
}

const wrapper = mount(TestComponent, {
stubs: {
'stub-component': StubComponent
}
})

expect(wrapper.text()).contains('No render function')
})

it('throws an error when passed an invalid value as stub', () => {
const error = '[vue-test-utils]: options.stub values must be passed a string or component'
const invalidValues = [1, null, [], {}, NaN]
Expand Down