import { afterEach, expect, it, vi } from "vitest"; import { mount } from "@vue/test-utils"; import { computed, defineComponent, h, nextTick, provide, ref } from "vue"; import { ListBox, ListBoxItem } from "../packages/vue/src"; import { collectionOwnerKey, type CollectionOwner, type OwnedCollection, } from "../packages/vue/src/components/collection/owner"; import Harness from "../packages/vue/tests/fixtures/FilterVirtualCombobox.vue"; const mounted: { unmount(): void }[] = []; afterEach(() => { while (mounted.length) mounted.pop()!.unmount(); vi.restoreAllMocks(); }); const settle = async () => { await nextTick(); await new Promise((r) => setTimeout(r, 3)); }; function setupList(virtualized = false) { let collection: OwnedCollection | undefined; const items = Array.from({ length: 6 }, (_, id) => ({ id, textValue: `Row ${id}`, })); const activeKey = ref(); const owner: CollectionOwner = { keys: computed(() => []), mode: computed(() => "single"), disabled: computed(() => false), readOnly: computed(() => false), disabledKeys: computed(() => []), id: "probe", labelledBy: () => undefined, ariaLabel: () => "Probe", request: () => {}, action: () => {}, leave: () => {}, connect: (c) => { collection = c; return () => { collection = undefined; }; }, filter: (r) => r.key === 3, virtualFocus: () => true, activeKey, }; const wrapper = mount( defineComponent({ setup() { provide(collectionOwnerKey, owner); return () => h( ListBox, { items, virtualized, rowHeight: 40, overscan: 8 }, { default: ({ item }: { item: (typeof items)[number] }) => h(ListBoxItem, { id: item.id }, () => item.textValue), }, ); }, }), { attachTo: document.body }, ); mounted.push(wrapper); return { wrapper, collection: () => collection }; } it("preserves caller hidden on an ordinary ListBoxItem", async () => { const w = mount(ListBox, { slots: { default: () => h(ListBoxItem, { id: "hidden", hidden: true }, () => "Hidden"), }, }); mounted.push(w); await settle(); expect(w.find('[role="option"]').attributes("hidden")).toBeDefined(); }); it("reveal scrolls a mounted ordinary row without moving DOM focus", async () => { const { wrapper, collection } = setupList(); await settle(); const node = wrapper.findAll('[role="option"]')[3].element; const scroll = vi.fn(); Object.defineProperty(node, "scrollIntoView", { configurable: true, value: scroll, }); await collection()!.reveal!(3); expect(scroll).toHaveBeenCalled(); }); it("virtual window contains no fixed height slots for filtered-out rows", async () => { const { wrapper } = setupList(true); await settle(); expect( wrapper.findAll( '[data-slot="list-box-virtual-content"] > [role="presentation"]', ), ).toHaveLength(1); }); it("restores keyboard highlight intent after a pointer interaction", async () => { const w = mount(Harness, { attachTo: document.body }); mounted.push(w); await settle(); const input = w.find("input"); await input.setValue(""); await settle(); await w.findAll('[role="option"]')[0].trigger("pointerdown"); await input.trigger("keydown", { key: "ArrowDown" }); await settle(); expect(w.find('[data-focused="true"]').attributes("data-focus-visible")).toBe( "true", ); }); it("has instance-unique names and controls in the verification harness", async () => { const w = mount( defineComponent({ setup: () => () => h("div", [h(Harness), h(Harness)]) }), ); mounted.push(w); await settle(); const ids = w.findAll("[id]").map((n) => n.attributes("id")); expect(new Set(ids).size).toBe(ids.length); });