Skip to content

Commit 993b293

Browse files
authored
fix(setData): allow empty objects to be set fix #1704 (#1705)
* 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
1 parent 002eb3e commit 993b293

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/test-utils/src/recursively-set-data.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ export function recursivelySetData(vm, target, data) {
55
const val = data[key]
66
const targetVal = target[key]
77

8-
if (isPlainObject(val) && isPlainObject(targetVal)) {
8+
if (
9+
isPlainObject(val) &&
10+
isPlainObject(targetVal) &&
11+
Object.keys(val).length > 0
12+
) {
913
recursivelySetData(vm, targetVal, val)
1014
} else {
1115
vm.$set(target, key, val)

test/specs/wrapper/setData.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,29 @@ describeWithShallowAndMount('setData', mountingMethod => {
320320
await wrapper.setData({ selectedDate: testDate })
321321
expect(wrapper.vm.selectedDate).toEqual(testDate)
322322
})
323+
324+
it('allows empty objects to be set', () => {
325+
const TestComponent = {
326+
data() {
327+
return {
328+
someKey: { someValue: true }
329+
}
330+
},
331+
render(h) {
332+
return h('span')
333+
}
334+
}
335+
336+
const wrapper = mountingMethod(TestComponent)
337+
338+
expect(wrapper.vm.$data).toEqual({ someKey: { someValue: true } })
339+
340+
wrapper.setData({ someKey: {} })
341+
342+
expect(wrapper.vm.$data).toEqual({ someKey: {} })
343+
344+
wrapper.setData({ someKey: { someValue: false } })
345+
346+
expect(wrapper.vm.$data).toEqual({ someKey: { someValue: false } })
347+
})
323348
})

0 commit comments

Comments
 (0)