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: renderStubDefaultSlot with scoped slots #2397

Merged
merged 1 commit into from
Apr 5, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/vnodeTransformers/stubComponentsTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ export const createStub = ({
// Also having function text as attribute is useless and annoying so
// we replace it with "[Function]""
const stubProps = normalizeStubProps(props)

return h(tag, stubProps, renderStubDefaultSlot ? slots : undefined)
// if renderStubDefaultSlot is true, we render the default slot
if (renderStubDefaultSlot && slots.default) {
// we explicitly call the default slot with an empty object
// so scope slots destructuring works
return h(tag, stubProps, slots.default({}))
}
return h(tag, stubProps)
}
}
})
Expand Down
30 changes: 30 additions & 0 deletions tests/mountingOptions/global.components.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('global.components', () => {
spy.mockRestore()
expect(wrapper.text()).toBe('Global')
})

it('render children with shallow and renderStubDefaultSlot', () => {
const Child = defineComponent({
template: '<div><p>child</p><slot /></div>'
Expand All @@ -97,4 +98,33 @@ describe('global.components', () => {
'</div>'
)
})

// https://github.com/vuejs/test-utils/issues/2395
it('render children with shallow and renderStubDefaultSlot with v-slot', () => {
const Child = defineComponent({
template: '<div><p>child</p><slot /></div>'
})
const Component = defineComponent({
template:
'<div><Child v-slot="{ count }"><div>hello{{ count }}there</div></Child></div>',
components: {
Child
}
})
const wrapper = mount(Component, {
shallow: true,
global: {
renderStubDefaultSlot: true
}
})

// count is undefined, but doesn't throw an error
expect(wrapper.html()).toEqual(
'<div>\n' +
' <child-stub>\n' +
' <div>hellothere</div>\n' +
' </child-stub>\n' +
'</div>'
)
})
})