Skip to content

Commit bf655f3

Browse files
mya-akeeddyerburgh
authored andcommitted
fix: setProps() throws an error if the property is the same reference (#791)
1 parent 96f1e39 commit bf655f3

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

packages/test-utils/src/wrapper.js

+13
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,19 @@ export default class Wrapper implements BaseWrapper {
659659
`is not defined on the component`
660660
)
661661
}
662+
if (
663+
typeof data[key] === 'object' &&
664+
data[key] !== null &&
665+
// $FlowIgnore : Problem with possibly null this.vm
666+
data[key] === this.vm[key]
667+
) {
668+
throwError(
669+
`wrapper.setProps() called with the same object ` +
670+
`of the existing ${key} property. ` +
671+
`You must call wrapper.setProps() with a new object ` +
672+
`to trigger reactivity`
673+
)
674+
}
662675

663676
if (this.vm && this.vm._props) {
664677
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 the same object of the existing obj property. You must call wrapper.setProps() with a new object to trigger reactivity'
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)