From 61002d40eb9afb54c3903d6d70966965129e4d00 Mon Sep 17 00:00:00 2001 From: freakzlike Date: Mon, 15 Aug 2022 08:27:20 +0200 Subject: [PATCH] fix: keep Date objects on setData (#1720) --- src/utils.ts | 2 ++ tests/setData.spec.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/utils.ts b/src/utils.ts index f27368f40..23c10ea61 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -74,6 +74,8 @@ export const mergeDeep = ( if (Array.isArray(targetValue) && Array.isArray(sourceValue)) { target[key] = sourceValue + } else if (sourceValue instanceof Date) { + target[key] = sourceValue } else if (isObject(targetValue) && isObject(sourceValue)) { target[key] = mergeDeep(Object.assign({}, targetValue), sourceValue) } else { diff --git a/tests/setData.spec.ts b/tests/setData.spec.ts index 9d291eaf9..30cfabd55 100644 --- a/tests/setData.spec.ts +++ b/tests/setData.spec.ts @@ -187,4 +187,31 @@ describe('setData', () => { await wrapper.setData({ state: { items: ['2', '3'] } }) expect(wrapper.html()).toMatchInlineSnapshot(`"
2,3
"`) }) + + it('should keep Date object on setData', async () => { + const wrapper = mount( + { + template: '
', + props: { modelValue: Date }, + data() { + return { value: this.modelValue } + } + }, + { + props: { + modelValue: new Date('2022-08-10T12:15:54Z') + } + } + ) + + expect(wrapper.vm.value).toBeInstanceOf(Date) + expect(wrapper.vm.value!.toISOString()).toBe('2022-08-10T12:15:54.000Z') + + await wrapper.setData({ + value: new Date('2022-08-11T12:15:54Z') + }) + + expect(wrapper.vm.value).toBeInstanceOf(Date) + expect(wrapper.vm.value!.toISOString()).toBe('2022-08-11T12:15:54.000Z') + }) })