import { afterEach, describe, expect, it, vi } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { defineComponent, h, ref } from "vue"; import { ListBox, ListBoxItem, ListBoxItemIndicator, ListBoxSection, Label, Description, Header, Fieldset, } from "../packages/vue/src"; const wrappers: ReturnType[] = []; afterEach(() => { wrappers.splice(0).forEach((wrapper) => wrapper.unmount()); document.body.innerHTML = ""; }); function fixture(template: string, setup = () => ({})) { const wrapper = mount( defineComponent({ components: { ListBox, ListBoxItem, ListBoxItemIndicator, ListBoxSection, Label, Description, Header, Fieldset, }, setup, template, }), { attachTo: document.body }, ); wrappers.push(wrapper); return wrapper; } const options = 'emma@example.com'; const option = (wrapper: ReturnType, index: number) => wrapper.findAll('[role="option"]')[index]; describe("ListBox public behavior", () => { it("retains the last Tab entry after blur and combines native focus-visible with local modality", async () => { const wrapper = fixture( `
${options}
`, ); await option(wrapper, 1).trigger("pointerdown", { button: 0 }); option(wrapper, 1).element.focus(); await flushPromises(); wrapper.get("button").element.focus(); await flushPromises(); expect(option(wrapper, 1).attributes("tabindex")).toBe("0"); expect(option(wrapper, 0).attributes("tabindex")).toBe("-1"); expect(option(wrapper, 1).attributes("data-focused")).toBeUndefined(); option(wrapper, 1).element.focus(); await flushPromises(); expect(option(wrapper, 1).element.matches(":focus-visible")).toBe(true); expect(option(wrapper, 1).attributes("data-focus-visible")).toBe("true"); }); it("hides selection in none mode, keeps item variants independent and leaves modified Enter alone", async () => { const wrapper = fixture( `${options}`, ); option(wrapper, 0).element.focus(); expect(option(wrapper, 3).attributes("data-selected")).toBeUndefined(); expect(option(wrapper, 0).classes()).toContain("hv-list-box-item--default"); await option(wrapper, 0).trigger("keydown", { key: "Enter", ctrlKey: true, }); expect(wrapper.getComponent(ListBox).emitted("action")).toBeUndefined(); await option(wrapper, 0).trigger("keydown", { key: "Enter" }); expect(wrapper.getComponent(ListBox).emitted("action")).toEqual([[0]]); }); it("holds keyboard pressed state, activates Space on release once and respects keyup cancellation", async () => { const cancelled = ref(false); const wrapper = fixture( `${options}`, () => ({ cancelled }), ); option(wrapper, 0).element.focus(); await option(wrapper, 0).trigger("keydown", { key: " " }); expect(option(wrapper, 0).attributes("data-pressed")).toBe("true"); expect(wrapper.getComponent(ListBox).emitted("action")).toBeUndefined(); await option(wrapper, 0).trigger("keydown", { key: " ", repeat: true }); await option(wrapper, 0).trigger("keyup", { key: " " }); expect(option(wrapper, 0).attributes("data-pressed")).toBeUndefined(); expect(wrapper.getComponent(ListBox).emitted("action")).toEqual([[0]]); cancelled.value = true; await flushPromises(); await option(wrapper, 0).trigger("keydown", { key: " " }); await option(wrapper, 0).trigger("keyup", { key: " " }); expect(wrapper.getComponent(ListBox).emitted("action")).toEqual([[0]]); expect(option(wrapper, 0).attributes("data-pressed")).toBeUndefined(); }); it("keeps the current prefix match while typing a word and cycles repeated initials", async () => { const wrapper = fixture( 'Liam SmithLiam JonesEmma', ); option(wrapper, 2).element.focus(); for (const key of ["l", "i", "a", "m"]) await wrapper.get('[role="listbox"]').trigger("keydown", { key }); expect(document.activeElement).toBe(option(wrapper, 0).element); // A new repeated-initial search cycles, rather than searching for "ll". vi.spyOn(Date, "now").mockReturnValue(Date.now() + 2000); try { await option(wrapper, 0).trigger("keydown", { key: "l" }); expect(document.activeElement).toBe(option(wrapper, 1).element); await option(wrapper, 1).trigger("keydown", { key: "l" }); expect(document.activeElement).toBe(option(wrapper, 0).element); } finally { vi.restoreAllMocks(); } }); it("keeps typed keys, selection aliases and generated DOM ids distinct", async () => { const wrapper = fixture( `${options}`, ); await flushPromises(); expect(option(wrapper, 0).attributes("aria-selected")).toBe("true"); expect(option(wrapper, 1).attributes("aria-selected")).toBe("false"); await option(wrapper, 1).trigger("click"); expect(wrapper.getComponent(ListBox).emitted("update:modelValue")).toEqual([ [[0, "0"]], ]); expect(wrapper.getComponent(ListBox).emitted("selectionChange")).toEqual([ [[0, "0"]], ]); expect(option(wrapper, 0).attributes("id")).not.toBe("0"); expect(option(wrapper, 0).attributes("id")).not.toBe( option(wrapper, 1).attributes("id"), ); expect(option(wrapper, 0).attributes("aria-labelledby")).toBe( option(wrapper, 0).get('[data-slot="label"]').attributes("id"), ); expect(option(wrapper, 0).attributes("aria-describedby")).toBe( option(wrapper, 0).get('[data-slot="description"]').attributes("id"), ); }); it("restores controlled selection after rejection and retries identical proposals", async () => { const wrapper = fixture( `${options}`, ); await option(wrapper, 1).trigger("click"); await option(wrapper, 1).trigger("click"); expect(option(wrapper, 1).attributes("aria-selected")).toBe("false"); expect(wrapper.getComponent(ListBox).emitted("update:modelValue")).toEqual([ [[0, "0"]], [[0, "0"]], ]); }); it("treats explicit undefined model as controlled and gives it priority over selectedKeys", async () => { const wrapper = fixture( `${options}`, ); await option(wrapper, 0).trigger("click"); expect(option(wrapper, 0).attributes("aria-selected")).toBe("false"); expect(wrapper.getComponent(ListBox).emitted("update:modelValue")).toEqual([ [[0]], ]); }); it("implements single toggle and disallow-empty selection", async () => { const locked = ref(true); const wrapper = fixture( `${options}`, () => ({ locked }), ); await option(wrapper, 0).trigger("click"); expect(option(wrapper, 0).attributes("aria-selected")).toBe("true"); locked.value = false; await flushPromises(); await option(wrapper, 0).trigger("click"); expect(option(wrapper, 0).attributes("aria-selected")).toBe("false"); await option(wrapper, 1).trigger("click"); expect(wrapper.getComponent(ListBox).emitted("update:modelValue")).toEqual([ [[]], [["0"]], ]); }); it("keeps action-only collections unselected for pointer and keyboard activation", async () => { const wrapper = fixture( `${options}`, ); await option(wrapper, 0).trigger("click"); await flushPromises(); await option(wrapper, 0).trigger("keydown", { key: "Enter" }); expect(wrapper.getComponent(ListBox).emitted("action")).toEqual([[0], [0]]); expect( wrapper.getComponent(ListBox).emitted("update:modelValue"), ).toBeUndefined(); expect(option(wrapper, 0).attributes("aria-selected")).toBeUndefined(); }); it("uses one Tab stop, skips disabled keys and supports Home/End and RTL orientation", async () => { const wrapper = fixture( `${options}`, ); await flushPromises(); const root = wrapper.get('[role="listbox"]'); expect(root.attributes("tabindex")).toBe("-1"); expect(wrapper.findAll('[role="option"][tabindex="0"]')).toHaveLength(1); option(wrapper, 0).element.focus(); await option(wrapper, 0).trigger("keydown", { key: "ArrowLeft" }); expect(document.activeElement).toBe(option(wrapper, 1).element); await option(wrapper, 1).trigger("keydown", { key: "ArrowLeft" }); expect(document.activeElement).toBe(option(wrapper, 3).element); await option(wrapper, 3).trigger("keydown", { key: "Home" }); expect(document.activeElement).toBe(option(wrapper, 0).element); await option(wrapper, 0).trigger("keydown", { key: "End" }); expect(document.activeElement).toBe(option(wrapper, 3).element); expect( wrapper.getComponent(ListBox).emitted("update:modelValue"), ).toBeUndefined(); }); it("selects a range with a zero anchor, toggles all available keys and respects readonly", async () => { const readonly = ref(false); const wrapper = fixture( `${options}`, () => ({ readonly }), ); await option(wrapper, 0).trigger("click"); await flushPromises(); await option(wrapper, 0).trigger("keydown", { key: "End", shiftKey: true }); expect(wrapper.findAll('[aria-selected="true"]')).toHaveLength(3); expect(option(wrapper, 2).attributes("aria-selected")).toBe("false"); readonly.value = true; await flushPromises(); await option(wrapper, 3).trigger("keydown", { key: "a", ctrlKey: true }); await option(wrapper, 1).trigger("click"); expect(wrapper.getComponent(ListBox).emitted("update:modelValue")).toEqual([ [[0]], [[0, "0", "last"]], ]); }); it("replace navigation selects focused options while modifier navigation only moves focus", async () => { const wrapper = fixture( `${options}`, ); await option(wrapper, 0).trigger("click"); await flushPromises(); await option(wrapper, 0).trigger("keydown", { key: "ArrowDown" }); expect(option(wrapper, 1).attributes("aria-selected")).toBe("true"); expect(option(wrapper, 0).attributes("aria-selected")).toBe("false"); await option(wrapper, 1).trigger("keydown", { key: "End", ctrlKey: true }); expect(document.activeElement).toBe(option(wrapper, 3).element); expect(option(wrapper, 3).attributes("aria-selected")).toBe("false"); }); it("uses textValue for accent-insensitive typeahead and leaves IME alone", async () => { const wrapper = fixture(`${options}`); option(wrapper, 1).element.focus(); await option(wrapper, 1).trigger("keydown", { key: "e", isComposing: true, }); expect(document.activeElement).toBe(option(wrapper, 1).element); await option(wrapper, 1).trigger("keydown", { key: "e" }); expect(document.activeElement).toBe(option(wrapper, 0).element); }); it("respects caller keyboard/click cancellation and DOM listener-array stop semantics", async () => { const second = vi.fn(); const wrapper = fixture( `AB`, () => ({ handlers: [ (event: MouseEvent) => event.stopImmediatePropagation(), second, ], }), ); option(wrapper, 0).element.focus(); await option(wrapper, 0).trigger("keydown", { key: "ArrowDown" }); expect(document.activeElement).toBe(option(wrapper, 0).element); await option(wrapper, 0).trigger("click"); expect(second).not.toHaveBeenCalled(); expect( wrapper.getComponent(ListBox).emitted("update:modelValue"), ).toBeUndefined(); }); it("cancels select before activation and exposes the original keyboard event", async () => { const select = vi.fn((event: CustomEvent) => event.preventDefault()); const wrapper = fixture( 'A', () => ({ select }), ); option(wrapper, 0).element.focus(); await option(wrapper, 0).trigger("keydown", { key: " " }); await option(wrapper, 0).trigger("keyup", { key: " " }); expect(select).toHaveBeenCalledOnce(); expect(select.mock.calls[0][0].detail.originalEvent).toBeInstanceOf( KeyboardEvent, ); expect(wrapper.getComponent(ListBox).emitted("action")).toBeUndefined(); }); it("applies Fieldset disabled semantics and preserves first Legend exemption", async () => { const wrapper = fixture( `
${options}${options}
`, ); const lists = wrapper.findAllComponents(ListBox); await lists[0].findAll('[role="option"]')[0].trigger("click"); await lists[1].findAll('[role="option"]')[0].trigger("click"); expect(lists[0].emitted("update:modelValue")).toEqual([[[0]]]); expect(lists[1].emitted("update:modelValue")).toBeUndefined(); expect( lists[1].findAll('[role="option"]')[0].attributes("aria-disabled"), ).toBe("true"); expect(lists[1].findAll('[tabindex="0"]')).toHaveLength(0); }); it("isolates option labels/descriptions, names sections and honors explicit ARIA", async () => { const wrapper = fixture( '
Team
Detail ADetail B
', ); expect(wrapper.get('[role="group"]').attributes("aria-labelledby")).toBe( wrapper.get("header").attributes("id"), ); expect(option(wrapper, 0).attributes("aria-describedby")).not.toBe( option(wrapper, 1).attributes("aria-describedby"), ); expect(option(wrapper, 1).attributes("aria-labelledby")).toBeUndefined(); expect(option(wrapper, 1).attributes("aria-label")).toBe("Explicit"); }); it("handles member reordering, disabled updates and removal without losing a focus entry", async () => { const items = ref([ { id: "a", textValue: "A" }, { id: "b", textValue: "B" }, { id: "c", textValue: "C" }, ]); const blocked = ref([]); const wrapper = fixture( '{{ item.textValue }}', () => ({ items, blocked }), ); option(wrapper, 0).element.focus(); items.value.reverse(); await flushPromises(); await option(wrapper, 2).trigger("keydown", { key: "Home" }); expect(document.activeElement?.textContent).toBe("C"); blocked.value = ["c"]; await flushPromises(); expect(document.activeElement?.textContent).toBe("B"); items.value = items.value.filter((item) => item.id !== "b"); await flushPromises(); expect(document.activeElement?.textContent).toBe("A"); expect(wrapper.findAll('[role="option"][tabindex="0"]')).toHaveLength(1); }); it("renders a bounded virtual window but selects the entire logical collection", async () => { const items = Array.from({ length: 1000 }, (_, index) => ({ id: index, textValue: `User ${index}`, isDisabled: index === 999, })); const wrapper = fixture( '{{ item.textValue }}', () => ({ items }), ); await flushPromises(); expect(wrapper.findAll('[role="option"]').length).toBeGreaterThan(0); expect(wrapper.findAll('[role="option"]').length).toBeLessThan(40); await option(wrapper, 0).trigger("keydown", { key: "a", ctrlKey: true }); const changes = wrapper.getComponent(ListBox).emitted("update:modelValue")!; expect(changes[0][0] as number[]).toHaveLength(999); expect(changes[0][0] as number[]).not.toContain(999); expect(option(wrapper, 0).attributes("aria-setsize")).toBe("1000"); // jsdom does not verify scrolling geometry; browser verifies End/scroll/focus. }); it("keeps all instances isolated and supports a supplied empty state", async () => { const wrapper = fixture( `
${options}${options}
`, ); await option(wrapper, 0).trigger("click"); expect(wrapper.findAll('[aria-selected="true"]')).toHaveLength(1); const ids = wrapper.findAll("[id]").map((node) => node.attributes("id")); expect(new Set(ids).size).toBe(ids.length); expect(wrapper.text()).toContain("No results"); }); it("compiles and mounts all nine baseline demos together without warnings or duplicate IDs", async () => { const demos = import.meta.glob("../apps/docs/demos/list-box-*.vue", { eager: true, import: "default", }); expect(Object.keys(demos)).toHaveLength(9); const warnings: string[] = []; const wrapper = mount( defineComponent({ render: () => h( "main", Object.values(demos).map((component) => h(component as ReturnType), ), ), }), { attachTo: document.body, global: { config: { warnHandler: (message) => warnings.push(message) }, }, }, ); wrappers.push(wrapper); await flushPromises(); expect(warnings).toEqual([]); expect(wrapper.findAll('[role="listbox"]')).toHaveLength(9); expect(wrapper.findAll('[role="option"]').length).toBeLessThan(100); const ids = wrapper.findAll("[id]").map((node) => node.attributes("id")); expect(new Set(ids).size).toBe(ids.length); const defaults = wrapper.findAll('[role="listbox"]')[0]; expect(wrapper.text()).toContain("bob@heroui.com"); expect(defaults.find('[role="option"]').exists()).toBe(true); }); });