Skip to content

Commit 5c9fdf6

Browse files
committed
feat(datepicker): Let invalid open-date default to today
1 parent ddeb6f9 commit 5c9fdf6

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/components/Datepicker.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ export default {
276276
return this.initialView || this.minimumView
277277
},
278278
computedOpenDate() {
279-
const openDateOrToday = this.utils.getNewDateObject(this.openDate || null)
279+
const parsedOpenDate = this.parseValue(this.openDate)
280+
const openDateOrToday = this.utils.getNewDateObject(parsedOpenDate)
280281
const openDate = this.selectedDate || openDateOrToday
281282
282283
// If the `minimum-view` is `month` or `year`, convert `openDate` accordingly

test/unit/specs/Datepicker/openDate.spec.js

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,50 @@ describe('Datepicker shallowMounted', () => {
1212
wrapper.destroy()
1313
})
1414

15-
it("shows today's date if no open date is set", () => {
15+
it("defaults to today's date if no open date is set", () => {
1616
const today = new Date()
1717
expect(wrapper.vm.pageDate.getMonth()).toEqual(today.getMonth())
1818
expect(wrapper.vm.pageDate.getFullYear()).toEqual(today.getFullYear())
1919
})
20+
21+
it("defaults to today's date when invalid", async () => {
22+
const today = new Date()
23+
24+
await wrapper.setProps({
25+
openDate: 'invalid',
26+
})
27+
28+
expect(wrapper.vm.pageDate.getMonth()).toEqual(today.getMonth())
29+
expect(wrapper.vm.pageDate.getFullYear()).toEqual(today.getFullYear())
30+
})
31+
32+
it('accepts an instance of a date object', async () => {
33+
await wrapper.setProps({
34+
openDate: new Date(2016, 9, 12),
35+
})
36+
37+
expect(wrapper.vm.pageDate.getMonth()).toEqual(9)
38+
expect(wrapper.vm.pageDate.getFullYear()).toEqual(2016)
39+
})
40+
41+
it('accepts a string value', async () => {
42+
await wrapper.setProps({
43+
openDate: '2016-10-12',
44+
useUtc: true,
45+
})
46+
47+
expect(wrapper.vm.pageDate.getMonth()).toEqual(9)
48+
expect(wrapper.vm.pageDate.getFullYear()).toEqual(2016)
49+
})
50+
51+
it('accepts a timestamp value', async () => {
52+
await wrapper.setProps({
53+
openDate: new Date(2016, 9, 12).valueOf(),
54+
})
55+
56+
expect(wrapper.vm.pageDate.getMonth()).toEqual(9)
57+
expect(wrapper.vm.pageDate.getFullYear()).toEqual(2016)
58+
})
2059
})
2160

2261
describe('Datepicker shallowMounted with open date', () => {
@@ -35,11 +74,6 @@ describe('Datepicker shallowMounted with open date', () => {
3574
wrapper.destroy()
3675
})
3776

38-
it('accepts an instance of a date object', () => {
39-
expect(wrapper.vm.pageDate.getMonth()).toEqual(9)
40-
expect(wrapper.vm.pageDate.getFullYear()).toEqual(2016)
41-
})
42-
4377
it("sets pageTimestamp to be first day of open date's month", () => {
4478
const date = new Date(wrapper.vm.pageTimestamp)
4579
expect(wrapper.vm.openDate.valueOf()).toEqual(openDate.valueOf())

0 commit comments

Comments
 (0)