Skip to content

Commit 618641f

Browse files
committed
fix: update deep merge to use lodash merge and updated tests
1 parent 12ac952 commit 618641f

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

flow/modules.flow.js

Lines changed: 4 additions & 0 deletions
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
}

packages/test-utils/src/wrapper.js

Lines changed: 4 additions & 5 deletions
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,
@@ -421,11 +422,9 @@ export default class Wrapper implements BaseWrapper {
421422

422423
Object.keys(data).forEach((key) => {
423424
if (typeof data[key] === 'object' && data[key] !== null) {
424-
Object.keys(data[key]).forEach((key2) => {
425-
const newObj = { ...data[key], ...data[key][key2] }
426-
// $FlowIgnore : Problem with possibly null this.vm
427-
this.vm.$set(this.vm, [key], newObj)
428-
})
425+
const newObj = merge(this.vm[key], data[key])
426+
// $FlowIgnore : Problem with possibly null this.vm
427+
this.vm.$set(this.vm, [key], newObj)
429428
} else {
430429
// $FlowIgnore : Problem with possibly null this.vm
431430
this.vm.$set(this.vm, [key], data[key])

test/specs/wrapper/setData.spec.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ 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 = {
135+
136+
it('should update an existing property in a data object', () => {
137+
const TestComponent = {
137138
data: () => ({
138139
anObject: {
139140
propA: {
@@ -143,14 +144,38 @@ describeWithShallowAndMount('setData', (mountingMethod) => {
143144
}
144145
})
145146
}
146-
const wrapper = mountingMethod(Cmp)
147+
const wrapper = mountingMethod(TestComponent)
147148
wrapper.setData({
148149
anObject: {
149150
propA: {
150151
prop1: 'c'
151152
}
152153
}
153154
})
155+
expect(wrapper.vm.anObject.propB).to.equal('b')
154156
expect(wrapper.vm.anObject.propA.prop1).to.equal('c')
155157
})
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+
})
156181
})

0 commit comments

Comments
 (0)