Skip to content

Commit 37924d7

Browse files
committed
refactor: updated set data method and add a new test for data obj
1 parent 498c2fd commit 37924d7

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

packages/test-utils/src/wrapper.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,14 @@ export default class Wrapper implements BaseWrapper {
421421

422422
Object.keys(data).forEach((key) => {
423423
// $FlowIgnore : Problem with possibly null this.vm
424-
this.vm.$set(this.vm, [key], data[key])
424+
if (typeof data[key] === typeof {} && data[key] !== null) {
425+
Object.keys(data[key]).forEach((key2) => {
426+
var newObj = Object.assign({}, data[key], data[key][key2])
427+
this.vm.$set(this.vm, [key], newObj)
428+
})
429+
} else {
430+
this.vm.$set(this.vm, [key], data[key])
431+
}
425432
})
426433
}
427434

test/specs/wrapper/setData.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,25 @@ describeWithShallowAndMount('setData', (mountingMethod) => {
132132
wrapper.setData({ message: null })
133133
expect(wrapper.text()).to.equal('There is no message yet')
134134
})
135+
it('should update a data object', () => {
136+
const Cmp = {
137+
data: () => ({
138+
anObject: {
139+
propA: {
140+
prop1: 'a'
141+
},
142+
propB: 'b'
143+
}
144+
})
145+
}
146+
const wrapper = mountingMethod(Cmp)
147+
wrapper.setData({
148+
anObject: {
149+
propA: {
150+
prop1: 'c'
151+
}
152+
}
153+
})
154+
expect(wrapper.vm.anObject.propA.prop1).to.equal('c')
155+
})
135156
})

0 commit comments

Comments
 (0)