c8d54abefb
HD-10 scope, against heroui ce90ea3 collection sources and their react-aria/react-stately dependencies: - the ComboBox async demo now auto-loads on scroll-to-bottom (IntersectionObserver sentinel + fill chain mirroring the pinned ListBoxLoadMoreItem re-observe semantics), never duplicates requests (loading guards + monotonic sequence token), drops stale responses on query switches, keeps a retry path for failures (injectable mock failure) and documents its differences from the pinned SWAPI source; the Select demo's observer stays untouched (real-network scroll triggering verified in browser) - the Autocomplete virtualization demo and the default filter share one locale-sensitive matcher: createBaseContains (Intl.Collator base sensitivity, cafe matches Cafe with accent folding) is now exported from the package root - Autocomplete trigger semantics verified complete (single tab stop, Enter/Space/Escape/Delete/IME, single and multiple selection) and the scoped slots gained composition data instead of child copying: AutocompleteValue exposes selectedItem and per-entry source item, the ComboBox root exposes selectedItem - DropdownSubmenuTrigger exposes the pinned delay contract (default 200ms): the controlled open gate retimes Reka's fixed 100ms hover, keyboard opens stay immediate and cancel the pending hover, pointer leave cancels; the dead side/align forwarding is gone (Reka fixes the submenu side per direction); Button/Avatar trigger layout compensation kept after browser proof it is what wins the cascade - tests/fixtures/collections-rtl.vue drives the real-browser delay and RTL geometry checks; b02-pickers contract pins the new export and slot typing Full suite 111 files / 1666 tests, typecheck, build:docs and the b02-pickers + api-root-aliases tarball contracts pass.
331 lines
11 KiB
TypeScript
331 lines
11 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, 4));
|
|
const mounted: { unmount: () => void }[] = [];
|
|
|
|
const previews = [
|
|
"default",
|
|
"full-width",
|
|
"with-description",
|
|
"required",
|
|
"disabled",
|
|
"with-disabled-options",
|
|
"with-sections",
|
|
"controlled",
|
|
"controlled-input-value",
|
|
"asynchronous-loading",
|
|
"default-selected-key",
|
|
"allows-custom-value",
|
|
"custom-indicator",
|
|
"custom-value",
|
|
"custom-filtering",
|
|
"render-function",
|
|
"menu-trigger",
|
|
"form-value",
|
|
"validation-behavior",
|
|
"custom-validation",
|
|
"read-only",
|
|
"multiple-selection",
|
|
"on-surface",
|
|
"custom-styles",
|
|
];
|
|
|
|
afterEach(async () => {
|
|
while (mounted.length) mounted.pop()?.unmount();
|
|
await nextTick();
|
|
await settle();
|
|
vi.restoreAllMocks();
|
|
document.body.innerHTML = "";
|
|
});
|
|
|
|
describe("ComboBox docs and demos", () => {
|
|
it("keeps the pinned preview order and one demo per preview", () => {
|
|
const document = readFileSync("apps/docs/components/combo-box.md", "utf8");
|
|
const names = [
|
|
...document.matchAll(/<ComponentPreview name="combo-box-([^"]+)"/g),
|
|
].map((match) => match[1]);
|
|
|
|
expect(names).toEqual(previews);
|
|
for (const name of previews) {
|
|
const source = readFileSync(
|
|
`apps/docs/demos/combo-box-${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 and events instead of React ones", () => {
|
|
const document = readFileSync("apps/docs/components/combo-box.md", "utf8");
|
|
|
|
expect(document).not.toMatch(/\|\s*`className`\s*\|/);
|
|
expect(document).toContain("`class`");
|
|
expect(document).toContain("Vue class 绑定");
|
|
expect(document).toContain("### ComboBoxInputGroup");
|
|
expect(document).toContain("### ComboBoxValue");
|
|
expect(document).toContain("### ComboBoxTrigger");
|
|
expect(document).toContain("### ComboBoxPopover");
|
|
expect(document).not.toContain("[!toc]");
|
|
expect(document).not.toContain("ComboBox.List");
|
|
});
|
|
|
|
it("mounts every demo with a real combobox control", async () => {
|
|
const modules = import.meta.glob("../apps/docs/demos/combo-box-*.vue");
|
|
expect(Object.keys(modules)).toHaveLength(previews.length);
|
|
|
|
for (const [path, load] of Object.entries(modules)) {
|
|
vi.useFakeTimers();
|
|
const demo = (await load()) as { default: Component };
|
|
const wrapper = mount(demo.default, { attachTo: document.body });
|
|
|
|
mounted.push(wrapper);
|
|
await nextTick();
|
|
const inputs = (wrapper.element as HTMLElement).querySelectorAll(
|
|
'[role="combobox"]',
|
|
);
|
|
|
|
expect(inputs.length, path).toBeGreaterThan(0);
|
|
// Every combobox must be a real text input with a label association.
|
|
for (const input of inputs) {
|
|
expect(input.tagName, path).toBe("INPUT");
|
|
expect(input.getAttribute("aria-expanded"), path).toBe("false");
|
|
expect(input.id, path).not.toBe("");
|
|
}
|
|
wrapper.unmount();
|
|
mounted.pop();
|
|
vi.useRealTimers();
|
|
await settle();
|
|
document.body.innerHTML = "";
|
|
}
|
|
});
|
|
|
|
it("opens, filters and selects inside representative demos", async () => {
|
|
const load = (name: string) =>
|
|
import(`../apps/docs/demos/combo-box-${name}.vue`);
|
|
|
|
for (const name of ["default", "with-sections", "custom-styles"]) {
|
|
const demo = (await load(name)) as { default: Component };
|
|
const wrapper = mount(demo.default, { attachTo: document.body });
|
|
mounted.push(wrapper);
|
|
await nextTick();
|
|
await settle();
|
|
const input = (
|
|
wrapper.element as HTMLElement
|
|
).querySelector<HTMLInputElement>('[role="combobox"]')!;
|
|
input.focus();
|
|
input.dispatchEvent(
|
|
new KeyboardEvent("keydown", {
|
|
key: "ArrowDown",
|
|
bubbles: true,
|
|
cancelable: true,
|
|
}),
|
|
);
|
|
await nextTick();
|
|
await settle();
|
|
expect(input.getAttribute("aria-expanded"), name).toBe("true");
|
|
const id = input.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("shows a preselected item's text in the input", async () => {
|
|
for (const [name, expected] of [
|
|
["default-selected-key", "猫"],
|
|
["disabled", "猫"],
|
|
["read-only", "猫"],
|
|
] as const) {
|
|
const demo = (await import(
|
|
`../apps/docs/demos/combo-box-${name}.vue`
|
|
)) as { default: Component };
|
|
const wrapper = mount(demo.default, { attachTo: document.body });
|
|
mounted.push(wrapper);
|
|
await nextTick();
|
|
await settle();
|
|
const input = (
|
|
wrapper.element as HTMLElement
|
|
).querySelector<HTMLInputElement>('[role="combobox"]')!;
|
|
expect(input.value, name).toBe(expected);
|
|
wrapper.unmount();
|
|
mounted.pop();
|
|
await settle();
|
|
document.body.innerHTML = "";
|
|
}
|
|
});
|
|
|
|
it("submits form-value demos through hidden inputs", async () => {
|
|
const demo =
|
|
(await import("../apps/docs/demos/combo-box-form-value.vue")) as {
|
|
default: Component;
|
|
};
|
|
const wrapper = mount(demo.default, { attachTo: document.body });
|
|
mounted.push(wrapper);
|
|
await nextTick();
|
|
await settle();
|
|
const form = (wrapper.element as HTMLElement).matches("form")
|
|
? (wrapper.element as HTMLFormElement)
|
|
: (wrapper.element as HTMLElement).querySelector("form")!;
|
|
// Nothing selected yet: an empty picker submits no entry at all.
|
|
expect(new FormData(form).has("animal")).toBe(false);
|
|
|
|
for (const input of [
|
|
...form.querySelectorAll<HTMLInputElement>('[data-slot="input"]'),
|
|
]) {
|
|
input.focus();
|
|
await settle();
|
|
input.dispatchEvent(
|
|
new KeyboardEvent("keydown", {
|
|
key: "ArrowDown",
|
|
bubbles: true,
|
|
cancelable: true,
|
|
}),
|
|
);
|
|
await settle();
|
|
input.dispatchEvent(
|
|
new KeyboardEvent("keydown", {
|
|
key: "Enter",
|
|
bubbles: true,
|
|
cancelable: true,
|
|
}),
|
|
);
|
|
await settle();
|
|
}
|
|
|
|
const data = new FormData(form);
|
|
const inputs = [
|
|
...form.querySelectorAll<HTMLInputElement>('[data-slot="input"]'),
|
|
];
|
|
// Both pickers confirmed the same row: key mode submits the id, text mode
|
|
// the label — never a comma-joined string.
|
|
expect(data.get("animal")).toBe("cat");
|
|
expect(data.get("animal-text")).toBe("猫");
|
|
expect(data.getAll("animal")).toEqual(["cat"]);
|
|
expect(inputs.map((input) => input.value)).toEqual(["猫", "猫"]);
|
|
wrapper.unmount();
|
|
mounted.pop();
|
|
});
|
|
});
|
|
|
|
describe("asynchronous loading demo", () => {
|
|
class FakeIntersectionObserver {
|
|
static instances: FakeIntersectionObserver[] = [];
|
|
callback: (entries: unknown[]) => void;
|
|
constructor(callback: (entries: unknown[]) => void) {
|
|
this.callback = callback;
|
|
FakeIntersectionObserver.instances.push(this);
|
|
}
|
|
observe() {}
|
|
unobserve() {}
|
|
disconnect() {}
|
|
trigger(isIntersecting: boolean) {
|
|
this.callback([{ isIntersecting }]);
|
|
}
|
|
static last() {
|
|
return FakeIntersectionObserver.instances.at(-1)!;
|
|
}
|
|
static reset() {
|
|
FakeIntersectionObserver.instances = [];
|
|
}
|
|
}
|
|
|
|
async function mountAsyncDemo() {
|
|
vi.useFakeTimers();
|
|
vi.stubGlobal("IntersectionObserver", FakeIntersectionObserver);
|
|
FakeIntersectionObserver.reset();
|
|
const demo = (
|
|
await import("../apps/docs/demos/combo-box-asynchronous-loading.vue")
|
|
).default;
|
|
const w = mount(demo, { attachTo: document.body });
|
|
mounted.push(w);
|
|
// Initial page resolves on the simulated latency.
|
|
await vi.advanceTimersByTimeAsync(350);
|
|
await nextTick();
|
|
(w.get('[data-slot="input"]').element as HTMLInputElement).focus();
|
|
await nextTick();
|
|
return w;
|
|
}
|
|
|
|
const optionNames = () =>
|
|
[...document.querySelectorAll('[role="option"]')].map((node) =>
|
|
node.textContent?.trim(),
|
|
);
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
vi.unstubAllGlobals();
|
|
FakeIntersectionObserver.reset();
|
|
});
|
|
|
|
it("auto-loads the next page when the sentinel intersects, without duplicate requests", async () => {
|
|
const w = await mountAsyncDemo();
|
|
expect(optionNames()).toHaveLength(4);
|
|
// Two intersections in the same tick must start one request only: a
|
|
// duplicated fetch would append the same offset twice.
|
|
FakeIntersectionObserver.last().trigger(true);
|
|
FakeIntersectionObserver.last().trigger(true);
|
|
await vi.advanceTimersByTimeAsync(350);
|
|
await nextTick();
|
|
const afterFirst = optionNames();
|
|
expect(afterFirst).toHaveLength(8);
|
|
expect(new Set(afterFirst).size).toBe(8);
|
|
// The chain fills the remaining page while the sentinel stays visible.
|
|
await vi.advanceTimersByTimeAsync(350);
|
|
await nextTick();
|
|
expect(optionNames()).toHaveLength(12);
|
|
expect(document.body.textContent).not.toContain("加载更多…");
|
|
void w;
|
|
});
|
|
|
|
it("drops stale responses when the query switches mid-flight", async () => {
|
|
await mountAsyncDemo();
|
|
const input = document.querySelector<HTMLInputElement>(
|
|
'[data-slot="input"]',
|
|
)!;
|
|
input.value = "vader";
|
|
input.dispatchEvent(new Event("input", { bubbles: true }));
|
|
await vi.advanceTimersByTimeAsync(100);
|
|
input.value = "solo";
|
|
input.dispatchEvent(new Event("input", { bubbles: true }));
|
|
await vi.advanceTimersByTimeAsync(800);
|
|
await nextTick();
|
|
const names = optionNames();
|
|
expect(names).toEqual(["Han Solo"]);
|
|
});
|
|
|
|
it("retries a failed page from the error state", async () => {
|
|
const w = await mountAsyncDemo();
|
|
const checkbox = w.get('input[type="checkbox"]');
|
|
await checkbox.setValue(true);
|
|
FakeIntersectionObserver.last().trigger(true);
|
|
await vi.advanceTimersByTimeAsync(350);
|
|
await nextTick();
|
|
// The error row renders inside the teleported popover surface.
|
|
expect(document.body.textContent).toContain("重试");
|
|
expect(optionNames()).toHaveLength(4);
|
|
await checkbox.setValue(false);
|
|
const retry = [...document.querySelectorAll("button")].find(
|
|
(button) => button.textContent?.trim() === "重试",
|
|
)!;
|
|
expect(retry).toBeTruthy();
|
|
retry.click();
|
|
await vi.advanceTimersByTimeAsync(350);
|
|
await nextTick();
|
|
expect(document.body.textContent).not.toContain("重试");
|
|
expect(optionNames()).toHaveLength(8);
|
|
void w;
|
|
});
|
|
});
|