Files
heroui-vue/tests/autocomplete-demos.test.ts
T
maoyingjie e34b9debdf fix(pickers): rework B02 against the public review
Restore native clearing, unify the form/field protocol with the other choice
families (per-key entries, disabled and removed keys omitted, shared form
owner), synchronise ComboBox text by key identity, keep filtering out of open
composition sessions, and stop stealing focus when a controlled close is
rejected. Also merge caller refs/ARIA/disabled, implement asChild, autoFocus
and ComboBox variant, use a locale-aware base matcher, and put the indicator
class and slot on the icon element the ported CSS selects.
2026-10-25 20:40:24 +08:00

211 lines
6.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("uses the baseline search field shell and empty state in every demo", () => {
for (const name of previews) {
const source = readFileSync(
`apps/docs/demos/autocomplete-${name}.vue`,
"utf8",
);
// Upstream renders SearchField autoFocus name="search" variant="secondary"
// with a clear button; the popover filter must keep that whole shell.
const fields = [...source.matchAll(/<SearchField\b[^>]*>/g)];
expect(fields.length, name).toBeGreaterThan(0);
for (const [tag] of fields) {
expect(tag, name).toMatch(/\bvariant="secondary"/);
expect(tag, name).toMatch(/\bname="search"/);
expect(tag, name).toMatch(/\bauto-focus/);
}
expect(source, name).toContain("SearchFieldClearButton");
// The ported .hv-empty-state padding must not be overridden per demo.
expect(source, name).not.toMatch(/<EmptyState\s+class=/);
expect(source, name).not.toContain("alert(");
}
});
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 root = wrapper.element as HTMLElement;
const pickerForm = (
root.matches("form") ? root : root.querySelector("form")
)!;
// Empty required pickers submit no entry; the Form guard blocks the submit.
expect(new FormData(pickerForm as HTMLFormElement).has("state")).toBe(
false,
);
expect(new FormData(pickerForm as HTMLFormElement).has("country")).toBe(
false,
);
// `Form` renders novalidate and guards submit itself: an empty required
// field must keep the demo's status line empty.
const form = pickerForm;
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();
});
});