import { afterEach, describe, expect, test } from "vitest"; import { enableAutoUnmount, mount } from "@vue/test-utils"; import { nextTick, type ComponentPublicInstance, type PropType } from "vue"; import { setTimeout } from "timers/promises"; import OTree from "@/components/tree/Tree.vue"; import OTreeItem from "@/components/tree/TreeItem.vue"; import type { OptionsProp } from "@/composables"; import type { TreeItemProps, TreeOptions } from "../props"; describe("OTree tests", () => { enableAutoUnmount(afterEach); const options: TreeOptions = [ { label: "Documents", icon: "folder", options: [ { label: "Work", value: "work", icon: "cog", options: [ { label: "Expenses.doc", value: "expenses", icon: "file", }, { label: "Resume.doc", value: "resume", icon: "file", }, ], }, { label: "Home", value: "home", icon: "home", options: [ { label: "Invoices.txt", value: "invoices", icon: "file", }, ], }, ], }, { label: "Events", value: "events", icon: "calendar", options: [ { label: "Meeting", value: "meeting", icon: "calendar-plus", }, { label: "Product Launch", value: "product-launch", icon: "calendar-plus", }, { label: "Report Review", value: "report-review", icon: "calendar-plus", }, ], }, { label: "Movies", value: "movies", icon: "star", options: [ { label: "Al Pacino", value: "al-pacion", icon: "star", options: [ { label: "Scarface", value: "scarface", icon: "video", }, { label: "Serpico", value: "serpico", icon: "video", }, ], }, { label: "Robert De Niro", value: "robert-de-niro", icon: "star", options: [ { label: "Goodfellas", value: "goodfellas", icon: "video", }, { label: "Untouchables", value: "untouchables", icon: "video", }, ], }, ], }, ]; const componentWrapper = { components: { OTree, OTreeItem, }, props: { items: { type: Array as PropType>, required: true, }, }, template: ` `, }; test("render correctly", () => { const wrapper = mount(OTree); expect(!!wrapper.vm).toBeTruthy(); expect(wrapper.exists()).toBeTruthy(); expect(wrapper.attributes("data-oruga")).toBe("tree"); expect(wrapper.html()).toMatchSnapshot(); expect(wrapper.classes("o-tree")).toBeTruthy(); }); test("render empty list correctly", () => { const wrapper = mount(OTree, { props: { options: [] } }); expect(wrapper.attributes("data-oruga")).toBe("tree"); expect(wrapper.html()).toMatchSnapshot(); const list = wrapper.find(".o-tree__list"); expect(list.exists()).toBeTruthy(); const items = list.findAll("*"); expect(items.length).toBe(1); }); test("render correctly with options", () => { const items = [ { label: "label1", icon: "user" }, { label: "label2", icon: "mobile" }, ]; const wrapper = mount(componentWrapper, { props: { items }, }); expect(wrapper.attributes("data-oruga")).toBe("tree"); expect(wrapper.html()).toMatchSnapshot(); const itemComps = wrapper.findAllComponents(OTreeItem); expect(itemComps.length).toBe(items.length); items.forEach((value, idx) => { expect(itemComps[idx].attributes("data-oruga")).toBe("tree-item"); expect(itemComps[idx].text()).toBe(value.label); expect(itemComps[idx].classes()).toContain("o-tree__item"); }); }); test("render correctly with deep options", () => { const wrapper = mount(OTree, { props: { options } }); expect(!!wrapper.vm).toBeTruthy(); expect(wrapper.exists()).toBeTruthy(); expect(wrapper.attributes("data-oruga")).toBe("tree"); expect(wrapper.html()).toMatchSnapshot(); expect(wrapper.classes("o-tree")).toBeTruthy(); const items = wrapper.findAllComponents(OTreeItem); expect(items.length).toBe(17); items.forEach((value, idx) => { expect(items[idx].attributes("data-oruga")).toBe("tree-item"); expect(items[idx].classes()).toContain("o-tree__item"); }); }); describe("verify collapsable", () => { test("react accordingly when tree is not collapsable", async () => { const wrapper = mount(OTree, { props: { options, collapsable: false }, }); const items = wrapper.findAllComponents(OTreeItem); expect(items.length).toBe(17); const itemOne = items[0]; // Documents item expect(itemOne.find(".o-tree__item-label").text()).toBe( "Documents", ); const itemTwo = items[6]; // Events item expect(itemTwo.find(".o-tree__item-label").text()).toBe("Events"); const itemThree = items[10]; // Movies item expect(itemThree.find(".o-tree__item-label").text()).toBe("Movies"); await itemOne.trigger("click"); expect(itemOne.emitted("open")).toBeUndefined(); expect(itemOne.emitted("close")).toBeUndefined(); await itemTwo.trigger("click"); expect(itemTwo.emitted("open")).toBeUndefined(); expect(itemTwo.emitted("close")).toBeUndefined(); await itemThree.trigger("click"); expect(itemThree.emitted("open")).toBeUndefined(); expect(itemThree.emitted("close")).toBeUndefined(); }); test("react accordingly when tree is collapsable and has no toggle icon", async () => { const wrapper = mount(OTree, { props: { options, collapsable: true, toggleIcon: "" }, }); await nextTick(); // await child component rendering const items = wrapper.findAllComponents(OTreeItem); expect(items.length).toBe(17); // Documents item const itemOne = items[0]; const itemOneLabel = itemOne.find(".o-tree__item-label"); expect(itemOneLabel.exists()).toBeTruthy(); const itemOneToggle = itemOneLabel.find( ".o-tree__item-toggle-icon", ); expect(itemOneToggle.exists()).toBeFalsy(); expect(itemOneLabel.text()).toBe("Documents"); // Events item const itemTwo = items[6]; const itemTwoLabel = itemTwo.find(".o-tree__item-label"); expect(itemTwoLabel.exists()).toBeTruthy(); const itemTwoToggle = itemTwoLabel.find( ".o-tree__item-toggle-icon", ); expect(itemTwoToggle.exists()).toBeFalsy(); expect(itemTwoLabel.text()).toBe("Events"); // Movies item const itemThree = items[10]; const itemThreeLabel = itemThree.find(".o-tree__item-label"); expect(itemThreeLabel.exists()).toBeTruthy(); const itemThreeToggle = itemThreeLabel.find( ".o-tree__item-toggle-icon", ); expect(itemThreeToggle.exists()).toBeFalsy(); expect(itemThreeLabel.text()).toBe("Movies"); // open tree sections await itemOneLabel.trigger("click"); expect(itemOne.emitted("open")).toBeDefined(); expect(itemOne.emitted("close")).toBeUndefined(); await itemTwoLabel.trigger("click"); expect(itemTwo.emitted("open")).toBeDefined(); expect(itemTwo.emitted("close")).toBeUndefined(); await itemThreeLabel.trigger("click"); expect(itemThree.emitted("open")).toBeDefined(); expect(itemThree.emitted("close")).toBeUndefined(); // close all tree sections await itemOneLabel.trigger("click"); expect(itemOne.emitted("open")).toBeDefined(); expect(itemOne.emitted("close")).toBeDefined(); await itemTwoLabel.trigger("click"); expect(itemTwo.emitted("open")).toBeDefined(); expect(itemTwo.emitted("close")).toBeDefined(); await itemThreeLabel.trigger("click"); expect(itemThree.emitted("open")).toBeDefined(); expect(itemThree.emitted("close")).toBeDefined(); }); test("react accordingly when tree is collapsable and has toggle icon", async () => { const wrapper = mount(OTree, { props: { options, collapsable: true, toggleIcon: "chevron-left", }, }); await nextTick(); // await child component rendering const items = wrapper.findAllComponents(OTreeItem); expect(items.length).toBe(17); // Documents item const itemOne = items[0]; const itemOneLabel = itemOne.find(".o-tree__item-label"); expect(itemOneLabel.exists()).toBeTruthy(); const itemOneToggle = itemOneLabel.find( ".o-tree__item-toggle-icon", ); expect(itemOneToggle.exists()).toBeTruthy(); expect(itemOneLabel.text()).toBe("Documents"); // Events item const itemTwo = items[6]; const itemTwoLabel = itemTwo.find(".o-tree__item-label"); expect(itemTwoLabel.exists()).toBeTruthy(); const itemTwoToggle = itemTwoLabel.find( ".o-tree__item-toggle-icon", ); expect(itemTwoToggle.exists()).toBeTruthy(); expect(itemTwoLabel.text()).toBe("Events"); // Movies item const itemThree = items[10]; const itemThreeLabel = itemThree.find(".o-tree__item-label"); expect(itemThreeLabel.exists()).toBeTruthy(); const itemThreeToggle = itemThreeLabel.find( ".o-tree__item-toggle-icon", ); expect(itemThreeToggle.exists()).toBeTruthy(); expect(itemThreeLabel.text()).toBe("Movies"); // open tree sections await itemOneToggle.trigger("click"); expect(itemOne.emitted("open")).toBeDefined(); expect(itemOne.emitted("close")).toBeUndefined(); await itemTwoToggle.trigger("click"); expect(itemTwo.emitted("open")).toBeDefined(); expect(itemTwo.emitted("close")).toBeUndefined(); await itemThreeToggle.trigger("click"); expect(itemThree.emitted("open")).toBeDefined(); expect(itemThree.emitted("close")).toBeUndefined(); // close all tree sections await itemOneToggle.trigger("click"); expect(itemOne.emitted("open")).toBeDefined(); expect(itemOne.emitted("close")).toBeDefined(); await itemTwoToggle.trigger("click"); expect(itemTwo.emitted("open")).toBeDefined(); expect(itemTwo.emitted("close")).toBeDefined(); await itemThreeToggle.trigger("click"); expect(itemThree.emitted("open")).toBeDefined(); expect(itemThree.emitted("close")).toBeDefined(); }); test("react accordingly with keyboard navigation", async () => { const wrapper = mount(OTree, { props: { options, collapsable: true, toggleIcon: "chevron-left", }, }); const list = wrapper.find("ul"); expect(list.exists()).toBeTruthy(); const items = wrapper.findAllComponents(OTreeItem); expect(items.length).toBe(17); // first item (Documents) should be focusable expect(items[0].attributes("tabindex")).toBe("0"); // focus first item await items[0].trigger("focusin"); // first item is focused expect(items[0].classes("o-tree__item--focused")).toBeTruthy(); // press ArrowDown -> next root element (Events at index 6) await list.trigger("keydown", { code: "ArrowDown", key: "Down" }); // Events element is focused expect(items[6].attributes("tabindex")).toBe("0"); expect(items[6].classes("o-tree__item--focused")).toBeTruthy(); // press ArrowRight -> Events opens await list.trigger("keydown", { code: "ArrowRight", key: "Right", }); expect(items[6].emitted("open")).toBeDefined(); // first child of Events should become visible after expand expect(items[7].attributes("aria-hidden")).toBe("false"); // Events element is still focused expect(items[6].attributes("tabindex")).toBe("0"); expect(items[6].classes("o-tree__item--focused")).toBeTruthy(); // press ArrowDown -> first child of Events (Meeting at index 7) await list.trigger("keydown", { code: "ArrowDown", key: "Down" }); // first child of Events is focused expect(items[7].attributes("tabindex")).toBe("0"); expect(items[7].classes("o-tree__item--focused")).toBeTruthy(); // press ArrowUp -> back to Events (index 6) await list.trigger("keydown", { code: "ArrowUp", key: "Up" }); // Events element is focused again expect(items[6].attributes("tabindex")).toBe("0"); expect(items[6].classes("o-tree__item--focused")).toBeTruthy(); // Press ArrowUp -> back to Documents (index 0) await list.trigger("keydown", { code: "ArrowUp", key: "Up" }); // first item is focused again expect(items[0].attributes("tabindex")).toBe("0"); expect(items[0].classes("o-tree__item--focused")).toBeTruthy(); }); }); describe("verify selectable", () => { const options: TreeOptions = [ { label: "New York", value: "NY" }, { label: "Rome", value: "RM" }, { label: "London", value: "LDN" }, { label: "Istanbul", value: "IST" }, { label: "Paris", value: "PRS" }, ]; test("react accordingly when new item is selected", async () => { const wrapper = mount(OTree, { props: { options, selectable: true, modelValue: options[0].value, }, }); expect(wrapper.classes("o-tree--selectable")).toBeTruthy(); const items = wrapper.findAll(".o-tree__item"); expect(items.length).toBe(options.length); expect(items[0].classes("o-tree__item--selected")).toBeTruthy(); expect(items[1].classes("o-tree__item--selected")).toBeFalsy(); expect(items[2].classes("o-tree__item--selected")).toBeFalsy(); const itemLabel = items[2].find(".o-tree__item-label"); expect(itemLabel.exists()).toBeTruthy(); await itemLabel.trigger("click"); expect(items[0].classes("o-tree__item--selected")).toBeFalsy(); expect(items[1].classes("o-tree__item--selected")).toBeFalsy(); expect(items[2].classes("o-tree__item--selected")).toBeTruthy(); expect(wrapper.emitted("update:modelValue")).toHaveLength(1); expect(wrapper.emitted("update:modelValue")?.[0][0]).toBe( options[2].value, ); expect(wrapper.emitted("select")).toHaveLength(1); expect(wrapper.emitted("select")?.[0][0]).toBe(options[2].value); }); test("react accordingly when item is clicked without selectable", async () => { const optionsWithChildren: TreeOptions = [ { label: "Documents", options: [ { label: "Work", value: "work" }, { label: "Home", value: "home" }, ], }, { label: "Events", value: "events" }, ]; const wrapper = mount(OTree, { props: { options: optionsWithChildren, toggleIcon: "chevron", selectable: false, }, }); const items = wrapper.findAll('[role="treeitem"]'); expect(items.length).toBe(4); expect(items[0].attributes("aria-hidden")).toBe("false"); expect(items[1].attributes("aria-hidden")).toBe("true"); expect(items[2].attributes("aria-hidden")).toBe("true"); const itemLabel = items[0].find(".o-tree__item-label"); expect(itemLabel.exists()).toBeTruthy(); await itemLabel.trigger("click"); expect(items[1].attributes("aria-hidden")).toBe("false"); expect(items[2].attributes("aria-hidden")).toBe("false"); expect(items[3].attributes("aria-hidden")).toBe("false"); expect(wrapper.emitted("update:modelValue")).toBeUndefined(); }); test("react accordingly when same item is deselected", async () => { const wrapper = mount(OTree, { props: { options, modelValue: options[0].value, selectable: true, }, }); const items = wrapper.findAll(".o-tree__item"); expect(items.length).toBe(options.length); expect(items[0].classes("o-tree__item--selected")).toBeTruthy(); expect(items[1].classes("o-tree__item--selected")).toBeFalsy(); expect(items[2].classes("o-tree__item--selected")).toBeFalsy(); const itemLabel = items[0].find(".o-tree__item-label"); expect(itemLabel.exists()).toBeTruthy(); await itemLabel.trigger("click"); expect(items[0].classes("o-tree__item--selected")).toBeFalsy(); expect(items[1].classes("o-tree__item--selected")).toBeFalsy(); expect(items[2].classes("o-tree__item--selected")).toBeFalsy(); expect(wrapper.emitted("update:modelValue")).toHaveLength(1); expect(wrapper.emitted("update:modelValue")?.[0][0]).toBe( undefined, ); expect(wrapper.emitted("select")).toBeUndefined(); }); test("react accordingly when an item is selected with multiple prop", async () => { const wrapper = mount(OTree, { props: { options, selectable: true, multiple: true, }, }); expect(wrapper.classes("o-tree--multiple")).toBeTruthy(); const items = wrapper.findAll(".o-tree__item"); expect(items.length).toBe(options.length); expect(items[0].classes("o-tree__item--selected")).toBeFalsy(); expect(items[1].classes("o-tree__item--selected")).toBeFalsy(); expect(items[2].classes("o-tree__item--selected")).toBeFalsy(); let itemLabel = items[0].find(".o-tree__item-label"); expect(itemLabel.exists()).toBeTruthy(); await itemLabel.trigger("click"); expect(items[0].classes("o-tree__item--selected")).toBeTruthy(); expect(items[1].classes("o-tree__item--selected")).toBeFalsy(); expect(items[2].classes("o-tree__item--selected")).toBeFalsy(); expect(wrapper.emitted("select")).toHaveLength(1); expect(wrapper.emitted("select")?.[0]).toContain(options[0].value); expect(wrapper.emitted("update:modelValue")).toHaveLength(1); expect(wrapper.emitted("update:modelValue")?.[0][0]).toHaveLength( 1, ); expect(wrapper.emitted("update:modelValue")?.[0][0]).toContain( options[0].value, ); itemLabel = items[2].find(".o-tree__item-label"); expect(itemLabel.exists()).toBeTruthy(); await itemLabel.trigger("click"); expect(items[0].classes("o-tree__item--selected")).toBeTruthy(); expect(items[1].classes("o-tree__item--selected")).toBeFalsy(); expect(items[2].classes("o-tree__item--selected")).toBeTruthy(); expect(wrapper.emitted("select")).toHaveLength(2); expect(wrapper.emitted("select")?.[1]).toContain(options[2].value); expect(wrapper.emitted("update:modelValue")).toHaveLength(2); expect(wrapper.emitted("update:modelValue")?.[1][0]).toHaveLength( 2, ); expect(wrapper.emitted("update:modelValue")?.[1][0]).toContain( options[0].value, ); expect(wrapper.emitted("update:modelValue")?.[1][0]).toContain( options[2].value, ); itemLabel = items[0].find(".o-tree__item-label"); expect(itemLabel.exists()).toBeTruthy(); await itemLabel.trigger("click"); expect(items[0].classes("o-tree__item--selected")).toBeFalsy(); expect(items[1].classes("o-tree__item--selected")).toBeFalsy(); expect(items[2].classes("o-tree__item--selected")).toBeTruthy(); expect(wrapper.emitted("select")).toHaveLength(2); expect(wrapper.emitted("update:modelValue")).toHaveLength(3); expect(wrapper.emitted("update:modelValue")?.[2][0]).toHaveLength( 1, ); expect(wrapper.emitted("update:modelValue")?.[2][0]).not.toContain( options[0].value, ); expect(wrapper.emitted("update:modelValue")?.[2][0]).toContain( options[2].value, ); }); test("react accordingly when tree is disabled", async () => { const wrapper = mount(OTree, { props: { options: options, disabled: true }, }); expect(wrapper.classes("o-tree--disabled")).toBeTruthy(); const items = wrapper.findAll(".o-tree__item"); expect(items.length).toBe(options.length); items.forEach((item) => { expect(item.classes("o-tree__item--selected")).toBeFalsy(); }); await items[0].trigger("click"); items.forEach((item) => { expect(item.classes("o-tree__item--selected")).toBeFalsy(); }); expect(wrapper.emitted("update:modelValue")).toBeUndefined(); expect(wrapper.emitted("select")).toBeUndefined(); expect(wrapper.classes("o-tree--disabled")).toBeTruthy(); }); test("react accordingly when item has disabled prop", () => { const items: TreeOptions = [ { label: "label1", disabled: true }, { label: "label2", disabled: false }, ]; const wrapper = mount(componentWrapper, { props: { items } }); const itemComps = wrapper.findAllComponents(OTreeItem); expect(itemComps.length).toBe(items.length); const treeItems = wrapper.findAll('[role="treeitem"]'); expect(treeItems.length).toBe(items.length); expect(treeItems[0].attributes("aria-disabled")).toBe("true"); expect(itemComps[0].classes()).toContain("o-tree__item--disabled"); expect(itemComps[1].attributes("aria-disabled")).toBe("false"); }); test("react accordingly when selected with keydown", async () => { const wrapper = mount(OTree, { props: { options, selectable: true }, }); const list = wrapper.find("ul"); expect(list.exists()).toBeTruthy(); await list.trigger("keydown", { code: "ArrowDown", key: "Down" }); await list.trigger("keydown", { code: "Enter", key: "Enter" }); expect(wrapper.emitted("select")).toStrictEqual([ [options[0].value], ]); expect(wrapper.emitted("update:modelValue")).toStrictEqual([ [options[0].value], ]); }); }); describe("verify checkable", () => { const checkableOptions: TreeOptions = [ { label: "Item A", value: "a" }, { label: "Item B", value: "b" }, { label: "Item C", value: "c" }, ]; test("renders checkboxes and updates selection when checked", async () => { const wrapper = mount(OTree, { props: { options: checkableOptions, checkable: true, modelValue: checkableOptions[0].value, }, }); const items = wrapper.findAll('[data-oruga="tree-item"]'); expect(items.length).toBe(checkableOptions.length); const checkboxes = wrapper.findAll('[data-oruga="checkbox"]'); expect(checkboxes.length).toBe(checkableOptions.length); const firstCheckbox = checkboxes[0].find( 'input[type="checkbox"]', ); expect(firstCheckbox.exists()).toBeTruthy(); expect(firstCheckbox.element.checked).toBe(true); const lastCheckbox = checkboxes[2].find( 'input[type="checkbox"]', ); expect(lastCheckbox.exists()).toBeTruthy(); expect(lastCheckbox.element.checked).toBe(false); await lastCheckbox.trigger("click"); expect(items[2].classes("o-tree__item--selected")).toBeTruthy(); expect(wrapper.emitted("update:modelValue")).toHaveLength(1); expect(wrapper.emitted("update:modelValue")?.[0][0]).toBe( checkableOptions[2].value, ); expect(wrapper.emitted("select")).toHaveLength(1); expect(wrapper.emitted("select")?.[0][0]).toBe( checkableOptions[2].value, ); }); test("syncs checkboxes with multiple selection", async () => { const wrapper = mount(OTree, { props: { options: checkableOptions, checkable: true, selectable: true, multiple: true, }, }); const items = wrapper.findAll('[data-oruga="tree-item"]'); const checkboxes = wrapper.findAll('[data-oruga="checkbox"]'); expect(checkboxes.length).toBe(items.length); await checkboxes[0].trigger("click"); await checkboxes[2].trigger("click"); expect(items[0].classes("o-tree__item--selected")).toBeTruthy(); expect(items[2].classes("o-tree__item--selected")).toBeTruthy(); expect(wrapper.emitted("update:modelValue")).toHaveLength(2); expect(wrapper.emitted("update:modelValue")?.[1][0]).toHaveLength( 2, ); expect(wrapper.emitted("update:modelValue")?.[1][0]).toContain( checkableOptions[0].value, ); expect(wrapper.emitted("update:modelValue")?.[1][0]).toContain( checkableOptions[2].value, ); }); test("handles deep nested tree items with checkable enabled", async () => { const wrapper = mount(OTree, { props: { options: options, checkable: true, collapsable: false, modelValue: options[0].value, }, }); const items = wrapper.findAll('[data-oruga="tree-item"]'); expect(items.length).toBe(17); // length of all deep nested option items const checkboxes = wrapper.findAll('[data-oruga="checkbox"]'); expect(checkboxes.length).toBe(17); // length of all deep nested option items const nestedCheckbox = checkboxes[2].find( 'input[type="checkbox"]', ); expect(nestedCheckbox.exists()).toBeTruthy(); expect(nestedCheckbox.element.checked).toBe(false); await nestedCheckbox.trigger("click"); expect(items[2].classes("o-tree__item--selected")).toBeTruthy(); expect(wrapper.emitted("update:modelValue")).toHaveLength(1); expect(wrapper.emitted("update:modelValue")?.[0][0]).toBe( "expenses", ); expect(wrapper.emitted("select")).toHaveLength(1); expect(wrapper.emitted("select")?.[0][0]).toBe("expenses"); }); }); describe("verify filterable", () => { test("should have correct custom icon", () => { const wrapper = mount(OTree, { props: { options, filterable: true, filterIcon: "pi pi-discord", }, }); const input = wrapper.find('[data-oruga="input"]'); expect(input.exists()).toBeTruthy(); const icon = input.find('[data-oruga="icon"] i'); expect(icon.exists()).toBeTruthy(); expect(icon.classes()).toContain("pi-discord"); }); test("label the filter input", async () => { const wrapper = mount(OTree, { props: { options, filterable: true }, }); const input = wrapper.find('[data-oruga="input"] input'); expect(input.attributes("aria-label")).toBe("tree filter input"); await wrapper.setProps({ ariaFilterLabel: "Filter the tree" }); expect(input.attributes("aria-label")).toBe("Filter the tree"); }); test("should correctly filter tree items", async () => { const wrapper = mount(OTree, { props: { options, filterable: true }, }); const filterInput = wrapper.find('[data-oruga="input"] input'); expect(filterInput.exists()).toBeTruthy(); await filterInput.setValue("res"); await filterInput.trigger("input"); await setTimeout(500); // await input debounce const items = wrapper.findAll('[ role="treeitem"]'); const visibleItems = items .filter((item) => item.attributes("aria-hidden") === "false") .map((item) => item.find(".o-tree__item-label").text()); expect(visibleItems).toHaveLength(3); expect(visibleItems).toStrictEqual([ "Documents", "Work", "Resume.doc", ]); expect(wrapper.emitted("filter")).toBeDefined(); expect(wrapper.emitted("filter")?.[0][0]).toContain("res"); }); test("should apply a custom filter function", async () => { const wrapper = mount(OTree, { props: { options, filterable: true, filter: (value: unknown, filterValue: string) => !filterValue || String((value as string) ?? "") .toLowerCase() .includes(filterValue.toLowerCase()) || value === undefined, }, }); const filterInput = wrapper.find('[data-oruga="input"] input'); await filterInput.setValue("res"); await filterInput.trigger("input"); await setTimeout(500); // await input debounce const visibleItems = wrapper .findAll('[role="treeitem"]') .filter((item) => item.attributes("aria-hidden") === "false") .map((item) => item.find(".o-tree__item-label").text()); expect(visibleItems).toContain("Documents"); expect(visibleItems).toContain("Resume.doc"); }); test("should correctly filter grouped items", async () => { const wrapper = mount(OTree, { props: { filterable: true, options: [ { label: "Germany", value: "DE", options: [ { label: "Berlin", value: "Berlin" }, { label: "Frankfurt", value: "Frankfurt" }, { label: "Hamburg", value: "Hamburg" }, { label: "Munich", value: "Munich" }, ], }, { label: "USA", value: "US", options: [ { label: "Chicago", value: "Chicago" }, { label: "Los Angeles", value: "Los Angeles" }, { label: "New York", value: "New York" }, { label: "San Francisco", value: "San Francisco", }, ], }, ], }, }); const filterInput = wrapper.find('[data-oruga="input"] input'); expect(filterInput.exists()).toBeTruthy(); await filterInput.setValue("ch"); await filterInput.trigger("input"); await setTimeout(500); // await input debounce const visibleItems = wrapper .findAll('[role="treeitem"]') .filter((item) => item.attributes("aria-hidden") === "false") .map((item) => item.find(".o-tree__item-label").text()); expect(visibleItems).toStrictEqual(["USA", "Chicago"]); expect(wrapper.emitted("filter")?.[0][0]).toContain("ch"); }); test("should keep items visible when backend filtering is enabled", async () => { const wrapper = mount(OTree, { props: { options, filterable: true, backendFiltering: true }, }); const items = wrapper.findAll('[role="treeitem"]'); expect(items.length).toBeGreaterThan(0); const filterInput = wrapper.find('[data-oruga="input"] input'); expect(filterInput.exists()).toBeTruthy(); await filterInput.setValue(options[2].value); await filterInput.trigger("input"); await setTimeout(500); // await input debounce const updatedItems = wrapper.findAll('[role="treeitem"]'); expect(updatedItems).toHaveLength(items.length); expect(wrapper.emitted("filter")?.[0][0]).toContain( options[2].value, ); }); }); describe("handle options props correctly", () => { const options: TreeOptions = [ { label: "New York", value: "NY" }, { label: "Rome", value: "RM" }, { label: "London", value: "LDN" }, { label: "Istanbul", value: "IST" }, { label: "Paris", value: "PRS" }, ]; test("react accordingly when is using objects values", async () => { const wrapper = mount(OTree, { props: { options, selectable: true }, }); const items = wrapper.findAll('[data-oruga="tree-item"]'); expect(items.length).toBe(options.length); items.forEach((item, index) => { expect(item.text()).toStrictEqual(options[index].label); }); const itemLabel = items[1].find(".o-tree__item-label"); expect(itemLabel.exists()).toBeTruthy(); await itemLabel.trigger("click"); expect(items[0].classes("o-tree__item--selected")).toBeFalsy(); expect(items[1].classes("o-tree__item--selected")).toBeTruthy(); expect(items[2].classes("o-tree__item--selected")).toBeFalsy(); expect(wrapper.emitted("update:modelValue")).toHaveLength(1); expect(wrapper.emitted("update:modelValue")?.[0][0]).toStrictEqual( options[1].value, ); expect(wrapper.emitted("select")).toHaveLength(1); expect(wrapper.emitted("select")?.[0][0]).toStrictEqual( options[1].value, ); }); test("handle options as primitves correctly", () => { const options: OptionsProp = ["Flint", "Silver", "Vane", 0, 1, 2]; const wrapper = mount(OTree, { props: { options } }); const items = wrapper.findAll('[data-oruga="tree-item"]'); expect(items).toHaveLength(options.length); items.forEach((el, idx) => { expect(el.text()).toBe(String(options[idx])); expect(el.attributes("aria-disabled")).toBe("false"); expect(el.attributes("aria-hidden")).toBe("false"); expect(el.attributes("aria-selected")).toBe("false"); }); }); test("handle options as object correctly", () => { const options: OptionsProp = { flint: "Flint", silver: "Silver", vane: "Vane", 0: "Zero", 1: "One", 2: "Two", }; const wrapper = mount(OTree, { props: { options } }); const items = wrapper.findAll('[data-oruga="tree-item"]'); expect(items).toHaveLength(Object.keys(options).length); items.forEach((el, idx) => { expect(el.text()).toBe(Object.entries(options)[idx][1]); expect(el.attributes("aria-disabled")).toBe("false"); expect(el.attributes("aria-hidden")).toBe("false"); expect(el.attributes("aria-selected")).toBe("false"); }); }); test("handle options as options array correctly", () => { const options: TreeOptions = [ { label: "Flint", value: "flint" }, { label: "Silver", value: "silver", disabled: true }, { label: "Vane", value: "vane" }, { label: "Zero", value: 0 }, { label: "One", value: 1 }, { label: "Two", value: 2, disabled: true }, ]; const wrapper = mount(OTree, { props: { options } }); const items = wrapper.findAll('[data-oruga="tree-item"]'); expect(items).toHaveLength(options.length); items.forEach((el, idx) => { expect(el.text()).toBe(options[idx].label); expect(el.attributes("aria-disabled")).toBe( options[idx].disabled ? "true" : "false", ); expect(el.attributes("aria-hidden")).toBe("false"); expect(el.attributes("aria-selected")).toBe("false"); }); }); test("handle grouped options correctly", () => { const options: TreeOptions = [ { label: "Black Sails", options: [ { label: "Flint", value: "flint" }, { label: "Silver", value: "silver" }, { label: "Vane", value: "vane" }, { label: "Billy", value: "billy" }, ], }, { label: "Breaking Bad", options: { heisenberg: "Heisenberg", jesse: "Jesse", saul: "Saul", mike: "Mike", }, }, { label: "Game of Thrones", disabled: true, options: [ "Tyrion Lannister", "Jamie Lannister", "Daenerys Targaryen", "Jon Snow", ], }, ]; const wrapper = mount(OTree, { props: { options } }); const items = wrapper.findAll('[data-oruga="tree-item"]'); expect(items).toHaveLength(15); // every fifth element is a group header items .filter((_, idx) => idx % 5 == 0) .forEach((el, g_idx) => { const option = options[g_idx]; const label = el.find(".o-tree__item-label"); expect(label.exists()).toBeTruthy(); expect(label.text()).toBe(option.label); expect(el.attributes("aria-disabled")).toBe( option.disabled ? "true" : "false", ); expect(el.attributes("aria-hidden")).toBe("false"); expect(el.attributes("aria-selected")).toBe("false"); }); items.forEach((el, idx) => { // group headers are checked above if (idx % 5 == 0) return; const g_idx = Math.floor(idx / 5); const o_idx = (idx % 5) - 1; const g_options = options[g_idx].options ?? []; let optionLabel; if (idx < 5) { optionLabel = (g_options[o_idx] as TreeItemProps).label || g_options[o_idx]; } else if (idx < 10) { optionLabel = Object.entries(g_options)[o_idx][1]; } else { optionLabel = g_options[o_idx]; } expect(el.text()).toBe(optionLabel); expect(el.attributes("aria-disabled")).toBe("false"); expect(el.attributes("aria-hidden")).toBe("true"); expect(el.attributes("aria-selected")).toBe("false"); }); }); }); });