Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datepicker): make date parsing work again #882

Merged
merged 4 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
/** Parse string into date */
function onChange(value: string): void {
const date = (props.dateParser as any)(value, defaultDateParser);
mlmoravek marked this conversation as resolved.
Show resolved Hide resolved
const validDate = (d: unknown): d is Date =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about moving this to utils/helper.ts and name it isDate like the existing helper isObject ?

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]))

Check warning on line 863 in packages/oruga/src/components/datepicker/Datepicker.vue

View check run for this annotation

Codecov / codecov/patch

packages/oruga/src/components/datepicker/Datepicker.vue#L861-L863

Added lines #L861 - L863 were not covered by tests
) {
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();
});
});