8000 fix(core): setProps validates keys against component options · Pull Request #416 · vuejs/vue-test-utils · GitHub
[go: up one dir, main page]

Skip to content

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

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 Feb 7, 2018
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
fix(core): setProps validates keys against component options
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.
  • Loading branch information
Nick Gravelyn committed Feb 7, 2018
commit 2a9d616164d324526d7748e8d42c541e1079a4bd
6 changes: 6 additions & 0 deletions src/wrappers/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,12 @@ export default class Wrapper implements BaseWrapper {
this.vm.$options.propsData = {}
}
Object.keys(data).forEach((key) => {
// Ignore properties that were not specified in the component options
// $FlowIgnore : Problem with possibly null this.vm
if (!this.vm.$options._propKeys.includes(key)) {
return
}

// $FlowIgnore : Problem with possibly null this.vm
if (this.vm._props) {
this.vm._props[key] = data[key]
Expand Down
7 changes: 7 additions & 0 deletions test/specs/wrapper/setProps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ describeWithShallowAndMount('setProps', (mountingMethod) => {
expect(wrapper.find('.prop-2').element.textContent).to.equal(prop2)
})

it('does not add properties not defined in component', () => {
const undefinedProp = 'some value'
const wrapper = mountingMethod(ComponentWithProps)
wrapper.setProps({ undefinedProp })
expect(wrapper.props().undefinedProp).to.be.undefined
})

it('runs watch function when prop is updated', () => {
const wrapper = mountingMethod(ComponentWithWatch)
const prop1 = 'testest'
Expand Down
0