Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: setProps() throws an error if the property is the same reference #791

Merged
merged 2 commits into from
Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ` +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reword:
wrapper.setProps() called with the same object of the existing ${key} property. You must call wrapper.setProps() with a new object to trigger reactivity

`needs to create a new Object`
)
}

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