chore: remove legacy SSR checks and consolidate migration maintenance

This commit is contained in:
maoyingjie
2026-10-26 10:32:22 +08:00
parent 52a0bef851
commit 60af6ee40b
169 changed files with 958 additions and 8422 deletions
@@ -0,0 +1,123 @@
import { afterEach, expect, it, 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 mounted: { unmount(): void }[] = [];
afterEach(() => {
while (mounted.length) mounted.pop()!.unmount();
vi.restoreAllMocks();
});
const settle = async () => {
await nextTick();
await new Promise((r) => setTimeout(r, 3));
};
function setupList(virtualized = false) {
let collection: OwnedCollection | undefined;
const items = Array.from({ length: 6 }, (_, id) => ({
id,
textValue: `Row ${id}`,
}));
const activeKey = ref<string | number>();
const owner: CollectionOwner = {
keys: computed(() => []),
mode: computed(() => "single"),
disabled: computed(() => false),
readOnly: computed(() => false),
disabledKeys: computed(() => []),
id: "probe",
labelledBy: () => undefined,
ariaLabel: () => "Probe",
request: () => {},
action: () => {},
leave: () => {},
connect: (c) => {
collection = c;
return () => {
collection = undefined;
};
},
filter: (r) => r.key === 3,
virtualFocus: () => true,
activeKey,
};
const wrapper = mount(
defineComponent({
setup() {
provide(collectionOwnerKey, owner);
return () =>
h(
ListBox,
{ items, virtualized, rowHeight: 40, overscan: 8 },
{
default: ({ item }: { item: (typeof items)[number] }) =>
h(ListBoxItem, { id: item.id }, () => item.textValue),
},
);
},
}),
{ attachTo: document.body },
);
mounted.push(wrapper);
return { wrapper, collection: () => collection };
}
it("preserves caller hidden on an ordinary ListBoxItem", async () => {
const w = mount(ListBox, {
slots: {
default: () =>
h(ListBoxItem, { id: "hidden", hidden: true }, () => "Hidden"),
},
});
mounted.push(w);
await settle();
expect(w.find('[role="option"]').attributes("hidden")).toBeDefined();
});
it("reveal scrolls a mounted ordinary row without moving DOM focus", async () => {
const { wrapper, collection } = setupList();
await settle();
const node = wrapper.findAll('[role="option"]')[3].element;
const scroll = vi.fn();
Object.defineProperty(node, "scrollIntoView", {
configurable: true,
value: scroll,
});
await collection()!.reveal!(3);
expect(scroll).toHaveBeenCalled();
});
it("virtual window contains no fixed height slots for filtered-out rows", async () => {
const { wrapper } = setupList(true);
await settle();
expect(
wrapper.findAll(
'[data-slot="list-box-virtual-content"] > [role="presentation"]',
),
).toHaveLength(1);
});
it("restores keyboard highlight intent after a pointer interaction", async () => {
const w = mount(Harness, { attachTo: document.body });
mounted.push(w);
await settle();
const input = w.find("input");
await input.setValue("");
await settle();
await w.findAll('[role="option"]')[0].trigger("pointerdown");
await input.trigger("keydown", { key: "ArrowDown" });
await settle();
expect(w.find('[data-focused="true"]').attributes("data-focus-visible")).toBe(
"true",
);
});
it("has instance-unique names and controls in the verification harness", async () => {
const w = mount(
defineComponent({ setup: () => () => h("div", [h(Harness), h(Harness)]) }),
);
mounted.push(w);
await settle();
const ids = w.findAll("[id]").map((n) => n.attributes("id"));
expect(new Set(ids).size).toBe(ids.length);
});
@@ -0,0 +1,148 @@
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);
});
@@ -0,0 +1,448 @@
import { afterEach, expect, it, vi } from "vitest";
import { mount } from "@vue/test-utils";
import { defineComponent, h, nextTick, ref, type Ref } from "vue";
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Autocomplete,
AutocompleteTrigger,
AutocompleteValue,
AutocompleteClearButton,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteFilter,
Input,
ListBox,
ListBoxItem,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldClearButton,
} from "../../../packages/vue/src";
const roots: { unmount(): void }[] = [];
const tick = async () => {
await nextTick();
await new Promise((r) => setTimeout(r, 6));
};
afterEach(async () => {
while (roots.length) roots.pop()!.unmount();
await tick();
vi.restoreAllMocks();
document.body.innerHTML = "";
});
type Item = { id: string | number; textValue: string };
const defaults: Item[] = [
{ id: 0, textValue: "Numeric Zero" },
{ id: "0", textValue: "String Zero" },
{ id: "a", textValue: "Alpha" },
{ id: "b", textValue: "Beta" },
];
function picker(
kind: "combo" | "auto",
props: () => Record<string, unknown> = () => ({}),
opts: {
items?: Ref<Item[]>;
inputProps?: Record<string, unknown>;
filterProps?: Record<string, unknown>;
} = {},
) {
const items = opts.items ?? ref(defaults);
const w = mount(
defineComponent({
setup: () => () =>
h("form", null, [
h(
kind === "combo" ? ComboBox : Autocomplete,
{ "aria-label": "Picker", ...props() },
{
default: () => [
kind === "combo"
? h(ComboBoxInputGroup, null, {
default: () => [
h(Input, opts.inputProps),
h(ComboBoxTrigger),
],
})
: h(AutocompleteTrigger, null, {
default: () => [
h(AutocompleteValue),
h(AutocompleteClearButton),
h(AutocompleteIndicator),
],
}),
h(
kind === "combo" ? ComboBoxPopover : AutocompletePopover,
null,
{
default: () => {
const list = h(ListBox, null, {
default: () =>
items.value.map((item) =>
h(
ListBoxItem,
{
id: item.id,
key: item.id,
textValue: item.textValue,
},
() => item.textValue,
),
),
});
return kind === "combo"
? list
: h(AutocompleteFilter, opts.filterProps, {
default: () => [
h(
SearchField,
{ "aria-label": "Search" },
{
default: () =>
h(SearchFieldGroup, null, {
default: () => [
h(SearchFieldInput, opts.inputProps),
h(SearchFieldClearButton),
],
}),
},
),
list,
],
});
},
},
),
],
},
),
]),
}),
{ attachTo: document.body },
);
roots.push(w);
return w;
}
const comboInput = () =>
document.querySelector<HTMLInputElement>('[data-slot="input"]')!;
const searchInput = () =>
document.querySelector<HTMLInputElement>('[data-slot="search-field-input"]')!;
async function open(kind: "combo" | "auto") {
if (kind === "combo") comboInput().focus();
else
document
.querySelector<HTMLElement>('[data-slot="autocomplete-trigger"]')!
.click();
await tick();
}
async function type(el: HTMLInputElement, text: string) {
el.value = text;
el.dispatchEvent(new Event("input", { bubbles: true }));
await tick();
}
it("native cancelable clear click actually clears Autocomplete", async () => {
const change = vi.fn();
picker("auto", () => ({ defaultValue: "a", onChange: change }));
await tick();
document
.querySelector<HTMLButtonElement>(
'[data-slot="autocomplete-clear-button"]',
)!
.click();
await tick();
expect(change).toHaveBeenLastCalledWith(null);
});
it.each(["combo", "auto"] as const)(
"%s disabled fields do not submit a value",
async (kind) => {
const w = picker(kind, () => ({
name: "choice",
defaultValue: "a",
isDisabled: true,
}));
await tick();
expect(new FormData(w.element as HTMLFormElement).has("choice")).toBe(
false,
);
},
);
it.each(["combo", "auto"] as const)(
"%s multiple values use same-name entries",
async (kind) => {
const w = picker(kind, () => ({
name: "choice",
selectionMode: "multiple",
defaultValue: ["a", "b"],
}));
await tick();
expect(new FormData(w.element as HTMLFormElement).getAll("choice")).toEqual(
["a", "b"],
);
},
);
it("ComboBox external clear clears displayed text", async () => {
const selected = ref<string | null>("a");
picker("combo", () => ({ modelValue: selected.value }));
await tick();
expect(comboInput().value).toBe("Alpha");
selected.value = null;
await tick();
expect(comboInput().value).toBe("");
});
it("ComboBox keeps numeric and string keys distinct in external text synchronization", async () => {
const selected = ref<string | number>(0);
picker("combo", () => ({ modelValue: selected.value }));
await tick();
selected.value = "0";
await tick();
expect(comboInput().value).toBe("String Zero");
});
it("ComboBox accepts selected label updates without an item-count change", async () => {
const items = ref<Item[]>([{ id: "a", textValue: "Old" }]);
picker("combo", () => ({ modelValue: "a" }), { items });
await tick();
items.value = [{ id: "a", textValue: "New" }];
await tick();
expect(comboInput().value).toBe("New");
});
it("ComboBox respects a controlled empty inputValue", async () => {
picker("combo", () => ({ modelValue: "a", inputValue: "" }));
await tick();
expect(comboInput().value).toBe("");
});
it("ComboBox allows clearing a required selection before validation", async () => {
const update = vi.fn();
picker("combo", () => ({
defaultValue: "a",
isRequired: true,
"onUpdate:modelValue": update,
}));
await tick();
await type(comboInput(), "");
expect(update).toHaveBeenLastCalledWith(null);
expect(comboInput().value).toBe("");
});
it.each(["combo", "auto"] as const)(
"%s asChild uses the supplied root",
async (kind) => {
// The ordinary component tree is the one custom wrapper supplied to the root.
const Root = kind === "combo" ? ComboBox : Autocomplete;
const w = mount(Root, {
props: { asChild: true },
slots: {
default: () => h("section", { "data-custom": "root" }, "Custom"),
},
attachTo: document.body,
});
roots.push(w);
await tick();
expect(w.element.tagName).toBe("SECTION");
},
);
it("ComboBox autoFocus focuses its composed input", async () => {
picker("combo", () => ({ autoFocus: true }));
await tick();
expect(document.activeElement).toBe(comboInput());
});
it("ComboBox secondary variant reaches its input", async () => {
picker("combo", () => ({ variant: "secondary" }));
await tick();
expect(comboInput().classList.contains("hv-input--secondary")).toBe(true);
});
it("ComboBox preserves caller input component ref", async () => {
const inputRef = ref();
picker("combo", () => ({}), { inputProps: { ref: inputRef } });
await tick();
expect(inputRef.value).toBeTruthy();
});
it("Autocomplete preserves caller search-input component ref", async () => {
const inputRef = ref();
picker("auto", () => ({}), { inputProps: { ref: inputRef } });
await tick();
await open("auto");
expect(inputRef.value).toBeTruthy();
});
it("Autocomplete search clear resets its filter", async () => {
picker("auto");
await tick();
await open("auto");
await type(searchInput(), "Beta");
expect(
document.querySelectorAll('[role="option"]:not([data-filtered])').length,
).toBe(1);
document
.querySelector<HTMLButtonElement>(
'[data-slot="search-field-clear-button"]',
)!
.click();
await tick();
expect(searchInput().value).toBe("");
expect(
document.querySelectorAll('[role="option"]:not([data-filtered])').length,
).toBe(4);
});
it("ComboBox respects an active composition session even on an unflagged Enter", async () => {
const change = vi.fn();
picker("combo", () => ({ onChange: change }));
await tick();
await open("combo");
comboInput().dispatchEvent(
new CompositionEvent("compositionstart", { bubbles: true }),
);
comboInput().dispatchEvent(
new KeyboardEvent("keydown", {
key: "Enter",
bubbles: true,
cancelable: true,
}),
);
await tick();
expect(change).not.toHaveBeenCalled();
});
it.each(["combo", "auto"] as const)(
"%s does not filter during a composition session",
async (kind) => {
picker(kind);
await tick();
await open(kind);
const input = kind === "combo" ? comboInput() : searchInput();
input.dispatchEvent(
new CompositionEvent("compositionstart", { bubbles: true }),
);
input.value = "qqq";
input.dispatchEvent(
new InputEvent("input", { bubbles: true, isComposing: true }),
);
await tick();
expect(
document.querySelectorAll('[role="option"]:not([data-filtered])').length,
).toBe(4);
},
);
it("Autocomplete keeps filter focus when parent rejects closing", async () => {
picker("auto", () => ({ open: true }));
await tick();
const input = searchInput();
input.focus();
await type(input, "Beta");
input.dispatchEvent(
new KeyboardEvent("keydown", {
key: "Enter",
bubbles: true,
cancelable: true,
}),
);
await tick();
expect(
document
.querySelector('[data-slot="autocomplete-trigger"]')!
.getAttribute("aria-expanded"),
).toBe("true");
expect(document.activeElement).toBe(input);
});
it("Autocomplete preserves an explicit search input accessible name", async () => {
picker("auto", () => ({}), {
inputProps: { "aria-label": "Specific search" },
});
await tick();
await open("auto");
expect(searchInput().getAttribute("aria-label")).toBe("Specific search");
});
it("ComboBox input participates in its explicitly associated form", async () => {
const external = document.createElement("form");
external.id = "external-form";
document.body.appendChild(external);
picker("combo", () => ({ form: "external-form", name: "choice" }));
await tick();
expect(comboInput().form).toBe(external);
});
it("Autocomplete clear button retains a caller disabled attribute", async () => {
const w = mount(Autocomplete, {
props: { defaultValue: "a" },
slots: {
default: () =>
h(AutocompleteTrigger, null, {
default: () => [
h(AutocompleteValue),
h(AutocompleteClearButton, { disabled: true }),
],
}),
},
attachTo: document.body,
});
roots.push(w);
await tick();
expect(
document.querySelector<HTMLButtonElement>(
'[data-slot="autocomplete-clear-button"]',
)!.disabled,
).toBe(true);
});
it.each(["combo", "auto"] as const)(
"%s does not submit a removed selection as a valid option",
async (kind) => {
const items = ref<Item[]>([...defaults]);
const w = picker(kind, () => ({ defaultValue: "a", name: "choice" }), {
items,
});
await tick();
items.value = items.value.filter((i) => i.id !== "a");
await tick();
expect(new FormData(w.element as HTMLFormElement).get("choice")).not.toBe(
"a",
);
},
);
it("Autocomplete default base-sensitivity filter matches accents", async () => {
picker("auto", () => ({}), {
items: ref([{ id: "cafe", textValue: "Café" }]),
});
await tick();
await open("auto");
await type(searchInput(), "cafe");
expect(
document.querySelectorAll('[role="option"]:not([data-filtered])').length,
).toBe(1);
});
it("Autocomplete default indicator matches its size selector", async () => {
picker("auto");
await tick();
// The shipped rule requires class and data-slot on the same element.
expect(
document.querySelector(
'.hv-autocomplete__indicator[data-slot="autocomplete-default-indicator"]',
),
).not.toBeNull();
});
it("ComboBox custom text is the submitted text after editing a selected item", async () => {
const w = picker("combo", () => ({
defaultValue: "a",
allowsCustomValue: true,
name: "choice",
}));
await tick();
await type(comboInput(), "Custom animal");
expect(new FormData(w.element as HTMLFormElement).get("choice")).toBe(
"Custom animal",
);
});
it("ComboBox trigger retains a caller disabled attribute", async () => {
const w = mount(ComboBox, {
slots: {
default: () =>
h(ComboBoxInputGroup, null, {
default: () => [h(Input), h(ComboBoxTrigger, { disabled: true })],
}),
},
attachTo: document.body,
});
roots.push(w);
await tick();
expect(
document.querySelector<HTMLButtonElement>(
'[data-slot="combo-box-trigger"]',
)!.disabled,
).toBe(true);
});