Skip to content

Commit 878bccd

Browse files
shortdiveddyerburgh
authored andcommittedApr 27, 2018
feat: support merging in setData (#565)
closes #563
1 parent 0646796 commit 878bccd

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed
 

Diff for: ‎flow/modules.flow.js

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ declare module 'lodash/cloneDeep' {
1212
declare module.exports: any;
1313
}
1414

15+
declare module 'lodash/merge' {
16+
declare module.exports: any;
17+
}
18+
1519
declare module 'vue-template-compiler' {
1620
declare module.exports: any;
1721
}

Diff for: ‎packages/test-utils/src/wrapper.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22

33
import Vue from 'vue'
4+
import merge from 'lodash/merge'
45
import getSelectorTypeOrThrow from './get-selector-type'
56
import {
67
REF_SELECTOR,
@@ -420,8 +421,15 @@ export default class Wrapper implements BaseWrapper {
420421
}
421422

422423
Object.keys(data).forEach((key) => {
423-
// $FlowIgnore : Problem with possibly null this.vm
424-
this.vm.$set(this.vm, [key], data[key])
424+
if (typeof data[key] === 'object' && data[key] !== null) {
425+
// $FlowIgnore : Problem with possibly null this.vm
426+
const newObj = merge(this.vm[key], data[key])
427+
// $FlowIgnore : Problem with possibly null this.vm
428+
this.vm.$set(this.vm, [key], newObj)
429+
} else {
430+
// $FlowIgnore : Problem with possibly null this.vm
431+
this.vm.$set(this.vm, [key], data[key])
432+
}
425433
})
426434
}
427435

Diff for: ‎test/specs/wrapper/setData.spec.js

+46
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,50 @@ describeWithShallowAndMount('setData', (mountingMethod) => {
132132
wrapper.setData({ message: null })
133133
expect(wrapper.text()).to.equal('There is no message yet')
134134
})
135+
136+
it('should update an existing property in a data object', () => {
137+
const TestComponent = {
138+
data: () => ({
139+
anObject: {
140+
propA: {
141+
prop1: 'a'
142+
},
143+
propB: 'b'
144+
}
145+
})
146+
}
147+
const wrapper = mountingMethod(TestComponent)
148+
wrapper.setData({
149+
anObject: {
150+
propA: {
151+
prop1: 'c'
152+
}
153+
}
154+
})
155+
expect(wrapper.vm.anObject.propB).to.equal('b')
156+
expect(wrapper.vm.anObject.propA.prop1).to.equal('c')
157+
})
158+
159+
it('should append a new property to an object without removing existing properties', () => {
160+
const TestComponent = {
161+
data: () => ({
162+
anObject: {
163+
propA: {
164+
prop1: 'a'
165+
},
166+
propB: 'b'
167+
}
168+
})
169+
}
170+
const wrapper = mountingMethod(TestComponent)
171+
wrapper.setData({
172+
anObject: {
173+
propA: {
174+
prop2: 'b'
175+
}
176+
}
177+
})
178+
expect(wrapper.vm.anObject.propA.prop1).to.equal('a')
179+
expect(wrapper.vm.anObject.propA.prop2).to.equal('b')
180+
})
135181
})

0 commit comments

Comments
 (0)
Please sign in to comment.