Skip to content

Commit f1a4d54

Browse files
committed
fix: SetProps throws an error if the property is the same reference (vuejs#761)
1 parent 221038b commit f1a4d54

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

packages/test-utils/src/wrapper.js

+11
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,17 @@ export default class Wrapper implements BaseWrapper {
657657
`is not defined on the component`
658658
)
659659
}
660+
if (
661+
typeof data[key] === 'object' &&
662+
data[key] !== null &&
663+
// $FlowIgnore : Problem with possibly null this.vm
664+
data[key] === this.vm[key]
665+
) {
666+
throwError(
667+
`wrapper.setProps() called with ${key} property ` +
668+
`needs to create a new Object`
669+
)
670+
}
660671

661672
if (this.vm && this.vm._props) {
662673
this.vm._props[key] = data[key]

test/specs/wrapper/setProps.spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,36 @@ describeWithShallowAndMount('setProps', mountingMethod => {
169169
expect(wrapper.vm.data).to.equal('1,2,3,4,5')
170170
})
171171

172+
it('should same reference when called with same object', () => {
173+
const TestComponent = {
174+
template: `<div></div>`,
175+
props: ['obj']
176+
}
177+
const wrapper = mountingMethod(TestComponent)
178+
const obj = {}
179+
wrapper.setProps({ obj })
180+
expect(wrapper.props().obj).to.equal(obj)
181+
})
182+
183+
it('throws an error if property is same reference', () => {
184+
const TestComponent = {
185+
template: `<div></div>`,
186+
props: ['obj']
187+
}
188+
const obj = {}
189+
const wrapper = mountingMethod(TestComponent, {
190+
propsData: {
191+
obj
192+
}
193+
})
194+
195+
const message = '[vue-test-utils]: wrapper.setProps() called with obj property needs to create a new Object'
196+
const fn = () => wrapper.setProps({ obj })
197+
expect(fn)
198+
.to.throw()
199+
.with.property('message', message)
200+
})
201+
172202
it('throws an error if node is not a Vue instance', () => {
173203
const message = 'wrapper.setProps() can only be called on a Vue instance'
174204
const compiled = compileToFunctions('<div><p></p></div>')

0 commit comments

Comments
 (0)