import { afterEach, it, expect, 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 roots: { unmount(): void }[] = []; const settle = async () => { await nextTick(); await new Promise((r) => setTimeout(r, 3)); }; afterEach(() => { while (roots.length) roots.pop()!.unmount(); vi.restoreAllMocks(); }); it("does not scroll a reveal request after filtering invalidates it", async () => { let collection: OwnedCollection | undefined; const visible = ref(true); const activeKey = ref(); const owner: CollectionOwner = { keys: computed(() => []), mode: computed(() => "single"), disabled: computed(() => false), readOnly: computed(() => false), disabledKeys: computed(() => []), id: "pending-reveal", labelledBy: () => undefined, ariaLabel: () => undefined, request: () => {}, action: () => {}, leave: () => {}, connect: (c) => { collection = c; return () => {}; }, virtualFocus: () => true, activeKey, filter: () => visible.value, }; const w = mount( defineComponent({ setup() { provide(collectionOwnerKey, owner); return () => h(ListBox, null, () => h(ListBoxItem, { id: "a" }, () => "A")); }, }), { attachTo: document.body }, ); roots.push(w); await settle(); const spy = vi.fn(); Object.defineProperty(w.find('[role="option"]').element, "scrollIntoView", { configurable: true, value: spy, }); const pending = collection!.reveal!("a"); visible.value = false; await pending; expect(spy).not.toHaveBeenCalled(); }); it("does not select the parent option when a nested button is clicked", async () => { const w = mount(Harness, { attachTo: document.body }); roots.push(w); await settle(); await w.find("input").setValue(""); await settle(); const row = w.find('[role="option"]').element; const button = document.createElement("button"); row.appendChild(button); button.click(); await settle(); expect(w.emitted("update:modelValue")).toBeUndefined(); }); it("shows pointer pressed state while preserving input focus", async () => { const w = mount(Harness, { attachTo: document.body }); roots.push(w); await settle(); await w.find("input").setValue(""); await settle(); const row = w.find('[role="option"]'); await row.trigger("pointerdown", { button: 0 }); await settle(); expect(row.attributes("data-pressed")).toBe("true"); }); it.each(["removed", "disabled"])( "does not confirm a dynamically %s active option", async (mode) => { const w = mount(Harness, { props: { options: [ { id: "a", text: "A" }, { id: "b", text: "B" }, ], }, attachTo: document.body, }); roots.push(w); await settle(); await w.find("input").setValue(""); await settle(); expect(w.vm.activeKey).toBe("a"); await w.setProps({ options: mode === "removed" ? [{ id: "b", text: "B" }] : [ { id: "a", text: "A", disabled: true }, { id: "b", text: "B" }, ], }); await settle(); await w.find("input").trigger("keydown", { key: "Enter" }); await settle(); expect( w.emitted("proposal")?.some((args) => args[0] === "a") ?? false, ).toBe(false); }, ); it("provides filtered-empty feedback for the static slot collection", async () => { const w = mount(Harness, { attachTo: document.body }); roots.push(w); await settle(); await w.find("input").setValue("no-match"); await settle(); expect(w.find('[data-test="empty"]').exists()).toBe(true); }); it("can filter a record outside the initially mounted virtual window", async () => { const options = Array.from({ length: 100 }, (_, i) => ({ id: i, text: `Row ${i}`, })); const w = mount(Harness, { props: { virtualized: true, options }, attachTo: document.body, }); roots.push(w); await settle(); await w.find("input").setValue("Row 99"); await settle(); expect( w.findAll('[role="option"]').some((row) => row.text().trim() === "Row 99"), ).toBe(true); });