Skip to content

Commit

Permalink
fix(setData): allow empty objects to be set fix #1704 (#1705)
Browse files Browse the repository at this point in the history
* fix(setData): allow empty objects to be set

fixes #1704 

This is a tentative fix to show one possible solution, I can clean it up if you think it makes sense

* fix formatting

* test
  • Loading branch information
Haroenv committed Oct 7, 2020
1 parent 002eb3e commit 993b293
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/test-utils/src/recursively-set-data.js
Expand Up @@ -5,7 +5,11 @@ export function recursivelySetData(vm, target, data) {
const val = data[key]
const targetVal = target[key]

if (isPlainObject(val) && isPlainObject(targetVal)) {
if (
isPlainObject(val) &&
isPlainObject(targetVal) &&
Object.keys(val).length > 0
) {
recursivelySetData(vm, targetVal, val)
} else {
vm.$set(target, key, val)
Expand Down
25 changes: 25 additions & 0 deletions test/specs/wrapper/setData.spec.js
Expand Up @@ -320,4 +320,29 @@ describeWithShallowAndMount('setData', mountingMethod => {
await wrapper.setData({ selectedDate: testDate })
expect(wrapper.vm.selectedDate).toEqual(testDate)
})

it('allows empty objects to be set', () => {
const TestComponent = {
data() {
return {
someKey: { someValue: true }
}
},
render(h) {
return h('span')
}
}

const wrapper = mountingMethod(TestComponent)

expect(wrapper.vm.$data).toEqual({ someKey: { someValue: true } })

wrapper.setData({ someKey: {} })

expect(wrapper.vm.$data).toEqual({ someKey: {} })

wrapper.setData({ someKey: { someValue: false } })

expect(wrapper.vm.$data).toEqual({ someKey: { someValue: false } })
})
})

0 comments on commit 993b293

Please sign in to comment.