From 41f2b2b18cb0d557bf36a5a28cf769a02dba931f Mon Sep 17 00:00:00 2001 From: Illya Klymov Date: Sat, 29 Aug 2020 13:05:57 +0300 Subject: [PATCH] fix: handle shallowMount on components with v-if and scoped slots (#1663) --- .../create-instance/create-component-stubs.js | 6 +++-- test/specs/shallow-mount.spec.js | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/create-instance/create-component-stubs.js b/packages/create-instance/create-component-stubs.js index 0375f7d8f..2a6f81941 100644 --- a/packages/create-instance/create-component-stubs.js +++ b/packages/create-instance/create-component-stubs.js @@ -95,7 +95,7 @@ function resolveOptions(component, _Vue) { function getScopedSlotRenderFunctions(ctx: any): Array { // In Vue 2.6+ a new v-slot syntax was introduced // scopedSlots are now saved in parent._vnode.data.scopedSlots - // We filter out the _normalized and $stable key + // We filter out _normalized, $stable and $key keys if ( ctx && ctx.$options && @@ -105,7 +105,9 @@ function getScopedSlotRenderFunctions(ctx: any): Array { ctx.$options.parent._vnode.data.scopedSlots ) { const slotKeys: Array = ctx.$options.parent._vnode.data.scopedSlots - return keys(slotKeys).filter(x => x !== '_normalized' && x !== '$stable') + return keys(slotKeys).filter( + x => x !== '_normalized' && x !== '$stable' && x !== '$key' + ) } return [] diff --git a/test/specs/shallow-mount.spec.js b/test/specs/shallow-mount.spec.js index 2357161b4..5251873f0 100644 --- a/test/specs/shallow-mount.spec.js +++ b/test/specs/shallow-mount.spec.js @@ -150,6 +150,30 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => { ) }) + it('renders named slots when they are located inside component with v-if', () => { + const localVue = createLocalVue() + localVue.component('Foo', { + template: '
' + }) + const TestComponent = { + template: ` + + + + ` + } + const wrapper = shallowMount(TestComponent, { + localVue + }) + expect(wrapper.find({ name: 'Foo' }).exists()).toEqual(true) + expect(wrapper.find('.new-example').exists()).toEqual(true) + expect(wrapper.html()).toEqual( + '\n' + '

text

\n' + '
' + ) + }) + it('renders no children if none supplied', () => { const TestComponent = { template: '',