From 2a9d616164d324526d7748e8d42c541e1079a4bd Mon Sep 17 00:00:00 2001 From: Nick Gravelyn Date: Tue, 6 Feb 2018 21:33:05 -0800 Subject: [PATCH] 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. --- src/wrappers/wrapper.js | 6 ++++++ test/specs/wrapper/setProps.spec.js | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/wrappers/wrapper.js b/src/wrappers/wrapper.js index efc9fc730..b48b2b485 100644 --- a/src/wrappers/wrapper.js +++ b/src/wrappers/wrapper.js @@ -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] diff --git a/test/specs/wrapper/setProps.spec.js b/test/specs/wrapper/setProps.spec.js index aafb49c33..d67cdc112 100644 --- a/test/specs/wrapper/setProps.spec.js +++ b/test/specs/wrapper/setProps.spec.js @@ -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'