Skip to content

Commit 9bfe35d

Browse files
Djalerlmiller1990
andauthored
fix: stubs components inlined in render function (#2017)
* fix: stubs components inlined in render function * chore: fix linting Co-authored-by: Lachlan Miller <[email protected]>
1 parent 6b9bea4 commit 9bfe35d

File tree

3 files changed

+98
-4
lines changed

3 files changed

+98
-4
lines changed

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

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

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

+86
Original file line numberDiff line numberDiff line change
@@ -704,4 +704,90 @@ describeWithShallowAndMount('options.stub', mountingMethod => {
704704
`</div>`
705705
)
706706
})
707+
708+
it('stubs component inlined in render function with custom stub', () => {
709+
let childComponentCreated = false
710+
let childComponent2Created = false
711+
const ChildComponent = Vue.extend({
712+
name: 'child-component',
713+
template: '<div />',
714+
created() {
715+
childComponentCreated = true
716+
}
717+
})
718+
const ChildComponent2 = {
719+
name: 'child-component-2',
720+
template: '<div />',
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: {
740+
name: 'child-component-2',
741+
template: '<div class="stub-2" />'
742+
}
743+
}
744+
})
745+
746+
expect(childComponentCreated).toBe(false)
747+
expect(childComponent2Created).toBe(false)
748+
749+
expect(wrapper.find('.stub').exists()).toBe(true)
750+
expect(wrapper.find('.stub-2').exists()).toBe(true)
751+
expect(wrapper.find(ChildComponent).exists()).toBe(true)
752+
expect(wrapper.find(ChildComponent2).exists()).toBe(true)
753+
})
754+
755+
it('stubs component inlined in render function with default stub', () => {
756+
let childComponentCreated = false
757+
let childComponent2Created = false
758+
const ChildComponent = Vue.extend({
759+
name: 'child-component',
760+
template: '<div />',
761+
created() {
762+
childComponentCreated = true
763+
}
764+
})
765+
const ChildComponent2 = {
766+
name: 'child-component-2',
767+
template: '<div />',
768+
created() {
769+
childComponent2Created = true
770+
}
771+
}
772+
773+
const ParentComponent = {
774+
name: 'parent-component',
775+
render(h) {
776+
return h('div', [h(ChildComponent), h(ChildComponent2)])
777+
}
778+
}
779+
780+
const wrapper = mountingMethod(ParentComponent, {
781+
stubs: {
782+
ChildComponent: true,
783+
ChildComponent2: true
784+
}
785+
})
786+
787+
expect(childComponentCreated).toBe(false)
788+
expect(childComponent2Created).toBe(false)
789+
790+
expect(wrapper.find(ChildComponent).exists()).toBe(true)
791+
expect(wrapper.find(ChildComponent2).exists()).toBe(true)
792+
})
707793
})

0 commit comments

Comments
 (0)