Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: overwrite arrays #652

Merged
merged 1 commit into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
})
})