Skip to content

Commit 22c7698

Browse files
xanfcexbrayat
authored andcommitted
fix(stubs): Do not render function body in stub
1 parent 348d651 commit 22c7698

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/vnodeTransformers/stubComponentsTransformer.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,16 @@ const shouldNotStub = (type: ConcreteComponent) => doNotStubComponents.has(type)
6363
export const addToDoNotStubComponents = (type: ConcreteComponent) =>
6464
doNotStubComponents.add(type)
6565

66-
const stringifySymbols = (props: ComponentPropsOptions) => {
66+
const normalizeStubProps = (props: ComponentPropsOptions) => {
6767
// props are always normalized to object syntax
6868
const $props = props as unknown as ComponentObjectPropsOptions
6969
return Object.keys($props).reduce((acc, key) => {
7070
if (typeof $props[key] === 'symbol') {
7171
return { ...acc, [key]: $props[key]?.toString() }
7272
}
73+
if (typeof $props[key] === 'function') {
74+
return { ...acc, [key]: '[Function]' }
75+
}
7376
return { ...acc, [key]: $props[key] }
7477
}, {})
7578
}
@@ -100,13 +103,11 @@ export const createStub = ({
100103
// causes an error.
101104
// Only a problem when shallow mounting. For this reason we iterate of the
102105
// props that will be passed and stringify any that are symbols.
103-
const propsWithoutSymbols = stringifySymbols(props)
106+
// Also having function text as attribute is useless and annoying so
107+
// we replace it with "[Function]""
108+
const stubProps = normalizeStubProps(props)
104109

105-
return h(
106-
tag,
107-
propsWithoutSymbols,
108-
renderStubDefaultSlot ? slots : undefined
109-
)
110+
return h(tag, stubProps, renderStubDefaultSlot ? slots : undefined)
110111
}
111112
}
112113
})

tests/props.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,22 @@ describe('props', () => {
274274
expect(wrapper.text()).toEqual('hello')
275275
})
276276

277+
it('stub function props when shallow mounting', () => {
278+
const Comp = defineComponent({
279+
name: 'Comp',
280+
template: `<div>Test</div>`,
281+
props: ['fn']
282+
})
283+
284+
const wrapper = shallowMount({
285+
render() {
286+
return h(Comp, { fn: () => {} })
287+
}
288+
})
289+
290+
expect(wrapper.html()).toBe('<comp-stub fn="[Function]"></comp-stub>')
291+
})
292+
277293
describe('edge case with symbol props and stubs', () => {
278294
it('works with Symbol as default', () => {
279295
const Comp = defineComponent({

0 commit comments

Comments
 (0)