import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; import { mount, enableAutoUnmount } from "@vue/test-utils"; import { nextTick } from "vue"; import ODatepicker from "@/components/datepicker/Datepicker.vue"; describe("ODatepicker", () => { enableAutoUnmount(afterEach); beforeEach(() => { // vi.useFakeTimers(); // vi.setSystemTime(new Date(2000, 0, 1)); }); afterEach(() => { vi.useRealTimers(); }); test("render correctly", () => { vi.useFakeTimers(); vi.setSystemTime(new Date(2000, 0, 1)); const wrapper = mount(ODatepicker, { props: { modelValue: new Date(2000, 0, 1) }, }); expect(!!wrapper.vm).toBeTruthy(); expect(wrapper.exists()).toBeTruthy(); expect(wrapper.attributes("data-oruga")).toBe("datepicker"); expect(wrapper.html()).toMatchSnapshot(); }); test("parses keyboard input", async () => { const wrapper = mount(ODatepicker); const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); await input.setValue("2024-04-10"); await input.trigger("keyup", { key: "Enter" }); let date = new Date(Date.UTC(2024, 3, 10)); let emits = wrapper.emitted("update:modelValue"); expect(emits).toHaveLength(1); expect(emits?.[0]).toHaveLength(1); expect(emits?.[0][0]).toBeInstanceOf(Date); expect((emits?.[0][0] as Date).toISOString()).toBe(date.toISOString()); expect(input.element.value).toBe("10/04/2024"); await input.setValue("2021-04-18"); await input.trigger("keyup", { key: "Enter" }); date = new Date(Date.UTC(2021, 3, 18)); emits = wrapper.emitted("update:modelValue"); expect(emits).toHaveLength(2); expect(emits?.[1]).toHaveLength(1); expect(emits?.[1][0]).toBeInstanceOf(Date); expect((emits?.[1][0] as Date).toISOString()).toBe(date.toISOString()); expect(input.element.value).toBe("18/04/2021"); }); test("handles invalid keyboard input", async () => { const wrapper = mount(ODatepicker, { props: { modelValue: new Date() }, }); const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); await input.setValue("not-a-date"); await input.trigger("keyup", { key: "Enter" }); let emits = wrapper.emitted("update:modelValue"); expect(emits).toHaveLength(1); expect(emits?.[0]).toHaveLength(1); expect(emits?.[0][0]).toBeUndefined(); expect(input.element.value).toBe(""); await input.setValue("21/06/wrong"); await input.trigger("keyup", { key: "Enter" }); emits = wrapper.emitted("update:modelValue"); expect(emits).toHaveLength(1); expect(input.element.value).toBe(""); }); test("keeps the overlay open when navigating a populated picker after input blur", async () => { const wrapper = mount(ODatepicker, { props: { modelValue: new Date(2024, 0, 1), active: true }, }); expect( wrapper.find(".o-datepicker__content--active").exists(), ).toBeTruthy(); const monthSelect = wrapper.find( 'select[aria-label="Select Month"]', ); monthSelect.element.focus(); await nextTick(); await monthSelect.setValue(1); expect( wrapper.find(".o-datepicker__content--active").exists(), ).toBeTruthy(); }); test("emit change-month with 0 when January is selected", async () => { const wrapper = mount(ODatepicker, { props: { modelValue: new Date(2024, 1, 10), active: true }, }); await wrapper .find('select[aria-label="Select Month"]') .setValue(0); expect(wrapper.emitted("change-month")).toStrictEqual([[0]]); expect(wrapper.emitted("change-year")).toBeUndefined(); }); test("emit change-month and change-year when wrapping to the previous year", async () => { const wrapper = mount(ODatepicker, { props: { modelValue: new Date(2024, 0, 10), active: true }, }); await wrapper .find('select[aria-label="Select Month"]') .trigger("keydown", { key: "ArrowLeft" }); expect(wrapper.emitted("change-month")).toStrictEqual([[11]]); expect(wrapper.emitted("change-year")).toStrictEqual([[2023]]); }); test("keeps the overlay open on first interaction when clicking a nav button in a populated picker", async () => { vi.useFakeTimers(); const wrapper = mount(ODatepicker, { props: { modelValue: new Date(2024, 0, 1) }, }); // picker is initially closed expect( wrapper.find(".o-datepicker__content--active").exists(), ).toBeFalsy(); // focus the input to open the picker via openOnFocus const input = wrapper.find("input"); await input.trigger("focus"); // advance past the 250ms openOnFocus delay vi.advanceTimersByTime(300); await nextTick(); // picker should now be open expect( wrapper.find(".o-datepicker__content--active").exists(), ).toBeTruthy(); // blur the input (simulates focus moving away when clicking the nav button) await input.trigger("blur"); await nextTick(); // click the previous month navigation button const prevButton = wrapper.find(".o-datepicker__header__previous"); await prevButton.trigger("click"); await nextTick(); // picker should remain open after navigating months expect( wrapper.find(".o-datepicker__content--active").exists(), ).toBeTruthy(); }); test("react accordingly when an date is selected with multiple prop", async () => { const wrapper = mount(ODatepicker, { props: { modelValue: [new Date()], multiple: true }, }); // check pre selected date const cells = wrapper.findAll(".o-datepicker__table__cell"); let selectedCells = cells.filter((c) => c.classes("o-datepicker__table__cell--selected"), ); // check only one date is selected expect(selectedCells.length).toBe(1); // check nothing got emitted yet let emits = wrapper.emitted("update:modelValue"); expect(emits).toBeUndefined(); // select another date const cellToSelect1 = cells.find( (c) => c.classes("o-datepicker__table__cell--selectable") && !c.classes("o-datepicker__table__cell--selected"), ); expect(cellToSelect1).toBeDefined(); await cellToSelect1?.trigger("click"); // check modelValue got updated emits = wrapper.emitted("update:modelValue"); expect(emits).toHaveLength(1); expect(emits?.[0][0]).toHaveLength(2); // check two dates are selected selectedCells = cells.filter((c) => c.classes("o-datepicker__table__cell--selected"), ); expect(selectedCells.length).toBe(2); // select another date const cellToSelect2 = cells.find( (c) => c.classes("o-datepicker__table__cell--selectable") && !c.classes("o-datepicker__table__cell--selected"), ); expect(cellToSelect2).toBeDefined(); await cellToSelect2?.trigger("click"); // check modelValue got updated emits = wrapper.emitted("update:modelValue"); expect(emits).toHaveLength(2); expect(emits?.[1][0]).toHaveLength(3); // check three dates are selected selectedCells = cells.filter((c) => c.classes("o-datepicker__table__cell--selected"), ); expect(selectedCells.length).toBe(3); // deselect preveus selected date await cellToSelect1?.trigger("click"); // check modelValue got updated emits = wrapper.emitted("update:modelValue"); expect(emits).toHaveLength(3); expect(emits?.[2][0]).toHaveLength(2); // check two dates are selected selectedCells = cells.filter((c) => c.classes("o-datepicker__table__cell--selected"), ); expect(selectedCells.length).toBe(2); }); test("react accordingly when an date is selected with range prop", async () => { const wrapper = mount(ODatepicker, { props: { modelValue: [], range: true }, }); // check pre selected date const cells = wrapper.findAll(".o-datepicker__table__cell"); let selectedCells = cells.filter((c) => c.classes("o-datepicker__table__cell--selected"), ); // check nothing is selected yet expect(selectedCells.length).toBe(0); // check nothing got emitted yet let emits = wrapper.emitted("update:modelValue"); expect(emits).toBeUndefined(); // select first date let cellToSelect = cells.find( (c) => c.classes("o-datepicker__table__cell--selectable") && !c.classes("o-datepicker__table__cell--selected"), ); expect(cellToSelect).toBeDefined(); await cellToSelect?.trigger("click"); // check nothing got emitted yet emits = wrapper.emitted("update:modelValue"); expect(emits).toBeUndefined(); // check one date is selected selectedCells = cells.filter((c) => c.classes("o-datepicker__table__cell--first-hovered"), ); expect(selectedCells.length).toBe(1); // select second date cellToSelect = cells.find( (c) => c.classes("o-datepicker__table__cell--selectable") && !c.classes("o-datepicker__table__cell--selected"), ); await cellToSelect?.trigger("click"); // check modelValue got updated emits = wrapper.emitted("update:modelValue"); expect(emits).toHaveLength(1); expect(emits?.[0][0]).toHaveLength(2); // check two dates are selected selectedCells = cells.filter((c) => c.classes("o-datepicker__table__cell--selected"), ); expect(selectedCells.length).not.toBe(0); // check one date is selected selectedCells = cells.filter((c) => c.classes("o-datepicker__table__cell--first-selected"), ); expect(selectedCells.length).toBe(1); // check one date is selected selectedCells = cells.filter((c) => c.classes("o-datepicker__table__cell--last-selected"), ); expect(selectedCells.length).toBe(1); // select another first date cellToSelect = cells.find( (c) => c.classes("o-datepicker__table__cell--selectable") && !c.classes("o-datepicker__table__cell--selected"), ); expect(cellToSelect).toBeDefined(); // select another date await cellToSelect?.trigger("click"); // check nothing got emitted yet emits = wrapper.emitted("update:modelValue"); expect(emits).toHaveLength(1); // select second date cellToSelect = cells.find( (c) => c.classes("o-datepicker__table__cell--selectable") && !c.classes("o-datepicker__table__cell--selected"), ); expect(cellToSelect).toBeDefined(); await cellToSelect?.trigger("click"); // check two dates are selected selectedCells = cells.filter((c) => c.classes("o-datepicker__table__cell--selected"), ); expect(selectedCells.length).not.toBe(0); // check one date is selected selectedCells = cells.filter((c) => c.classes("o-datepicker__table__cell--first-selected"), ); expect(selectedCells.length).toBe(1); // check one date is selected selectedCells = cells.filter((c) => c.classes("o-datepicker__table__cell--last-selected"), ); expect(selectedCells.length).toBe(1); // check modelValue got updated emits = wrapper.emitted("update:modelValue"); expect(emits).toHaveLength(2); expect(emits?.[1][0]).toHaveLength(2); }); test("closes overlay on date selection when stayOpen is false", async () => { const wrapper = mount(ODatepicker, { props: { modelValue: new Date(2024, 0, 1), active: true, stayOpen: false, }, }); // find and click on a selectable date cell const cells = wrapper.findAll(".o-datepicker__table__cell"); const cellToSelect = cells.find( (c) => c.classes("o-datepicker__table__cell--selectable") && !c.classes("o-datepicker__table__cell--selected"), ); expect(cellToSelect).toBeDefined(); // select a date await cellToSelect?.trigger("click"); // check that update:active event was emitted with false (overlay closes) const updateActiveEmits = wrapper.emitted("update:active"); expect(updateActiveEmits).toBeDefined(); expect(updateActiveEmits?.[0]).toStrictEqual([false]); }); test("keeps overlay open on date selection when stayOpen is true", async () => { const wrapper = mount(ODatepicker, { props: { modelValue: new Date(2024, 0, 1), active: true, stayOpen: true, }, }); // find and click on a selectable date cell const cells = wrapper.findAll(".o-datepicker__table__cell"); const cellToSelect = cells.find( (c) => c.classes("o-datepicker__table__cell--selectable") && !c.classes("o-datepicker__table__cell--selected"), ); expect(cellToSelect).toBeDefined(); // select a date await cellToSelect?.trigger("click"); // check that update:active event was NOT emitted (overlay stays open) const updateActiveEmits = wrapper.emitted("update:active"); expect(updateActiveEmits).toBeUndefined(); // verify the content still has the active class const contentElement = wrapper.find(".o-datepicker__content"); expect(contentElement.exists()).toBe(true); expect(contentElement.classes("o-datepicker__content--active")).toBe( true, ); }); describe("attribute placement", () => { test("input attributes land on the input only", () => { const wrapper = mount(ODatepicker, { attrs: { "aria-label": "Birthday", name: "when", required: true, }, }); const input = wrapper.find("input"); expect(input.attributes("aria-label")).toBe("Birthday"); expect(input.attributes("name")).toBe("when"); expect(wrapper.attributes("aria-label")).toBeUndefined(); expect(wrapper.attributes("name")).toBeUndefined(); expect(wrapper.findAll("[aria-label='Birthday']")).toHaveLength(1); }); test("class, style and data attributes land on the root only", () => { const wrapper = mount(ODatepicker, { attrs: { class: "custom", style: "margin: 1px", "data-testid": "picker", }, }); expect(wrapper.classes("custom")).toBe(true); expect(wrapper.attributes("data-testid")).toBe("picker"); expect(wrapper.attributes("data-oruga")).toBe("datepicker"); const input = wrapper.find("input"); expect(input.classes("custom")).toBe(false); expect(input.attributes("data-testid")).toBeUndefined(); expect(input.attributes("style")).toBeUndefined(); }); test("listeners are called once", async () => { const onKeydown = vi.fn(); const wrapper = mount(ODatepicker, { attrs: { onKeydown } }); await wrapper.find("input").trigger("keydown", { key: "a" }); expect(onKeydown).toHaveBeenCalledTimes(1); }); test("an inline picker keeps all attributes on the root", () => { const wrapper = mount(ODatepicker, { props: { inline: true }, attrs: { "aria-label": "Birthday" }, }); expect(wrapper.find("input").exists()).toBe(false); expect(wrapper.attributes("aria-label")).toBe("Birthday"); }); }); });