Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 032a7a4

Browse filesBrowse files
authored
feat: overwrite arrays in setData (vuejs#652)
breaking change
1 parent 3ecce2e commit 032a7a4
Copy full SHA for 032a7a4

File tree

Expand file treeCollapse file tree

4 files changed

+27
-7
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+27
-7
lines changed

‎docs/api/wrapper/setData.md

Copy file name to clipboardExpand all lines: docs/api/wrapper/setData.md
+3-1Lines changed: 3 additions & 1 deletion
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

Copy file name to clipboardExpand all lines: flow/modules.flow.js
+1-1Lines changed: 1 addition & 1 deletion
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

Copy file name to clipboardExpand all lines: packages/test-utils/src/wrapper.js
+4-2Lines changed: 4 additions & 2 deletions
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

Copy file name to clipboardExpand all lines: test/specs/wrapper/setData.spec.js
+19-3Lines changed: 19 additions & 3 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.