Skip to content

Commit

Permalink
fix: support functional component props (#1326)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
edisonLzy and evanzyli authored Feb 16, 2022
1 parent bf74150 commit 2872e51
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@
]
}
}
}
}
3 changes: 2 additions & 1 deletion src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export function mount(
options?: MountingOptions<any> & Record<string, any>
): VueWrapper<any> {
// normalise the incoming component
let originalComponent = unwrapLegacyVueExtendComponent(inputComponent)
const originalComponent = unwrapLegacyVueExtendComponent(inputComponent)
let component: ConcreteComponent
const instanceOptions = getInstanceOptions(options ?? {})

Expand All @@ -291,6 +291,7 @@ export function mount(
? 'suppress-warning'
: false
},
props: originalComponent.props || {},
setup:
(_, { attrs, slots }) =>
() =>
Expand Down
12 changes: 12 additions & 0 deletions tests/components/FunctionComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { FunctionalComponent } from 'vue'

interface Props {
title: string
}
const Title: FunctionalComponent<Props> = ({ title }) => {
return <h1> {title} </h1>
}
Title.props = {
title: String
}
export default Title
13 changes: 11 additions & 2 deletions tests/props.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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'
Expand All @@ -191,7 +192,6 @@ describe('props', () => {
foo: 'new value'
})
})

it('https://github.com/vuejs/test-utils/issues/440', async () => {
const Foo = defineComponent({
name: 'Foo',
Expand Down Expand Up @@ -275,5 +275,14 @@ describe('props', () => {
'<prop-with-symbol-stub sym="Symbol()"></prop-with-symbol-stub>'
)
})

it('should get props from functional component', async () => {
const wrapper = mount(Title, {
props: {
title: 'nickname'
}
})
expect(wrapper.props('title')).toBe('nickname')
})
})
})

0 comments on commit 2872e51

Please sign in to comment.