Skip to content

Commit

Permalink
fix: stub dynamic components (#1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyerburgh committed Dec 1, 2018
1 parent fb7a66c commit 4338403
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
12 changes: 9 additions & 3 deletions packages/create-instance/patch-render.js
Expand Up @@ -50,6 +50,10 @@ function isConstructor (el) {
return typeof el === 'function'
}

function isComponentOptions (el) {
return typeof el === 'object' && (el.template || el.render)
}

export function patchRender (_Vue, stubs, stubAllComponents) {
// This mixin patches vm.$createElement so that we can stub all components
// before they are rendered in shallow mode. We also need to ensure that
Expand All @@ -60,7 +64,10 @@ export function patchRender (_Vue, stubs, stubAllComponents) {
function patchRenderMixin () {
const vm = this

if (vm.$options.$_doNotStubChildren || vm._isFunctionalContainer) {
if (
vm.$options.$_doNotStubChildren ||
vm.$options._isFunctionalContainer
) {
return
}

Expand All @@ -73,12 +80,11 @@ export function patchRender (_Vue, stubs, stubAllComponents) {
return originalCreateElement(el, ...args)
}

if (isConstructor(el)) {
if (isConstructor(el) || isComponentOptions(el)) {
if (stubAllComponents) {
const stub = createStubFromComponent(el, el.name || 'anonymous')
return originalCreateElement(stub, ...args)
}

const Constructor = shouldExtend(el, _Vue) ? extend(el, _Vue) : el

return originalCreateElement(Constructor, ...args)
Expand Down
2 changes: 1 addition & 1 deletion test/specs/mounting-options/slots.spec.js
Expand Up @@ -56,7 +56,7 @@ describeWithMountingMethods('options.slots', mountingMethod => {
}
})

it('mounts component with default slot if passed object with template prop in slot object', () => {
it('mounts component with default slot if passed compiled options in slot object', () => {
const compiled = compileToFunctions('<div id="div" />')
const wrapper = mountingMethod(ComponentWithSlots, {
slots: { default: [compiled] }
Expand Down
18 changes: 12 additions & 6 deletions test/specs/shallow-mount.spec.js
Expand Up @@ -454,10 +454,10 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
const TestComponent = {
template: `
<div>
<ChildComponent text="normal" />
<component :is="dataComponent" text="data" />
<component :is="computedComponent" text="computed" />
<component :is="methodComponent()" text="method" />
<ChildComponent />
<component :is="dataComponent" />
<component :is="computedComponent" />
<component :is="methodComponent()" />
</div>
`,
components: { ChildComponent },
Expand All @@ -481,7 +481,13 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
}
}
const wrapper = shallowMount(TestComponent)
expect(wrapper.text()).to.equal('')
expect(wrapper.findAll(ChildComponent).length).to.equal(4)
expect(wrapper.html()).to.equal(
'<div>' +
'<childcomponent-stub></childcomponent-stub> ' +
'<anonymous-stub></anonymous-stub> ' +
'<anonymous-stub></anonymous-stub> ' +
'<anonymous-stub></anonymous-stub>' +
'</div>'
)
})
})

0 comments on commit 4338403

Please sign in to comment.