Skip to content

fix: update props when watcher depends on value #842

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

Merged
merged 2 commits into from
Jul 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,12 +674,17 @@ export default class Wrapper implements BaseWrapper {
}

if (this.vm && this.vm._props) {
// Set actual props value
this.vm._props[key] = data[key]
} else {
// $FlowIgnore : Problem with possibly null this.vm
this.vm[key] = data[key]
} else {
// $FlowIgnore : Problem with possibly null this.vm.$options
this.vm.$options.propsData[key] = data[key]
// $FlowIgnore : Problem with possibly null this.vm
this.vm[key] = data[key]
// $FlowIgnore : Need to call this twice to fix watcher bug in 2.0.x
this.vm[key] = data[key]
}
})
// $FlowIgnore : Problem with possibly null this.vm
Expand Down
18 changes: 18 additions & 0 deletions test/specs/wrapper/setProps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ describeWithShallowAndMount('setProps', mountingMethod => {
.with.property('message', message)
})

it('updates watched prop', () => {
const TestComponent = {
template: '<div />',
props: ['propA'],
mounted () {
this.$watch('propA', function () {
this.propA
}, { immediate: true }
)
}
}
const wrapper = mountingMethod(TestComponent, { propsData: { propA: 'none' }})

wrapper.setProps({ propA: 'value' })
expect(wrapper.props().propA).to.equal('value')
expect(wrapper.vm.propA).to.equal('value')
})

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