Skip to content

Commit

Permalink
fix(datepicker): make date parsing work again (#882)
Browse files Browse the repository at this point in the history
* fix(datepicker): make date parsing work again

* fix(datepicker): add test for parsing
  • Loading branch information
blm768 committed Apr 20, 2024
1 parent b9656a6 commit 047e999
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/oruga/src/components/datepicker/Datepicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -852,13 +852,15 @@ function formatNative(value: Date | Date[]): string {
/** Parse string into date */
function onChange(value: string): void {
const date = (props.dateParser as any)(value, defaultDateParser);
const validDate = (d: unknown): d is Date =>
d instanceof Date && !isNaN(d.getTime());
if (
date &&
Array.isArray(date) &&
date.length === 2 &&
!isNaN(date[0]) &&
!isNaN(date[1])
validDate(date) ||
(Array.isArray(date) &&
date.length === 2 &&
validDate(date[0]) &&
validDate(date[1]))
) {
vmodel.value = date;
} else {
Expand Down
39 changes: 39 additions & 0 deletions packages/oruga/src/components/datepicker/tests/datepicker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { afterEach, describe, expect, test } from "vitest";
import { mount, enableAutoUnmount } from "@vue/test-utils";

import ODatepicker from "@/components/datepicker/Datepicker.vue";

describe("ODatepicker", () => {
enableAutoUnmount(afterEach);

test("parses keyboard input", () => {
const wrapper = mount(ODatepicker, { props: { readonly: false } });

const target = wrapper.find("input");
expect(target.exists()).toBeTruthy();
target.setValue("2024-04-10");

const updates = wrapper.emitted("update:modelValue");
expect(updates).toHaveLength(1);
expect(updates[0]).toHaveLength(1);
const updateValue = updates[0][0];
expect(updateValue).toBeInstanceOf(Date);
expect((updateValue as Date).toISOString()).toBe(
"2024-04-10T00:00:00.000Z",
);
});

test("handles invalid keyboard input", () => {
const wrapper = mount(ODatepicker, { props: { readonly: false } });

const target = wrapper.find("input");
expect(target.exists()).toBeTruthy();
target.setValue("not-a-date");

const updates = wrapper.emitted("update:modelValue");
expect(updates).toHaveLength(1);
expect(updates[0]).toHaveLength(1);
const updateValue = updates[0][0];
expect(updateValue).toBeNull();
});
});

0 comments on commit 047e999

Please sign in to comment.