import { describe, test, expect, afterEach, vi } from "vitest"; import { enableAutoUnmount, mount } from "@vue/test-utils"; import { nextTick } from "vue"; import { setTimeout } from "timers/promises"; import type { OptionsProp } from "@/composables"; import OAutocomplete from "@/components/autocomplete/Autocomplete.vue"; import type { AutocompleteOptions } from "../props"; import type { DropdownItemProps } from "@/components/dropdown/props"; describe("OAutocomplete tests", () => { enableAutoUnmount(afterEach); const OPTIONS = [ "Angular", "Angular 2", "Aurelia", "Backbone", "Ember", "jQuery", "Meteor", "Node.js", "Polymer", "React", "RxJS", "Vue.js", ]; test("render correctly", () => { const wrapper = mount(OAutocomplete, { props: { options: OPTIONS }, }); expect(!!wrapper.vm).toBeTruthy(); expect(wrapper.exists()).toBeTruthy(); expect(wrapper.attributes("data-oruga")).toBe("autocomplete"); expect(wrapper.html()).toMatchSnapshot(); }); test("has configurable menu and item tags", () => { const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, menuTag: "ul", itemTag: "li" }, }); expect(wrapper.find("ul.o-dropdown__menu").exists()).toBeTruthy(); expect(wrapper.find("li.o-dropdown__item").exists()).toBeTruthy(); }); test("has a dropdown menu hidden by default", () => { const wrapper = mount(OAutocomplete, { attachTo: document.body }); const dropdown = wrapper.find(".o-dropdown__menu"); expect(dropdown.exists()).toBeTruthy(); expect(dropdown.isVisible()).toBeFalsy(); }); test("can autocomplete with keydown", async () => { const VALUE_TYPED = "Ang"; const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, openOnFocus: true, keepOpen: false }, attachTo: document.body, }); const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); // open menu await input.trigger("focus"); await input.setValue(VALUE_TYPED); await setTimeout(); // await popover open const dropdown = wrapper.find(".o-dropdown__menu"); expect(dropdown.exists()).toBeTruthy(); expect(dropdown.isVisible()).toBeTruthy(); await input.trigger("keydown", { key: "Down" }); await input.trigger("keydown", { key: "Enter" }); expect(input.element.value).toBe(OPTIONS[0]); expect(wrapper.emitted("select")).toStrictEqual([[OPTIONS[0]]]); expect(wrapper.emitted("update:modelValue")).toStrictEqual([ [OPTIONS[0]], ]); expect(dropdown.exists()).toBeTruthy(); expect(dropdown.isVisible()).toBeFalsy(); }); test("set values correct when two-way-binded", async () => { const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, modelValue: OPTIONS[2], "onUpdate:modelValue": (modelValue) => { wrapper.setProps({ modelValue }); }, }, }); await nextTick(); // await child component rendering const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); const dropdown = wrapper.find(".o-dropdown__menu"); expect(dropdown.exists()).toBeTruthy(); expect(dropdown.isVisible()).toBeFalsy(); const optionElements = wrapper.findAll('[data-oruga="dropdown-item"]'); expect(optionElements).toHaveLength(OPTIONS.length); // check default selected value expect(input.element.value).toBe(OPTIONS[2]); expect( optionElements[2].classes("o-dropdown__item--active"), ).toBeTruthy(); expect(optionElements[2].attributes("aria-selected")).toBeTruthy(); // chenge selection wrapper.setProps({ modelValue: OPTIONS[0] }); await nextTick(); // check new selected value expect(input.element.value).toBe(OPTIONS[0]); expect( optionElements[0].classes("o-dropdown__item--active"), ).toBeTruthy(); expect(optionElements[0].attributes("aria-selected")).toBeTruthy(); }); test("close dropdown on esc", async () => { const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, openOnFocus: true }, attachTo: document.body, }); const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); // open menu await input.trigger("focus"); await setTimeout(); // await open popover const dropdown = wrapper.find(".o-dropdown__menu"); expect(dropdown.exists()).toBeTruthy(); expect(dropdown.isVisible()).toBeTruthy(); await input.trigger("keydown", { key: "Escape" }); expect(dropdown.isVisible()).toBeFalsy(); }); test("close dropdown on click outside", async () => { const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, openOnFocus: true }, attachTo: document.body, }); const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); // open menu await input.trigger("focus"); await setTimeout(); // await event handler get set const dropdown = wrapper.find(".o-dropdown__menu"); expect(dropdown.exists()).toBeTruthy(); expect(dropdown.isVisible()).toBeTruthy(); // click outside window.dispatchEvent(new Event("click")); await nextTick(); // await dom update expect(dropdown.isVisible()).toBeFalsy(); }); test("open dropdown on down key click", async () => { const wrapper = mount(OAutocomplete, { props: { options: OPTIONS }, attachTo: document.body, }); const dropdown = wrapper.find(".o-dropdown__menu"); expect(dropdown.exists()).toBeTruthy(); expect(dropdown.isVisible()).toBeFalsy(); const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); await input.trigger("focus"); await input.trigger("keydown", { key: "Down" }); await setTimeout(); // await open popover expect(dropdown.isVisible()).toBeTruthy(); }); test("manages tab pressed as expected", async () => { const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, openOnFocus: true, keepFirst: true }, attachTo: document.body, }); const dropdown = wrapper.find(".o-dropdown__menu"); expect(dropdown.exists()).toBeTruthy(); expect(dropdown.isVisible()).toBeFalsy(); const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); await input.trigger("keydown", { key: "Tab" }); expect(dropdown.isVisible()).toBeFalsy(); await input.trigger("focus"); await input.trigger("keydown", { key: "Tab" }); expect(input.element.value).toBe(""); }); test("can openOnFocus and keepFirst", async () => { const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, openOnFocus: true, keepFirst: true, }, attachTo: document.body, }); const dropdown = wrapper.find(".o-dropdown__menu"); expect(dropdown.exists()).toBeTruthy(); expect(dropdown.isVisible()).toBeFalsy(); const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); await input.trigger("focus"); await setTimeout(); // await open popover expect(dropdown.isVisible()).toBeTruthy(); const firstItem = dropdown.find('[data-oruga="dropdown-item"]'); expect(firstItem.exists()).toBeTruthy(); expect(firstItem.classes("o-dropdown__item--focused")).toBeTruthy(); }); test("keepFirst works as input changes", async () => { const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, openOnFocus: true, keepFirst: true, debounce: 0, }, attachTo: document.body, }); const dropdown = wrapper.find(".o-dropdown__menu"); expect(dropdown.exists()).toBeTruthy(); expect(dropdown.isVisible()).toBeFalsy(); const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); await input.trigger("focus"); await setTimeout(); // await popover open expect(dropdown.isVisible()).toBeTruthy(); const focusedItem = wrapper.find(".o-dropdown__item--focused"); expect(focusedItem.exists()).toBeTruthy(); expect(focusedItem.text()).toContain(OPTIONS[0]); await input.setValue("v"); await setTimeout(); // await debounce timer await nextTick(); const updatedFocusedItem = wrapper.find(".o-dropdown__item--focused"); expect(updatedFocusedItem.exists()).toBeTruthy(); expect(updatedFocusedItem.text()).toContain("Vue.js"); }); test("do not open when openOnFocus and empty options", async () => { const wrapper = mount(OAutocomplete, { props: { options: [], openOnFocus: true }, attachTo: document.body, }); const dropdown = wrapper.find(".o-dropdown__menu"); expect(dropdown.exists()).toBeTruthy(); expect(dropdown.isVisible()).toBeFalsy(); const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); await input.trigger("focus"); expect(dropdown.isVisible()).toBeFalsy(); }); describe("verify events", () => { test("check emit focus and blur event correctly", async () => { const wrapper = mount(OAutocomplete); const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); await input.trigger("focus"); await input.trigger("blur"); expect(wrapper.emitted("focus")).toHaveLength(1); expect(wrapper.emitted("blur")).toHaveLength(1); }); test("can emit input, focus and blur events", async () => { const VALUE_TYPED = "test"; const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, debounce: 0 }, }); const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); // open menu await input.trigger("focus"); expect(wrapper.emitted("focus")).toHaveLength(1); await input.setValue(VALUE_TYPED); await input.trigger("input"); await setTimeout(); // await input debounce expect(wrapper.emitted("input")).toHaveLength(1); expect(wrapper.emitted("input")?.[0]).toContain(VALUE_TYPED); expect(wrapper.emitted("update:input")).toHaveLength(1); expect(wrapper.emitted("update:input")?.[0]).toContain(VALUE_TYPED); await input.trigger("blur"); expect(wrapper.emitted("blur")).toBeDefined(); }); }); describe("filtering", () => { test("do not sort when `backend-filtering` is given", async () => { vi.useFakeTimers(); const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, backendFiltering: true, active: true, }, attachTo: document.body, }); await nextTick(); // await child component rendering const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); let optionElements = wrapper.findAll( '[data-oruga="dropdown-item"]', ); expect(optionElements).toHaveLength(OPTIONS.length); optionElements.forEach((option) => { expect(option.attributes("disabled")).toBeUndefined(); }); await input.setValue(OPTIONS[2]); vi.runAllTimers(); // await debounce input handler await nextTick(); // await child component rendering // check that there are no out filtered elements optionElements = wrapper.findAll('[data-oruga="dropdown-item"]'); expect(optionElements).toHaveLength(OPTIONS.length); optionElements.forEach((option) => { expect(option.isVisible()).toBeTruthy(); expect(option.attributes("disabled")).toBeUndefined(); }); vi.useRealTimers(); }); test("show available options correctly when filter is given", async () => { vi.useFakeTimers(); const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, active: true }, attachTo: document.body, }); await nextTick(); // await child component rendering const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); let optionElements = wrapper.findAll( '[data-oruga="dropdown-item"]', ); expect(optionElements).toHaveLength(OPTIONS.length); optionElements.forEach((option) => { expect(option.attributes("disabled")).toBeUndefined(); }); await input.setValue("j"); vi.runAllTimers(); // await debounce input handler await nextTick(); // await child component rendering // check that there are no out filtered elements optionElements = wrapper.findAll('[data-oruga="dropdown-item"]'); expect(optionElements).toHaveLength(OPTIONS.length); optionElements.forEach((option) => { expect(option.isVisible()).toBe( option.text().toLowerCase().includes("j"), ); expect(option.attributes("disabled")).toBeUndefined(); }); vi.useRealTimers(); }); }); describe("clear button", () => { test("clear button does not exist when the search input is empty", () => { const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, modelValue: "", clearable: true, }, }); const subject = wrapper.find(".o-icon"); expect(subject.exists()).toBeFalsy(); }); test("clears search input text when clear button gets clicked", async () => { const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, modelValue: OPTIONS[5], clearable: true, }, }); await nextTick(); // await child component rendering const input = wrapper.find("input"); expect(input.exists()).toBeTruthy(); expect(input.element.value).toBe(OPTIONS[5]); const icon = wrapper.find(".o-icon"); expect(icon.exists()).toBeTruthy(); await icon.trigger("click"); expect(input.element.value).toStrictEqual(""); }); test("clear button does not appear when clearable property is not set to true", () => { const wrapper = mount(OAutocomplete, { props: { options: OPTIONS, modelValue: OPTIONS[5], clearable: false, }, }); const subject = wrapper.find(".o-icon"); expect(subject.exists()).toBeFalsy(); }); }); describe("header & footer", () => { test("can emit select-header by keyboard and click", async () => { const wrapper = mount(OAutocomplete, { props: { openOnFocus: true, keepOpen: true, selectableHeader: true, selectableFooter: true, }, slots: { header: "