Skip to content

Commit 046b804

Browse files
committed
fix: stubs components inlined in render function
1 parent c6d28ef commit 046b804

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

packages/create-instance/create-component-stubs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ function resolveComponent(obj: Object, component: string): Object {
4343
)
4444
}
4545

46-
function getCoreProperties(componentOptions: Component): Object {
46+
function getCoreProperties(componentOptions: Component, name?: string): Object {
4747
return {
4848
attrs: componentOptions.attrs,
49-
name: componentOptions.name,
49+
name: componentOptions.name || name,
5050
model: componentOptions.model,
5151
props: componentOptions.props,
5252
on: componentOptions.on,
@@ -108,7 +108,7 @@ export function createStubFromComponent(
108108
}
109109

110110
return {
111-
...getCoreProperties(componentOptions),
111+
...getCoreProperties(componentOptions, name),
112112
$_vueTestUtils_original: originalComponent,
113113
$_doNotStubChildren: true,
114114
render(h, context) {

packages/create-instance/patch-create-element.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,16 @@ export function patchCreateElement(_Vue, stubs, stubAllComponents) {
6868
}
6969

7070
if (isConstructor(el) || isComponentOptions(el)) {
71+
const componentOptions = isConstructor(el) ? el.options : el
72+
const elName = componentOptions.name
73+
74+
const stubbedComponent = resolveComponent(elName, stubs)
75+
if (stubbedComponent) {
76+
return originalCreateElement(stubbedComponent, ...args)
77+
}
78+
7179
if (stubAllComponents) {
72-
const stub = createStubFromComponent(el, el.name || 'anonymous', _Vue)
80+
const stub = createStubFromComponent(el, elName || 'anonymous', _Vue)
7381
return originalCreateElement(stub, ...args)
7482
}
7583
const Constructor = shouldExtend(el, _Vue) ? extend(el, _Vue) : el

test/specs/mounting-options/stubs.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,4 +704,47 @@ describeWithShallowAndMount('options.stub', mountingMethod => {
704704
`</div>`
705705
)
706706
})
707+
708+
it('stubs component inlined in render function', () => {
709+
let childComponentCreated = false
710+
let childComponent2Created = false
711+
const ChildComponent = {
712+
name: 'child-component',
713+
template: '<div class="child-component"/>',
714+
created() {
715+
childComponentCreated = true
716+
}
717+
}
718+
const ChildComponent2 = {
719+
name: 'child-component-2',
720+
template: '<div class="child-component-2"/>',
721+
created() {
722+
childComponent2Created = true
723+
}
724+
}
725+
726+
const ParentComponent = {
727+
name: 'parent-component',
728+
render(h) {
729+
return h('div', [h(ChildComponent), h(ChildComponent2)])
730+
}
731+
}
732+
733+
const wrapper = mountingMethod(ParentComponent, {
734+
stubs: {
735+
ChildComponent: {
736+
name: 'child-component',
737+
template: '<div class="stub" />'
738+
},
739+
ChildComponent2: true
740+
}
741+
})
742+
743+
expect(childComponentCreated).toBe(false)
744+
expect(childComponent2Created).toBe(false)
745+
746+
expect(wrapper.find('.stub').exists()).toBe(true)
747+
expect(wrapper.find(ChildComponent).exists()).toBe(true)
748+
expect(wrapper.find(ChildComponent2).exists()).toBe(true)
749+
})
707750
})

0 commit comments

Comments
 (0)