Skip to content

Commit 41f2b2b

Browse files
authored
fix: handle shallowMount on components with v-if and scoped slots (#1663)
1 parent f78f817 commit 41f2b2b

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

Diff for: packages/create-instance/create-component-stubs.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function resolveOptions(component, _Vue) {
9595
function getScopedSlotRenderFunctions(ctx: any): Array<string> {
9696
// In Vue 2.6+ a new v-slot syntax was introduced
9797
// scopedSlots are now saved in parent._vnode.data.scopedSlots
98-
// We filter out the _normalized and $stable key
98+
// We filter out _normalized, $stable and $key keys
9999
if (
100100
ctx &&
101101
ctx.$options &&
@@ -105,7 +105,9 @@ function getScopedSlotRenderFunctions(ctx: any): Array<string> {
105105
ctx.$options.parent._vnode.data.scopedSlots
106106
) {
107107
const slotKeys: Array<string> = ctx.$options.parent._vnode.data.scopedSlots
108-
return keys(slotKeys).filter(x => x !== '_normalized' && x !== '$stable')
108+
return keys(slotKeys).filter(
109+
x => x !== '_normalized' && x !== '$stable' && x !== '$key'
110+
)
109111
}
110112

111113
return []

Diff for: test/specs/shallow-mount.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,30 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
150150
)
151151
})
152152

153+
it('renders named slots when they are located inside component with v-if', () => {
154+
const localVue = createLocalVue()
155+
localVue.component('Foo', {
156+
template: '<div><slot name="newSyntax" /></div>'
157+
})
158+
const TestComponent = {
159+
template: `
160+
<Foo v-if="true">
161+
<template v-slot:newSyntax>
162+
<p class="new-example">text</p>
163+
</template>
164+
</Foo>
165+
`
166+
}
167+
const wrapper = shallowMount(TestComponent, {
168+
localVue
169+
})
170+
expect(wrapper.find({ name: 'Foo' }).exists()).toEqual(true)
171+
expect(wrapper.find('.new-example').exists()).toEqual(true)
172+
expect(wrapper.html()).toEqual(
173+
'<foo-stub>\n' + ' <p class="new-example">text</p>\n' + '</foo-stub>'
174+
)
175+
})
176+
153177
it('renders no children if none supplied', () => {
154178
const TestComponent = {
155179
template: '<child />',

0 commit comments

Comments
 (0)