Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle shallowMount on components with v-if and scoped slots #1663

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/create-instance/create-component-stubs.js
Expand Up @@ -95,7 +95,7 @@ function resolveOptions(component, _Vue) {
function getScopedSlotRenderFunctions(ctx: any): Array<string> {
// 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 &&
Expand All @@ -105,7 +105,9 @@ function getScopedSlotRenderFunctions(ctx: any): Array<string> {
ctx.$options.parent._vnode.data.scopedSlots
) {
const slotKeys: Array<string> = 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 []
Expand Down
24 changes: 24 additions & 0 deletions test/specs/shallow-mount.spec.js
Expand Up @@ -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: '<div><slot name="newSyntax" /></div>'
})
const TestComponent = {
template: `
<Foo v-if="true">
<template v-slot:newSyntax>
<p class="new-example">text</p>
</template>
</Foo>
`
}
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(
'<foo-stub>\n' + ' <p class="new-example">text</p>\n' + '</foo-stub>'
)
})

it('renders no children if none supplied', () => {
const TestComponent = {
template: '<child />',
Expand Down