Files
heroui-vue/tests/b02a-round2-review.probe.test.ts
T
maoyingjie c8b0aa26c3 fix(collection): make virtual window, reveal and confirmation honest
- Give the verification instance a real constrained scroll owner and a row
  height that matches the rendered row, and feed ListBox the full data schema so
  a row that was never mounted can still be filtered.
- Move reveal validity in front of every side effect and re-check after each
  await: latest token, mounted component, enabled row, still visible and open.
  Waiting for a virtualized row now requires connected DOM, not just a
  registration, and closing the collection cancels pending work.
- Reconcile the shared active key reactively: a removed, disabled, filtered-out
  or closed active row is cleared, while the accepted value stays as history.
  The composite re-validates before confirming, so a stale candidate cannot be
  submitted.
- Read the caller's cancellation before the focus guard so a plain virtual-focus
  press keeps its pressed state, and use one nested-interactive boundary for
  both pointer and click, covering contenteditable variants and custom roles.
- Render the empty channel for static slot collections too, keeping hidden
  members mounted.

Adds the seven-case round-two probe and twelve regressions; browser checks cover
the real virtual window, remote filtering, viewport-contained navigation and
filter restoration.
2026-10-25 16:40:00 +08:00

149 lines
4.4 KiB
TypeScript

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<string | number>();
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);
});