Skip to content

Commit 032a7a4

Browse files
authored
feat: overwrite arrays in setData (#652)
breaking change
1 parent 3ecce2e commit 032a7a4

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

docs/api/wrapper/setData.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## setData(data)
22

3-
Sets `Wrapper` `vm` data and forces update.
3+
Sets `Wrapper` `vm` data.
4+
5+
setData works by merging existing properties, except for arrays which are overwritten.
46

57
**Note the Wrapper must contain a Vue instance.**
68

flow/modules.flow.js

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

15-
declare module 'lodash/merge' {
15+
declare module 'lodash/mergeWith' {
1616
declare module.exports: any;
1717
}
1818

packages/test-utils/src/wrapper.js

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

33
import Vue from 'vue'
4-
import merge from 'lodash/merge'
4+
import mergeWith from 'lodash/mergeWith'
55
import getSelectorTypeOrThrow from './get-selector-type'
66
import {
77
REF_SELECTOR,
@@ -424,7 +424,9 @@ export default class Wrapper implements BaseWrapper {
424424
if (typeof data[key] === 'object' && data[key] !== null &&
425425
!Array.isArray(data[key])) {
426426
// $FlowIgnore : Problem with possibly null this.vm
427-
const newObj = merge(this.vm[key], data[key])
427+
const newObj = mergeWith(this.vm[key], data[key], (objValue, srcValue) => {
428+
return Array.isArray(srcValue) ? srcValue : undefined
429+
})
428430
// $FlowIgnore : Problem with possibly null this.vm
429431
this.vm.$set(this.vm, [key], newObj)
430432
} else {

test/specs/wrapper/setData.spec.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ describeWithShallowAndMount('setData', (mountingMethod) => {
165165
},
166166
propB: 'b'
167167
}
168-
})
168+
}),
169+
render: () => {}
169170
}
170171
const wrapper = mountingMethod(TestComponent)
171172
wrapper.setData({
@@ -179,16 +180,31 @@ describeWithShallowAndMount('setData', (mountingMethod) => {
179180
expect(wrapper.vm.anObject.propA.prop2).to.equal('b')
180181
})
181182

182-
it('sets array data properly', () => {
183+
it('does not merge arrays', () => {
183184
const TestComponent = {
185+
template: '<div>{{nested.nested.nestedArray[0]}}</div>',
184186
data: () => ({
185-
items: [1, 2]
187+
items: [1, 2],
188+
nested: {
189+
nested: {
190+
nestedArray: [1, 2]
191+
}
192+
}
186193
})
187194
}
188195
const wrapper = mountingMethod(TestComponent)
189196
wrapper.setData({
190197
items: [3]
191198
})
192199
expect(wrapper.vm.items).to.deep.equal([3])
200+
wrapper.setData({
201+
nested: {
202+
nested: {
203+
nestedArray: [10]
204+
}
205+
}
206+
})
207+
expect(wrapper.text()).to.equal('10')
208+
expect(wrapper.vm.nested.nested.nestedArray).to.deep.equal([10])
193209
})
194210
})

0 commit comments

Comments
 (0)