e34b9debdf
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.
449 lines
14 KiB
TypeScript
449 lines
14 KiB
TypeScript
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);
|
|
});
|