Skip to content

Commit

Permalink
fix: stubs components inlined in render function (#2017)
Browse files Browse the repository at this point in the history
* fix: stubs components inlined in render function

* chore: fix linting

Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com>
  • Loading branch information
Djaler and lmiller1990 authored Jan 27, 2023
1 parent 6b9bea4 commit 9bfe35d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/create-instance/create-component-stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ function resolveComponent(obj: Object, component: string): Object {
)
}

function getCoreProperties(componentOptions: Component): Object {
function getCoreProperties(componentOptions: Component, name?: string): Object {
return {
attrs: componentOptions.attrs,
name: componentOptions.name,
name: componentOptions.name || name,
model: componentOptions.model,
props: componentOptions.props,
on: componentOptions.on,
Expand Down Expand Up @@ -108,7 +108,7 @@ export function createStubFromComponent(
}

return {
...getCoreProperties(componentOptions),
...getCoreProperties(componentOptions, name),
$_vueTestUtils_original: originalComponent,
$_doNotStubChildren: true,
render(h, context) {
Expand Down
10 changes: 9 additions & 1 deletion packages/create-instance/patch-create-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ export function patchCreateElement(_Vue, stubs, stubAllComponents) {
}

if (isConstructor(el) || isComponentOptions(el)) {
const componentOptions = isConstructor(el) ? el.options : el
const elName = componentOptions.name

const stubbedComponent = resolveComponent(elName, stubs)
if (stubbedComponent) {
return originalCreateElement(stubbedComponent, ...args)
}

if (stubAllComponents) {
const stub = createStubFromComponent(el, el.name || 'anonymous', _Vue)
const stub = createStubFromComponent(el, elName || 'anonymous', _Vue)
return originalCreateElement(stub, ...args)
}
const Constructor = shouldExtend(el, _Vue) ? extend(el, _Vue) : el
Expand Down
86 changes: 86 additions & 0 deletions test/specs/mounting-options/stubs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,4 +704,90 @@ describeWithShallowAndMount('options.stub', mountingMethod => {
`</div>`
)
})

it('stubs component inlined in render function with custom stub', () => {
let childComponentCreated = false
let childComponent2Created = false
const ChildComponent = Vue.extend({
name: 'child-component',
template: '<div />',
created() {
childComponentCreated = true
}
})
const ChildComponent2 = {
name: 'child-component-2',
template: '<div />',
created() {
childComponent2Created = true
}
}

const ParentComponent = {
name: 'parent-component',
render(h) {
return h('div', [h(ChildComponent), h(ChildComponent2)])
}
}

const wrapper = mountingMethod(ParentComponent, {
stubs: {
ChildComponent: {
name: 'child-component',
template: '<div class="stub" />'
},
ChildComponent2: {
name: 'child-component-2',
template: '<div class="stub-2" />'
}
}
})

expect(childComponentCreated).toBe(false)
expect(childComponent2Created).toBe(false)

expect(wrapper.find('.stub').exists()).toBe(true)
expect(wrapper.find('.stub-2').exists()).toBe(true)
expect(wrapper.find(ChildComponent).exists()).toBe(true)
expect(wrapper.find(ChildComponent2).exists()).toBe(true)
})

it('stubs component inlined in render function with default stub', () => {
let childComponentCreated = false
let childComponent2Created = false
const ChildComponent = Vue.extend({
name: 'child-component',
template: '<div />',
created() {
childComponentCreated = true
}
})
const ChildComponent2 = {
name: 'child-component-2',
template: '<div />',
created() {
childComponent2Created = true
}
}

const ParentComponent = {
name: 'parent-component',
render(h) {
return h('div', [h(ChildComponent), h(ChildComponent2)])
}
}

const wrapper = mountingMethod(ParentComponent, {
stubs: {
ChildComponent: true,
ChildComponent2: true
}
})

expect(childComponentCreated).toBe(false)
expect(childComponent2Created).toBe(false)

expect(wrapper.find(ChildComponent).exists()).toBe(true)
expect(wrapper.find(ChildComponent2).exists()).toBe(true)
})
})

0 comments on commit 9bfe35d

Please sign in to comment.