Skip to content

fix: update $props object in setProps #381

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
Jan 26, 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
17 changes: 13 additions & 4 deletions src/wrappers/vue-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ import Wrapper from './wrapper'
import addSlots from '../lib/add-slots'
import cloneDeep from 'lodash/cloneDeep'

function update () {
function update (changedData) {
// the only component made by mount()
if (this.$_originalSlots) {
this.$slots = cloneDeep(this.$_originalSlots)
}
if (this.$_mountingOptionsSlots) {
addSlots(this, this.$_mountingOptionsSlots)
}
this._watchers.forEach(watcher => {
watcher.run()
})
if (changedData) {
Object.keys(changedData).forEach((key) => {
// $FlowIgnore : Problem with possibly null this.vm
this._watchers.forEach((watcher) => {
if (watcher.expression === key) { watcher.run() }
})
})
} else {
this._watchers.forEach(watcher => {
watcher.run()
})
}
const vnodes = this._render()
this._update(vnodes)
this.$children.forEach(child => update.call(child))
Expand Down
11 changes: 8 additions & 3 deletions src/wrappers/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export default class Wrapper implements BaseWrapper {
this.vm.$set(this.vm, [key], data[key])
})

this.update()
this.update(data)
}

/**
Expand Down Expand Up @@ -452,18 +452,23 @@ export default class Wrapper implements BaseWrapper {
if (!this.isVueComponent || !this.vm) {
throwError('wrapper.setProps() can only be called on a Vue instance')
}

if (!this.vm.$options.propsData) {
this.vm.$options.propsData = {}
}
Object.keys(data).forEach((key) => {
// $FlowIgnore : Problem with possibly null this.vm
if (this.vm._props) {
this.vm._props[key] = data[key]
this.vm.$props[key] = data[key]
this.vm.$options.propsData[key] = data[key]
} else {
// $FlowIgnore : Problem with possibly null this.vm
this.vm[key] = data[key]
this.vm.$options.propsData[key] = data[key]
}
})

this.update()
this.update(data)
// $FlowIgnore : Problem with possibly null this.vm
this.vnode = this.vm._vnode
}
Expand Down
14 changes: 14 additions & 0 deletions test/unit/specs/mount/Wrapper/props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ describe('props', () => {
expect(wrapper.props()).to.eql({})
})

it('should update after setProps', () => {
const prop1 = {}
const prop2 = 'val1'
const wrapper = mount(ComponentWithProps, {
propsData: { prop1, prop2 }
})

expect(wrapper.props()).to.eql({ prop1: {}, prop2: 'val1' })
// setProps
wrapper.setProps({ prop2: 'val2' })
expect(wrapper.vm.prop2).to.eql('val2') // pass
expect(wrapper.props()).to.eql({ prop1: {}, prop2: 'val2' }) // fail
})

it('throws an error if called on a non vm wrapper', () => {
const compiled = compileToFunctions('<div><p /></div>')
const p = mount(compiled).findAll('p').at(0)
Expand Down
22 changes: 22 additions & 0 deletions test/unit/specs/mount/Wrapper/setData.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,26 @@ describe('setData', () => {
const p = wrapper.find('p')
expect(() => p.setData({ ready: true })).throw(Error, message)
})

it('should not run watchers if data updated is null', () => {
const TestComponent = {
template: `
<div>
<div v-if="!message">There is no message yet</div>
<div v-else>{{ reversedMessage }}</div>
</div>
`,
data: () => ({
message: 'egassem'
}),
computed: {
reversedMessage: function () {
return this.message.split('').reverse().join('')
}
}
}
const wrapper = mount(TestComponent)
wrapper.setData({ message: null })
expect(wrapper.text()).to.equal('There is no message yet')
})
})
24 changes: 24 additions & 0 deletions test/unit/specs/mount/Wrapper/setProps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ describe('setProps', () => {
expect(info.args[0][0]).to.equal(prop1)
})

it('should not run watchers if prop updated is null', () => {
const TestComponent = {
template: `
<div>
<div v-if="!message">There is no message yet</div>
<div v-else>{{ reversedMessage }}</div>
</div>
`,
computed: {
reversedMessage: function () {
return this.message.split('').reverse().join('')
}
},
props: ['message']
}
const wrapper = mount(TestComponent, {
propsData: {
message: 'message'
}
})
wrapper.setProps({ message: null })
expect(wrapper.text()).to.equal('There is no message yet')
})

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