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

fix(core): setProps validates keys against component options #416

Merged
merged 1 commit into from Feb 7, 2018
Merged

fix(core): setProps validates keys against component options #416

merged 1 commit into from Feb 7, 2018

Conversation

ghost
Copy link

@ghost ghost commented Feb 7, 2018

My team and I discovered that setProps adds all given keys as values on components, whereas the propsData passed to the Vue constructor will only add properties for those that are actually defined. For example if I have this component:

<template>
  <div>{{someprop}}</div>
</template>

<script>
  export default{
    name: 'My Component',
    props: ['someprop']
  }
</script>

If I mount the component for test with options:

var testObj = mount(MyComponent, {
    propsData: {
        somewrongprop: 'value'
    }
})

Vue ignores that somewrongprop entirely. However we found that calling:

testObj.setProps({ somewrongprop: 'value' })

did add that property to the instance. This bit us a little in our TDD since the developer didn't realize they hadn't defined the property in their component because of setProp having that inconsistent behavior. Basically they could use a variable in the template that wasn't defined in data or props because setProps injected it for us.

We started using mount in more tests to avoid this, but it felt bad so here's a PR that seems to resolve the issue by having setProps check the vm.$options._propsKey value that Vue creates. I'm not sure that poking into that _propsKey collection is the best answer since that feels very private to me, but I'm not sure another way to do this check and it seems like this is important behavior for setProps for consistency. If there's another way to approach this or a good reason why setProps shouldn't have this, please let me know.

Vue's constructor doesn't allow setting props on a component that weren't
declared, however the setProps method was adding all given keys regardless of
whether they were declared for the component. This change makes setProps
validate keys against the Vue instance's cached list of property keys before
setting the value so that setProps behavior more closely matches that of
passing propsData into the constructor options.
@ghost ghost changed the title setProps validates keys against component options fix(core): setProps validates keys against component options Feb 7, 2018
Copy link
Member

@eddyerburgh eddyerburgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, thanks!

I think this is a fine way to validate that props can be accepted by the component. 😀

@eddyerburgh eddyerburgh merged commit a63e34d into vuejs:dev Feb 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant