Skip to content

Commit

Permalink
fix: setProps() throws an error if the property is the same reference (
Browse files Browse the repository at this point in the history
  • Loading branch information
mya-ake authored and eddyerburgh committed Jul 5, 2018
1 parent 96f1e39 commit bf655f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,19 @@ 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 the same object ` +
`of the existing ${key} property. ` +
`You must call wrapper.setProps() with a new object ` +
`to trigger reactivity`
)
}

if (this.vm && this.vm._props) {
this.vm._props[key] = data[key]
Expand Down
30 changes: 30 additions & 0 deletions test/specs/wrapper/setProps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<div></div>`,
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: `<div></div>`,
props: ['obj']
}
const obj = {}
const wrapper = mountingMethod(TestComponent, {
propsData: {
obj
}
})

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()
.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('<div><p></p></div>')
Expand Down

0 comments on commit bf655f3

Please sign in to comment.