Files
heroui-vue/tests/autocomplete-demos.test.ts
T
maoyingjie 5e688680e9 feat(autocomplete): migrate Autocomplete with popover-owned filtering
Port the fixed-baseline Autocomplete onto the B02a collection protocol. The
trigger is the single Tab entry; opening moves focus to the search box inside the
popover, which owns filtering, aria-activedescendant and keyboard navigation
while the list only tracks an active row.

- Seven parts: root (field, value/open models, form value, owner wiring),
  trigger (anchor + activation), value (scoped slot with selectedItems and
  selectedText instead of copied item subtrees), indicator, clear button,
  popover (measured trigger width), filter (owns the search text and composes the
  inner SearchFieldInput into the combobox control).
- Filter exposes input-value/update:inputValue/inputChange, matching ComboBox.
- Confirming the active row in multiple mode now toggles it instead of replacing
  the accepted set (ComboBox shared the same bug).
- ComboBox shows the accepted label on the first render via a display value that
  falls back to the selected record, so preselected fields are readable before
  the selection watcher runs (including SSR).
- Popovers measure the anchor with a ResizeObserver because Reka does not publish
  the anchor size; the caller's root class is merged again so the trigger fills
  the field width.
- 24 baseline previews, Chinese doc page, navigation, 17 behaviour tests, 5
  demo/document contracts and an independent tarball consumption script for both
  pickers.

Not included: the full bundler/NodeNext type matrix (B02d).
2026-10-25 17:20:17 +08:00

188 lines
5.9 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { mount } from "@vue/test-utils";
import { nextTick, type Component } from "vue";
import { readFileSync } from "node:fs";
const settle = () => new Promise((resolve) => setTimeout(resolve, 6));
const mounted: { unmount: () => void }[] = [];
const previews = [
"default",
"variants",
"full-width",
"with-description",
"required",
"disabled",
"with-disabled-options",
"allows-empty-collection",
"with-sections",
"multiple-select",
"controlled",
"controlled-multiple",
"controlled-open-state",
"asynchronous-filtering",
"custom-indicator",
"custom-value",
"on-surface",
"virtualization",
"user-selection",
"user-selection-multiple",
"location-search",
"tag-group-selection",
"email-recipients",
"custom-styles",
];
afterEach(async () => {
while (mounted.length) mounted.pop()?.unmount();
await nextTick();
await settle();
vi.restoreAllMocks();
document.body.innerHTML = "";
});
describe("Autocomplete docs and demos", () => {
it("keeps the pinned preview order and one demo per preview", () => {
const document = readFileSync(
"apps/docs/components/autocomplete.md",
"utf8",
);
const names = [
...document.matchAll(/<ComponentPreview name="autocomplete-([^"]+)"/g),
].map((match) => match[1]);
expect(names).toEqual(previews);
for (const name of previews) {
const source = readFileSync(
`apps/docs/demos/autocomplete-${name}.vue`,
"utf8",
);
expect(source, name).toContain("<template>");
expect(source, name).not.toContain("className");
expect(source, name).not.toContain("@gravity-ui/icons");
expect(source, name).not.toContain("<path");
}
});
it("documents Vue props instead of React ones", () => {
const document = readFileSync(
"apps/docs/components/autocomplete.md",
"utf8",
);
expect(document).not.toMatch(/\|\s*`className`\s*\|/);
expect(document).toContain("`class`");
expect(document).toContain("Vue class 绑定");
for (const part of [
"AutocompleteTrigger",
"AutocompleteValue",
"AutocompleteIndicator",
"AutocompleteClearButton",
"AutocompletePopover",
"AutocompleteFilter",
])
expect(document, part).toContain(`### ${part}`);
expect(document).not.toContain("[!toc]");
// React object-style part names must not appear as API table entries.
expect(document).not.toMatch(/\|\s*`Autocomplete\.[A-Z]\w*`\s*\|/);
});
it("mounts every demo with a named trigger", async () => {
const modules = import.meta.glob("../apps/docs/demos/autocomplete-*.vue");
expect(Object.keys(modules)).toHaveLength(previews.length);
for (const [path, load] of Object.entries(modules)) {
const demo = (await load()) as { default: Component };
const wrapper = mount(demo.default, { attachTo: document.body });
mounted.push(wrapper);
await nextTick();
const triggers = (wrapper.element as HTMLElement).querySelectorAll(
'[data-slot="autocomplete-trigger"]',
);
expect(triggers.length, path).toBeGreaterThan(0);
for (const element of triggers) {
expect(element.getAttribute("role"), path).toBe("button");
expect(element.getAttribute("aria-expanded"), path).toBe("false");
}
wrapper.unmount();
mounted.pop();
await settle();
document.body.innerHTML = "";
}
});
it("opens and filters inside representative demos", async () => {
for (const name of [
"default",
"multiple-select",
"with-sections",
"custom-styles",
"virtualization",
]) {
const demo = (await import(
`../apps/docs/demos/autocomplete-${name}.vue`
)) as { default: Component };
const wrapper = mount(demo.default, { attachTo: document.body });
mounted.push(wrapper);
await nextTick();
await settle();
const trigger = (
wrapper.element as HTMLElement
).querySelector<HTMLElement>('[data-slot="autocomplete-trigger"]')!;
trigger.dispatchEvent(new MouseEvent("click", { bubbles: true }));
await nextTick();
await settle();
const search = document.querySelector<HTMLInputElement>(
'[data-slot="search-field-input"]',
);
expect(search, name).not.toBeNull();
expect(trigger.getAttribute("aria-expanded"), name).toBe("true");
const id = search!.getAttribute("aria-activedescendant");
expect(id, name).toBeTruthy();
expect(document.getElementById(id!)?.getAttribute("role"), name).toBe(
"option",
);
wrapper.unmount();
mounted.pop();
await settle();
document.body.innerHTML = "";
}
});
it("submits the required and name demos", async () => {
const formDemo =
(await import("../apps/docs/demos/autocomplete-required.vue")) as {
default: Component;
};
const wrapper = mount(formDemo.default, { attachTo: document.body });
mounted.push(wrapper);
await nextTick();
await settle();
const hidden = [
...(wrapper.element as HTMLElement).querySelectorAll<HTMLInputElement>(
'input[type="hidden"][name]',
),
];
expect(hidden.map((input) => input.name).sort()).toEqual([
"country",
"state",
]);
// `Form` renders novalidate and guards submit itself: an empty required
// field must keep the demo's status line empty.
const root = wrapper.element as HTMLElement;
const form = (root.matches("form") ? root : root.querySelector("form"))!;
const submit = (
wrapper.element as HTMLElement
).querySelector<HTMLButtonElement>('button[type="submit"]')!;
submit.click();
await nextTick();
expect(form.getAttribute("novalidate")).not.toBeNull();
expect(
(wrapper.element as HTMLElement)
.querySelector('[role="status"]')
?.textContent?.trim() ?? "",
).toBe("");
wrapper.unmount();
mounted.pop();
});
});