Skip to content

Commit

Permalink
fix: renderStubDefaultSlot with scoped slots
Browse files Browse the repository at this point in the history
Fixes #2395

We now explicitely call the default slot with an empty object to ensure that `<ComponentWithSlot v-slot="{ count }">` won't throw when tested with `renderStubDefaultSlot`.
  • Loading branch information
cexbrayat committed Apr 4, 2024
1 parent d686ec4 commit e07276c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/vnodeTransformers/stubComponentsTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,14 @@ 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) {
// we explicitly call the default slot with an empty object
// so scope slots destructuring works
const slot = slots.default ? slots.default({}) : slots
return h(tag, stubProps, slot)
}
return h(tag, stubProps);

Check failure on line 127 in src/vnodeTransformers/stubComponentsTransformer.ts

View workflow job for this annotation

GitHub Actions / build (18)

Delete `;`

Check failure on line 127 in src/vnodeTransformers/stubComponentsTransformer.ts

View workflow job for this annotation

GitHub Actions / build (20)

Delete `;`
}
}
})
Expand Down
41 changes: 35 additions & 6 deletions tests/mountingOptions/global.components.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ describe('global.components', () => {
spy.mockRestore()
expect(wrapper.html()).toBe(
'<div>\n' +
' <global-component-stub></global-component-stub>\n' +
'</div>'
' <global-component-stub></global-component-stub>\n' +

Check failure on line 47 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `··`

Check failure on line 47 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
'</div>'

Check failure on line 48 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `··`

Check failure on line 48 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
)
})

Expand All @@ -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 @@ -91,10 +92,38 @@ describe('global.components', () => {

expect(wrapper.html()).toEqual(
'<div>\n' +
' <child-stub>\n' +
' <div>hello</div>\n' +
' </child-stub>\n' +
'</div>'
' <child-stub>\n' +

Check failure on line 95 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `··`

Check failure on line 95 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
' <div>hello</div>\n' +

Check failure on line 96 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `··`

Check failure on line 96 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
' </child-stub>\n' +

Check failure on line 97 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `··`

Check failure on line 97 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
'</div>'

Check failure on line 98 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `··`

Check failure on line 98 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
)
})

// 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>',

Check failure on line 108 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `⏎·······`

Check failure on line 108 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `⏎·······`
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' +

Check failure on line 123 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `··`

Check failure on line 123 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
' <div>hellothere</div>\n' +

Check failure on line 124 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `··`

Check failure on line 124 in tests/mountingOptions/global.components.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
' </child-stub>\n' +
'</div>'
)
})
})

0 comments on commit e07276c

Please sign in to comment.