From f2c39a0ee23a2bb8077373b2c01fc92af5bcd420 Mon Sep 17 00:00:00 2001 From: Kirill Romanov Date: Sun, 16 Oct 2022 13:26:23 +0300 Subject: [PATCH 1/2] fix: stubs components inlined in render function --- .../create-instance/create-component-stubs.js | 6 +- .../create-instance/patch-create-element.js | 10 ++- test/specs/mounting-options/stubs.spec.js | 86 +++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) diff --git a/packages/create-instance/create-component-stubs.js b/packages/create-instance/create-component-stubs.js index 7185ab648..0b8f8d094 100644 --- a/packages/create-instance/create-component-stubs.js +++ b/packages/create-instance/create-component-stubs.js @@ -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, @@ -108,7 +108,7 @@ export function createStubFromComponent( } return { - ...getCoreProperties(componentOptions), + ...getCoreProperties(componentOptions, name), $_vueTestUtils_original: originalComponent, $_doNotStubChildren: true, render(h, context) { diff --git a/packages/create-instance/patch-create-element.js b/packages/create-instance/patch-create-element.js index 02b7d1256..daab487f9 100644 --- a/packages/create-instance/patch-create-element.js +++ b/packages/create-instance/patch-create-element.js @@ -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 diff --git a/test/specs/mounting-options/stubs.spec.js b/test/specs/mounting-options/stubs.spec.js index fa926c278..1fa368b9b 100644 --- a/test/specs/mounting-options/stubs.spec.js +++ b/test/specs/mounting-options/stubs.spec.js @@ -704,4 +704,90 @@ describeWithShallowAndMount('options.stub', mountingMethod => { `` ) }) + + it('stubs component inlined in render function with custom stub', () => { + let childComponentCreated = false + let childComponent2Created = false + const ChildComponent = Vue.extend({ + name: 'child-component', + template: '
', + created() { + childComponentCreated = true + } + }) + const ChildComponent2 = { + name: 'child-component-2', + template: '
', + 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: '
' + }, + ChildComponent2: { + name: 'child-component-2', + template: '
' + } + } + }) + + 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: '
', + created() { + childComponentCreated = true + } + }) + const ChildComponent2 = { + name: 'child-component-2', + template: '
', + 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) + }) }) From 8ece5091e908ad7595858f5dad4b1c23cd050b1b Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Fri, 27 Jan 2023 11:45:59 +1000 Subject: [PATCH 2/2] chore: fix linting --- docs/.vuepress/theme/Layout.vue | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/docs/.vuepress/theme/Layout.vue b/docs/.vuepress/theme/Layout.vue index 5e1811726..dc1a028a7 100644 --- a/docs/.vuepress/theme/Layout.vue +++ b/docs/.vuepress/theme/Layout.vue @@ -9,10 +9,7 @@

To read docs for Vue Test Utils for Vue 3, - . + .