Skip to content

Commit

Permalink
feat: overwrite arrays in setData (#652)
Browse files Browse the repository at this point in the history
breaking change
  • Loading branch information
eddyerburgh authored May 25, 2018
1 parent 3ecce2e commit 032a7a4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
4 changes: 3 additions & 1 deletion docs/api/wrapper/setData.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## setData(data)

Sets `Wrapper` `vm` data and forces update.
Sets `Wrapper` `vm` data.

setData works by merging existing properties, except for arrays which are overwritten.

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

Expand Down
2 changes: 1 addition & 1 deletion flow/modules.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ declare module 'lodash/cloneDeep' {
declare module.exports: any;
}

declare module 'lodash/merge' {
declare module 'lodash/mergeWith' {
declare module.exports: any;
}

Expand Down
6 changes: 4 additions & 2 deletions packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow

import Vue from 'vue'
import merge from 'lodash/merge'
import mergeWith from 'lodash/mergeWith'
import getSelectorTypeOrThrow from './get-selector-type'
import {
REF_SELECTOR,
Expand Down Expand Up @@ -424,7 +424,9 @@ export default class Wrapper implements BaseWrapper {
if (typeof data[key] === 'object' && data[key] !== null &&
!Array.isArray(data[key])) {
// $FlowIgnore : Problem with possibly null this.vm
const newObj = merge(this.vm[key], data[key])
const newObj = mergeWith(this.vm[key], data[key], (objValue, srcValue) => {
return Array.isArray(srcValue) ? srcValue : undefined
})
// $FlowIgnore : Problem with possibly null this.vm
this.vm.$set(this.vm, [key], newObj)
} else {
Expand Down
22 changes: 19 additions & 3 deletions test/specs/wrapper/setData.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ describeWithShallowAndMount('setData', (mountingMethod) => {
},
propB: 'b'
}
})
}),
render: () => {}
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({
Expand All @@ -179,16 +180,31 @@ describeWithShallowAndMount('setData', (mountingMethod) => {
expect(wrapper.vm.anObject.propA.prop2).to.equal('b')
})

it('sets array data properly', () => {
it('does not merge arrays', () => {
const TestComponent = {
template: '<div>{{nested.nested.nestedArray[0]}}</div>',
data: () => ({
items: [1, 2]
items: [1, 2],
nested: {
nested: {
nestedArray: [1, 2]
}
}
})
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({
items: [3]
})
expect(wrapper.vm.items).to.deep.equal([3])
wrapper.setData({
nested: {
nested: {
nestedArray: [10]
}
}
})
expect(wrapper.text()).to.equal('10')
expect(wrapper.vm.nested.nested.nestedArray).to.deep.equal([10])
})
})

0 comments on commit 032a7a4

Please sign in to comment.