Skip to content

Commit

Permalink
feat: support merging in setData (#565)
Browse files Browse the repository at this point in the history
closes #563
  • Loading branch information
shortdiv authored and eddyerburgh committed Apr 27, 2018
1 parent 0646796 commit 878bccd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
4 changes: 4 additions & 0 deletions flow/modules.flow.js
Expand Up @@ -12,6 +12,10 @@ declare module 'lodash/cloneDeep' {
declare module.exports: any;
}

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

declare module 'vue-template-compiler' {
declare module.exports: any;
}
Expand Down
12 changes: 10 additions & 2 deletions packages/test-utils/src/wrapper.js
@@ -1,6 +1,7 @@
// @flow

import Vue from 'vue'
import merge from 'lodash/merge'
import getSelectorTypeOrThrow from './get-selector-type'
import {
REF_SELECTOR,
Expand Down Expand Up @@ -420,8 +421,15 @@ export default class Wrapper implements BaseWrapper {
}

Object.keys(data).forEach((key) => {
// $FlowIgnore : Problem with possibly null this.vm
this.vm.$set(this.vm, [key], data[key])
if (typeof data[key] === 'object' && data[key] !== null) {
// $FlowIgnore : Problem with possibly null this.vm
const newObj = merge(this.vm[key], data[key])
// $FlowIgnore : Problem with possibly null this.vm
this.vm.$set(this.vm, [key], newObj)
} else {
// $FlowIgnore : Problem with possibly null this.vm
this.vm.$set(this.vm, [key], data[key])
}
})
}

Expand Down
46 changes: 46 additions & 0 deletions test/specs/wrapper/setData.spec.js
Expand Up @@ -132,4 +132,50 @@ describeWithShallowAndMount('setData', (mountingMethod) => {
wrapper.setData({ message: null })
expect(wrapper.text()).to.equal('There is no message yet')
})

it('should update an existing property in a data object', () => {
const TestComponent = {
data: () => ({
anObject: {
propA: {
prop1: 'a'
},
propB: 'b'
}
})
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({
anObject: {
propA: {
prop1: 'c'
}
}
})
expect(wrapper.vm.anObject.propB).to.equal('b')
expect(wrapper.vm.anObject.propA.prop1).to.equal('c')
})

it('should append a new property to an object without removing existing properties', () => {
const TestComponent = {
data: () => ({
anObject: {
propA: {
prop1: 'a'
},
propB: 'b'
}
})
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({
anObject: {
propA: {
prop2: 'b'
}
}
})
expect(wrapper.vm.anObject.propA.prop1).to.equal('a')
expect(wrapper.vm.anObject.propA.prop2).to.equal('b')
})
})

0 comments on commit 878bccd

Please sign in to comment.