Skip to content
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

Bug: The wrapper methods props can't get specific prop in functional component #1311

Closed
edisonLzy opened this issue Feb 14, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@edisonLzy
Copy link
Contributor

Describe the bug

The wrapper methods props can't get specific prop in functional component
To Reproduce

const wrapper = mount(Component,{
props: {
title: 'title'
}
});
expect(wrapper.props('title')).toContain('title') // not pass , the wrapper.props('title') is undeinfed

Expected behavior

const wrapper = mount(Component,{
  props: {
    title: 'title'
  }
});
expect(wrapper.props('title')).toContain('title')  // pass

Related information:

  • @vue/test-utils version: ^2.0.0-rc.17
  • Vue version: 3.2.27
  • node version: v16.0.0
  • npm (or yarn) version: v8.3.0

Additional context

@edisonLzy edisonLzy added the bug Something isn't working label Feb 14, 2022
@cexbrayat
Copy link
Member

Hmm, interesting. I guess not a lot of people are using functional components in Vue 3 and we never caught this. Would you like to try to fix it?

You can start by adding a test in props.spec.ts https://github.com/vuejs/test-utils/blob/main/tests/props.spec.ts to reproduce your error.

Then the relevant code is here:

props(): { [key: string]: any }
props(selector: string): any
props(selector?: string): { [key: string]: any } | any {
const props = this.componentVM.$props as { [key: string]: any }
return selector ? props[selector] : props
}

We probably need some special handling for functional components.

@edisonLzy
Copy link
Contributor Author

edisonLzy commented Feb 15, 2022

woot 😄 . i find the problem is the functional component wrapper ignored the props options.
The code is here:

test-utils/src/mount.ts

Lines 281 to 301 in 67a3e7a

if (
isFunctionalComponent(originalComponent) ||
isLegacyFunctionalComponent(originalComponent)
) {
component = defineComponent({
compatConfig: {
MODE: 3,
INSTANCE_LISTENERS: false,
INSTANCE_ATTRS_CLASS_STYLE: false,
COMPONENT_FUNCTIONAL: isLegacyFunctionalComponent(originalComponent)
? 'suppress-warning'
: false
},
setup:
(_, { attrs, slots }) =>
() =>
h(originalComponent, attrs, slots),
...instanceOptions
})
addToDoNotStubComponents(originalComponent)
} else if (isObjectComponent(originalComponent)) {

I have an idea is add instance methods name attrs in wrapper like this:

const wrapper = mount(xx,{
 props: {
   "title":"h1" 
 }
});
wrapper.attrs('title') === "h1"

edisonLzy pushed a commit to edisonLzy/test-utils that referenced this issue Feb 16, 2022
edisonLzy pushed a commit to edisonLzy/test-utils that referenced this issue Feb 16, 2022
cexbrayat pushed a commit that referenced this issue Feb 16, 2022
* 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]>
@cexbrayat
Copy link
Member

Fixed by #1326

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants