Skip to content

Commit

Permalink
refactor: updated set data method and add a new test for data obj
Browse files Browse the repository at this point in the history
  • Loading branch information
shortdiv committed Apr 26, 2018
1 parent 498c2fd commit 37924d7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,14 @@ export default class Wrapper implements BaseWrapper {

Object.keys(data).forEach((key) => {
// $FlowIgnore : Problem with possibly null this.vm
this.vm.$set(this.vm, [key], data[key])
if (typeof data[key] === typeof {} && data[key] !== null) {
Object.keys(data[key]).forEach((key2) => {
var newObj = Object.assign({}, data[key], data[key][key2])
this.vm.$set(this.vm, [key], newObj)
})
} else {
this.vm.$set(this.vm, [key], data[key])
}
})
}

Expand Down
21 changes: 21 additions & 0 deletions test/specs/wrapper/setData.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,25 @@ describeWithShallowAndMount('setData', (mountingMethod) => {
wrapper.setData({ message: null })
expect(wrapper.text()).to.equal('There is no message yet')
})
it('should update a data object', () => {
const Cmp = {
data: () => ({
anObject: {
propA: {
prop1: 'a'
},
propB: 'b'
}
})
}
const wrapper = mountingMethod(Cmp)
wrapper.setData({
anObject: {
propA: {
prop1: 'c'
}
}
})
expect(wrapper.vm.anObject.propA.prop1).to.equal('c')
})
})

0 comments on commit 37924d7

Please sign in to comment.