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

refactor: Update setData method (address #563) #565

Merged
merged 3 commits into from
Apr 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions flow/modules.flow.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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')
})
})