From 2872e51a8e56b8f7bd7786c0856d8351a489d52c Mon Sep 17 00:00:00 2001 From: evanzyli <45064281+edisonLzy@users.noreply.github.com> Date: Wed, 16 Feb 2022 20:06:10 +0800 Subject: [PATCH] fix: support functional component props (#1326) * feat(props): add test for functional component * feat(props): compatible functional component props #1311 * feat(props): compatible functional component props #1311 * fix: lint code style Co-authored-by: evanzyli --- package.json | 2 +- src/mount.ts | 3 ++- tests/components/FunctionComponent.tsx | 12 ++++++++++++ tests/props.spec.ts | 13 +++++++++++-- 4 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 tests/components/FunctionComponent.tsx diff --git a/package.json b/package.json index 34de6cfdc..bd8ee5554 100644 --- a/package.json +++ b/package.json @@ -92,4 +92,4 @@ ] } } -} \ No newline at end of file +} diff --git a/src/mount.ts b/src/mount.ts index d75b70904..714191073 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -274,7 +274,7 @@ export function mount( options?: MountingOptions & Record ): VueWrapper { // normalise the incoming component - let originalComponent = unwrapLegacyVueExtendComponent(inputComponent) + const originalComponent = unwrapLegacyVueExtendComponent(inputComponent) let component: ConcreteComponent const instanceOptions = getInstanceOptions(options ?? {}) @@ -291,6 +291,7 @@ export function mount( ? 'suppress-warning' : false }, + props: originalComponent.props || {}, setup: (_, { attrs, slots }) => () => diff --git a/tests/components/FunctionComponent.tsx b/tests/components/FunctionComponent.tsx new file mode 100644 index 000000000..0a14c8209 --- /dev/null +++ b/tests/components/FunctionComponent.tsx @@ -0,0 +1,12 @@ +import type { FunctionalComponent } from 'vue' + +interface Props { + title: string +} +const Title: FunctionalComponent = ({ title }) => { + return

{title}

+} +Title.props = { + title: String +} +export default Title diff --git a/tests/props.spec.ts b/tests/props.spec.ts index 84e3e7ab7..46843ca4c 100644 --- a/tests/props.spec.ts +++ b/tests/props.spec.ts @@ -3,6 +3,7 @@ import WithProps from './components/WithProps.vue' import PropWithSymbol from './components/PropWithSymbol.vue' import Hello from './components/Hello.vue' import { defineComponent, h } from 'vue' +import Title from './components/FunctionComponent' describe('props', () => { it('returns a single prop applied to a component', () => { @@ -179,7 +180,7 @@ describe('props', () => { const wrapper = mount(Component, { shallow: true }) - let fooCmp = wrapper.findComponent({ name: 'Foo' }) + const fooCmp = wrapper.findComponent({ name: 'Foo' }) expect(fooCmp.props()).toEqual({ foo: 'old value' @@ -191,7 +192,6 @@ describe('props', () => { foo: 'new value' }) }) - it('https://github.com/vuejs/test-utils/issues/440', async () => { const Foo = defineComponent({ name: 'Foo', @@ -275,5 +275,14 @@ describe('props', () => { '' ) }) + + it('should get props from functional component', async () => { + const wrapper = mount(Title, { + props: { + title: 'nickname' + } + }) + expect(wrapper.props('title')).toBe('nickname') + }) }) })