Skip to content

Commit 23ff5d1

Browse files
Merge pull request #46 from reedsy/fix-opendate
Fix openDate update
2 parents 4495a39 + 612da29 commit 23ff5d1

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@reedsy/vuejs-datepicker",
3-
"version": "1.6.2-reedsy-2.1.10",
3+
"version": "1.6.2-reedsy-2.1.11",
44
"description": "A simple Vue.js datepicker component. Supports disabling of dates, inline mode, translations",
55
"keywords": [
66
"vue",

src/components/Datepicker.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
</picker-year>
160160
</div>
161161
</template>
162+
162163
<script>
163164
import en from '../locale/translations/en';
164165
import DateInput from './DateInput.vue';
@@ -322,7 +323,10 @@ export default {
322323
},
323324
openDate () {
324325
this.setPageDate();
325-
this.focusedDate = this.openDate.getTime();
326+
327+
this.focusedDate = this.openDate ?
328+
this.openDate.getTime() :
329+
new Date().getTime();
326330
},
327331
initialView () {
328332
this.setInitialView();
@@ -622,6 +626,7 @@ export default {
622626
if (this.modelValue) {
623627
this.setValue(this.modelValue);
624628
}
629+
625630
if (this.isInline) {
626631
this.setInitialView();
627632
}

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,40 @@ describe('Datepicker mounted', () => {
241241
expect(spy).toBeCalled();
242242
});
243243

244-
it('watches openDate', async () => {
245-
const wrapper = shallowMount(Datepicker, {
246-
propsData: {
247-
openDate: new Date(2018, 0, 1),
248-
},
244+
describe('openDate watcher', () => {
245+
it('should update the pageTimestamp and focusedDate', async () => {
246+
const wrapper = shallowMount(Datepicker, {
247+
propsData: {
248+
openDate: new Date(2018, 0, 1),
249+
},
250+
});
251+
expect(wrapper.vm.focusedDate).toEqual(new Date(2018, 0, 1).getTime());
252+
expect(wrapper.vm.pageTimestamp).toEqual(new Date(2018, 0, 1).getTime());
253+
const spy = jest.spyOn(wrapper.vm, 'setPageDate');
254+
await wrapper.setProps({
255+
openDate: new Date(2018, 3, 26),
256+
});
257+
expect(spy).toBeCalled();
258+
expect(wrapper.vm.focusedDate).toEqual(new Date(2018, 3, 26).getTime());
259+
expect(wrapper.vm.pageTimestamp).toEqual(new Date(2018, 3, 1).getTime());
249260
});
250-
const spy = jest.spyOn(wrapper.vm, 'setPageDate');
251-
await wrapper.setProps({
252-
openDate: new Date(2018, 3, 26),
261+
262+
it('Should not crash when openDate is cleared', async () => {
263+
const wrapper = shallowMount(Datepicker, {
264+
propsData: {
265+
openDate: new Date(2018, 0, 1),
266+
},
267+
});
268+
expect(wrapper.vm.focusedDate).toEqual(new Date(2018, 0, 1).getTime());
269+
expect(wrapper.vm.pageTimestamp).toEqual(new Date(2018, 0, 1).getTime());
270+
const spy = jest.spyOn(wrapper.vm, 'setPageDate');
271+
await wrapper.setProps({
272+
openDate: null,
273+
});
274+
expect(spy).toBeCalled();
275+
expect(wrapper.vm.focusedDate).not.toEqual(new Date(2018, 0, 1).getTime());
276+
expect(wrapper.vm.pageTimestamp).not.toEqual(new Date(2018, 0, 1).getTime());
253277
});
254-
expect(spy).toBeCalled();
255278
});
256279

257280
it('watches initialView', async () => {

0 commit comments

Comments
 (0)