From f1a4d547260b6260c9631b790e374b1bb55ac37a Mon Sep 17 00:00:00 2001 From: mya-ake Date: Tue, 3 Jul 2018 09:56:57 +0900 Subject: [PATCH 1/2] fix: SetProps throws an error if the property is the same reference (#761) --- packages/test-utils/src/wrapper.js | 11 +++++++++++ test/specs/wrapper/setProps.spec.js | 30 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/packages/test-utils/src/wrapper.js b/packages/test-utils/src/wrapper.js index db4180acd..836c4bbca 100644 --- a/packages/test-utils/src/wrapper.js +++ b/packages/test-utils/src/wrapper.js @@ -657,6 +657,17 @@ export default class Wrapper implements BaseWrapper { `is not defined on the component` ) } + if ( + typeof data[key] === 'object' && + data[key] !== null && + // $FlowIgnore : Problem with possibly null this.vm + data[key] === this.vm[key] + ) { + throwError( + `wrapper.setProps() called with ${key} property ` + + `needs to create a new Object` + ) + } if (this.vm && 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 a275f1ab7..c3e376ffa 100644 --- a/test/specs/wrapper/setProps.spec.js +++ b/test/specs/wrapper/setProps.spec.js @@ -169,6 +169,36 @@ describeWithShallowAndMount('setProps', mountingMethod => { expect(wrapper.vm.data).to.equal('1,2,3,4,5') }) + it('should same reference when called with same object', () => { + const TestComponent = { + template: `
`, + props: ['obj'] + } + const wrapper = mountingMethod(TestComponent) + const obj = {} + wrapper.setProps({ obj }) + expect(wrapper.props().obj).to.equal(obj) + }) + + it('throws an error if property is same reference', () => { + const TestComponent = { + template: `
`, + props: ['obj'] + } + const obj = {} + const wrapper = mountingMethod(TestComponent, { + propsData: { + obj + } + }) + + const message = '[vue-test-utils]: wrapper.setProps() called with obj property needs to create a new Object' + const fn = () => wrapper.setProps({ obj }) + expect(fn) + .to.throw() + .with.property('message', message) + }) + it('throws an error if node is not a Vue instance', () => { const message = 'wrapper.setProps() can only be called on a Vue instance' const compiled = compileToFunctions('

') From 194e681cd922f979baba37ac13a8f286fc442bd1 Mon Sep 17 00:00:00 2001 From: mya-ake Date: Wed, 4 Jul 2018 18:45:04 +0900 Subject: [PATCH 2/2] Update error message (#761) --- packages/test-utils/src/wrapper.js | 6 ++++-- test/specs/wrapper/setProps.spec.js | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/test-utils/src/wrapper.js b/packages/test-utils/src/wrapper.js index 836c4bbca..69ad5af8a 100644 --- a/packages/test-utils/src/wrapper.js +++ b/packages/test-utils/src/wrapper.js @@ -664,8 +664,10 @@ export default class Wrapper implements BaseWrapper { data[key] === this.vm[key] ) { throwError( - `wrapper.setProps() called with ${key} property ` + - `needs to create a new Object` + `wrapper.setProps() called with the same object ` + + `of the existing ${key} property. ` + + `You must call wrapper.setProps() with a new object ` + + `to trigger reactivity` ) } diff --git a/test/specs/wrapper/setProps.spec.js b/test/specs/wrapper/setProps.spec.js index c3e376ffa..fee329a4a 100644 --- a/test/specs/wrapper/setProps.spec.js +++ b/test/specs/wrapper/setProps.spec.js @@ -192,7 +192,7 @@ describeWithShallowAndMount('setProps', mountingMethod => { } }) - const message = '[vue-test-utils]: wrapper.setProps() called with obj property needs to create a new Object' + const message = '[vue-test-utils]: wrapper.setProps() called with the same object of the existing obj property. You must call wrapper.setProps() with a new object to trigger reactivity' const fn = () => wrapper.setProps({ obj }) expect(fn) .to.throw()