Skip to content

fix(compat): handle listener, style and class in legacy functional components #1080

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

Merged
merged 1 commit into from
Nov 17, 2021
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
5 changes: 5 additions & 0 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ export function mount(
isLegacyFunctionalComponent(originalComponent)
) {
component = defineComponent({
compatConfig: {
MODE: 3,
INSTANCE_LISTENERS: false,
INSTANCE_ATTRS_CLASS_STYLE: false
},
setup:
(_, { attrs, slots }) =>
() =>
Expand Down
8 changes: 4 additions & 4 deletions src/utils/vueCompatSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export function isLegacyFunctionalComponent(component: unknown) {
return false
}

return (
return Boolean(
component &&
typeof component === 'object' &&
hasOwnProperty(component, 'functional') &&
component.functional
typeof component === 'object' &&
hasOwnProperty(component, 'functional') &&
component.functional
)
}
31 changes: 31 additions & 0 deletions tests-compat/compat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,35 @@ describe('@vue/compat build', () => {

expect(wrapper.vm.foo).toBe('bar')
})

it('correctly passes all props to functional component', async () => {
configureCompat({
MODE: 3,
INSTANCE_LISTENERS: 'suppress-warning',
INSTANCE_ATTRS_CLASS_STYLE: 'suppress-warning'
})

const FunctionalComponent = {
functional: true,
render(h: any, context: any) {
return h('div', context.data, context.props.text)
}
}

const onClick = jest.fn()
const wrapper = mount(FunctionalComponent, {
props: {
class: 'foo',
text: 'message',
style: { color: 'red' },
onClick
}
})
expect(wrapper.text()).toBe('message')
await wrapper.trigger('click')
expect(onClick).toHaveBeenCalled()
expect(wrapper.html()).toBe(
'<div class="foo" text="message" style="color: red;">message</div>'
)
})
})