560 lines
19 KiB
TypeScript
560 lines
19 KiB
TypeScript
import { describe, it, expect, afterEach, vi } from "vitest";
|
|
import { mount } from "@vue/test-utils";
|
|
import { defineComponent, h, nextTick, ref } from "vue";
|
|
import { readFileSync } from "node:fs";
|
|
import {
|
|
Description,
|
|
FieldError,
|
|
Form,
|
|
InputGroup,
|
|
InputGroupInput,
|
|
InputGroupPrefix,
|
|
InputGroupSuffix,
|
|
InputGroupTextArea,
|
|
Label,
|
|
TextField,
|
|
} from "../packages/vue/src";
|
|
import { createSSRApp } from "vue";
|
|
import { renderToString } from "@vue/server-renderer";
|
|
import InputGroupDefaultDemo from "../apps/docs/demos/input-group-default.vue";
|
|
import InputGroupPasswordDemo from "../apps/docs/demos/input-group-password-with-toggle.vue";
|
|
import InputGroupCopyDemo from "../apps/docs/demos/input-group-with-copy-suffix.vue";
|
|
|
|
afterEach(() => {
|
|
document.body.innerHTML = "";
|
|
});
|
|
|
|
const settle = async () => {
|
|
await nextTick();
|
|
await nextTick();
|
|
};
|
|
|
|
function groupField(props: Record<string, unknown> = {}, inputProps = {}) {
|
|
return h(TextField, props, () => [
|
|
h(Label, {}, () => "Label"),
|
|
h(InputGroup, {}, () => [
|
|
h(InputGroupPrefix, {}, () => "$"),
|
|
h(InputGroupInput, inputProps),
|
|
h(InputGroupSuffix, {}, () => "USD"),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
describe("InputGroup", () => {
|
|
it("renders group shell with input/prefix/suffix data-slots and classes", () => {
|
|
const w = mount(groupField({ name: "price", defaultValue: "10" }));
|
|
const group = w.get("[data-slot=input-group]");
|
|
expect(group.attributes("role")).toBe("group");
|
|
expect(group.classes()).toContain("hv-input-group");
|
|
expect(group.classes()).toContain("hv-input-group--primary");
|
|
const input = w.get("[data-slot=input-group-input]");
|
|
expect(input.element.tagName).toBe("INPUT");
|
|
expect(input.classes()).toContain("hv-input-group__input");
|
|
expect(input.attributes("name")).toBe("price");
|
|
expect((input.element as HTMLInputElement).value).toBe("10");
|
|
expect(w.get("[data-slot=input-group-prefix]").classes()).toContain(
|
|
"hv-input-group__prefix",
|
|
);
|
|
expect(w.get("[data-slot=input-group-suffix]").classes()).toContain(
|
|
"hv-input-group__suffix",
|
|
);
|
|
});
|
|
|
|
it("inherits TextField variant and allows explicit override", async () => {
|
|
const variant = ref<"primary" | "secondary">("secondary");
|
|
const explicit = ref<"primary" | "secondary" | undefined>(undefined);
|
|
const w = mount(
|
|
defineComponent({
|
|
components: {
|
|
TextField,
|
|
InputGroup,
|
|
InputGroupInput,
|
|
InputGroupPrefix,
|
|
},
|
|
setup: () => ({ variant, explicit }),
|
|
template: `
|
|
<TextField :variant="variant" name="x">
|
|
<InputGroup :variant="explicit">
|
|
<InputGroupPrefix>A</InputGroupPrefix>
|
|
<InputGroupInput />
|
|
</InputGroup>
|
|
</TextField>
|
|
`,
|
|
}),
|
|
);
|
|
expect(w.get("[data-slot=input-group]").classes()).toContain(
|
|
"hv-input-group--secondary",
|
|
);
|
|
explicit.value = "primary";
|
|
await settle();
|
|
expect(w.get("[data-slot=input-group]").classes()).toContain(
|
|
"hv-input-group--primary",
|
|
);
|
|
expect(w.get("[data-slot=input-group]").classes()).not.toContain(
|
|
"hv-input-group--secondary",
|
|
);
|
|
explicit.value = undefined;
|
|
await settle();
|
|
expect(w.get("[data-slot=input-group]").classes()).toContain(
|
|
"hv-input-group--secondary",
|
|
);
|
|
w.unmount();
|
|
});
|
|
|
|
it("applies fullWidth and custom role", () => {
|
|
const w = mount(
|
|
defineComponent({
|
|
components: { InputGroup, InputGroupInput },
|
|
template: `<InputGroup full-width role="region"><InputGroupInput /></InputGroup>`,
|
|
}),
|
|
);
|
|
const group = w.get("[data-slot=input-group]");
|
|
expect(group.classes()).toContain("hv-input-group--full-width");
|
|
expect(group.attributes("role")).toBe("region");
|
|
});
|
|
|
|
it("reflects TextField disabled and invalid on the group shell", async () => {
|
|
const isDisabled = ref(false);
|
|
const isInvalid = ref(false);
|
|
const w = mount(
|
|
defineComponent({
|
|
components: { TextField, InputGroup, InputGroupInput },
|
|
setup: () => ({ isDisabled, isInvalid }),
|
|
template: `
|
|
<TextField :is-disabled="isDisabled" :is-invalid="isInvalid" name="x">
|
|
<InputGroup><InputGroupInput /></InputGroup>
|
|
</TextField>
|
|
`,
|
|
}),
|
|
);
|
|
const group = () => w.get("[data-slot=input-group]");
|
|
expect(group().attributes("data-disabled")).toBeUndefined();
|
|
expect(group().attributes("data-invalid")).toBeUndefined();
|
|
isDisabled.value = true;
|
|
isInvalid.value = true;
|
|
await settle();
|
|
expect(group().attributes("data-disabled")).toBe("true");
|
|
expect(group().attributes("data-invalid")).toBe("true");
|
|
expect(group().attributes("aria-disabled")).toBe("true");
|
|
w.unmount();
|
|
});
|
|
|
|
it("focuses the real input when clicking group padding but not buttons", async () => {
|
|
const w = mount(
|
|
defineComponent({
|
|
components: {
|
|
InputGroup,
|
|
InputGroupInput,
|
|
InputGroupSuffix,
|
|
},
|
|
template: `
|
|
<InputGroup>
|
|
<InputGroupInput />
|
|
<InputGroupSuffix>
|
|
<button type="button" class="action">Go</button>
|
|
</InputGroupSuffix>
|
|
</InputGroup>
|
|
`,
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
const input = w.get("input").element as HTMLInputElement;
|
|
const group = w.get("[data-slot=input-group]");
|
|
await group.trigger("click");
|
|
expect(document.activeElement).toBe(input);
|
|
input.blur();
|
|
await w.get("button.action").trigger("click");
|
|
expect(document.activeElement).not.toBe(input);
|
|
w.unmount();
|
|
});
|
|
|
|
it("keeps prefix/suffix out of FormData and submits only the real input", async () => {
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, {}, () => [
|
|
groupField({ name: "amount", defaultValue: "3" }),
|
|
]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
const input = w.get("input");
|
|
await input.setValue("9");
|
|
const data = Object.fromEntries(
|
|
new FormData(w.get("form").element as HTMLFormElement),
|
|
);
|
|
expect(data).toEqual({ amount: "9" });
|
|
expect(Object.keys(data)).toEqual(["amount"]);
|
|
w.unmount();
|
|
});
|
|
|
|
it("resets uncontrolled values and restores controlled rejection", async () => {
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, {}, () => [groupField({ name: "n", defaultValue: "start" })]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
const input = w.get("input");
|
|
await input.setValue("changed");
|
|
(w.get("form").element as HTMLFormElement).reset();
|
|
await settle();
|
|
expect((input.element as HTMLInputElement).value).toBe("start");
|
|
w.unmount();
|
|
});
|
|
|
|
it("does not treat suffix buttons as form submitters", async () => {
|
|
const submit = vi.fn((e: Event) => e.preventDefault());
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h("form", { onSubmit: submit }, [
|
|
h(InputGroup, {}, () => [
|
|
h(InputGroupInput, { name: "q" }),
|
|
h(InputGroupSuffix, {}, () => [
|
|
h("button", { type: "button", class: "copy-btn" }, "Copy"),
|
|
]),
|
|
]),
|
|
]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
await w.get("button.copy-btn").trigger("click");
|
|
expect(submit).not.toHaveBeenCalled();
|
|
expect(w.get("button.copy-btn").attributes("type")).toBe("button");
|
|
w.unmount();
|
|
});
|
|
|
|
it("supports textarea child and keeps value/reset protocol", async () => {
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, {}, () => [
|
|
h(TextField, { name: "bio", defaultValue: "hello" }, () => [
|
|
h(InputGroup, {}, () => [
|
|
h(InputGroupPrefix, {}, () => ">"),
|
|
h(InputGroupTextArea, { rows: 3 }),
|
|
h(InputGroupSuffix, {}, () => "!"),
|
|
]),
|
|
]),
|
|
]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
const area = w.get("[data-slot=input-group-textarea]");
|
|
expect(area.element.tagName).toBe("TEXTAREA");
|
|
expect((area.element as HTMLTextAreaElement).value).toBe("hello");
|
|
await area.setValue("changed");
|
|
expect(
|
|
Object.fromEntries(
|
|
new FormData(w.get("form").element as HTMLFormElement),
|
|
),
|
|
).toEqual({ bio: "changed" });
|
|
(w.get("form").element as HTMLFormElement).reset();
|
|
await settle();
|
|
expect((area.element as HTMLTextAreaElement).value).toBe("hello");
|
|
w.unmount();
|
|
});
|
|
|
|
it("merges caller class on group and input without dropping base classes", async () => {
|
|
const extra = ref<Record<string, unknown>>({});
|
|
const classValue = ref<unknown>("group-custom");
|
|
const w = mount(
|
|
defineComponent({
|
|
components: { InputGroup, InputGroupInput },
|
|
setup: () => ({ extra, classValue }),
|
|
template: `
|
|
<InputGroup v-bind="extra" :class="classValue">
|
|
<InputGroupInput class="input-custom" />
|
|
</InputGroup>
|
|
`,
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
let group = w.get("[data-slot=input-group]");
|
|
expect(group.classes()).toContain("hv-input-group");
|
|
expect(group.classes()).toContain("group-custom");
|
|
expect(w.get("input").classes()).toContain("hv-input-group__input");
|
|
expect(w.get("input").classes()).toContain("input-custom");
|
|
|
|
extra.value = { "data-x": "1", title: "t" };
|
|
classValue.value = ["group-custom", { "group-more": true }];
|
|
await settle();
|
|
group = w.get("[data-slot=input-group]");
|
|
expect(group.attributes("data-x")).toBe("1");
|
|
expect(group.attributes("title")).toBe("t");
|
|
expect(group.classes()).toContain("group-more");
|
|
expect(group.classes()).toContain("hv-input-group");
|
|
|
|
extra.value = {};
|
|
await settle();
|
|
expect(
|
|
w.get("[data-slot=input-group]").attributes("data-x"),
|
|
).toBeUndefined();
|
|
|
|
extra.value = { "aria-label": "search" };
|
|
await settle();
|
|
expect(w.get("[data-slot=input-group]").attributes("aria-label")).toBe(
|
|
"search",
|
|
);
|
|
w.unmount();
|
|
});
|
|
|
|
it("updates hover and focus-within data attributes", async () => {
|
|
const w = mount(InputGroup, {
|
|
slots: { default: () => h(InputGroupInput) },
|
|
attachTo: document.body,
|
|
});
|
|
const group = w.get("[data-slot=input-group]");
|
|
await group.trigger("pointerenter");
|
|
expect(group.attributes("data-hovered")).toBe("true");
|
|
await group.trigger("focusin");
|
|
expect(group.attributes("data-focus-within")).toBe("true");
|
|
await group.trigger("focusout");
|
|
expect(group.attributes("data-focus-within")).toBeUndefined();
|
|
await group.trigger("pointerleave");
|
|
expect(group.attributes("data-hovered")).toBeUndefined();
|
|
w.unmount();
|
|
});
|
|
|
|
it("keeps password toggle from submitting and updates input type/value", async () => {
|
|
const w = mount(InputGroupPasswordDemo, { attachTo: document.body });
|
|
const input = w.get("[data-slot=input-group-input]");
|
|
expect(input.attributes("type")).toBe("password");
|
|
const toggle = w.get("button");
|
|
expect(toggle.attributes("type")).toBe("button");
|
|
expect(toggle.attributes("aria-label")).toBe("显示密码");
|
|
await toggle.trigger("click");
|
|
await settle();
|
|
expect(w.get("[data-slot=input-group-input]").attributes("type")).toBe(
|
|
"text",
|
|
);
|
|
expect(w.get("button").attributes("aria-label")).toBe("隐藏密码");
|
|
w.unmount();
|
|
});
|
|
|
|
it("renders the default and copy demos with library parts", () => {
|
|
const basic = mount(InputGroupDefaultDemo);
|
|
expect(basic.find("[data-slot=input-group]").exists()).toBe(true);
|
|
expect(basic.find("[data-slot=input-group-input]").exists()).toBe(true);
|
|
expect(basic.get("label").text()).toBe("邮箱地址");
|
|
|
|
const copy = mount(InputGroupCopyDemo);
|
|
const btn = copy.get("button");
|
|
expect(btn.attributes("type")).toBe("button");
|
|
expect(btn.attributes("aria-label")).toBe("复制");
|
|
expect(copy.find("[data-slot=input-group-suffix]").exists()).toBe(true);
|
|
});
|
|
|
|
it("keeps public prop types constrained", async () => {
|
|
const { InputGroup: Group, InputGroupInput: Inp } =
|
|
await import("../packages/vue/src");
|
|
// @ts-expect-error variant must be primary | secondary
|
|
const badVariant: InstanceType<typeof Group>["$props"]["variant"] = "ghost";
|
|
// @ts-expect-error role must be group | region | presentation
|
|
const badRole: InstanceType<typeof Group>["$props"]["role"] = "banner";
|
|
void badVariant;
|
|
void badRole;
|
|
const ok: InstanceType<typeof Group>["$props"] = {
|
|
variant: "secondary",
|
|
fullWidth: true,
|
|
role: "group",
|
|
};
|
|
const okInput: InstanceType<typeof Inp>["$props"] = {
|
|
type: "email",
|
|
modelValue: "a@b.c",
|
|
};
|
|
expect(ok.variant).toBe("secondary");
|
|
expect(okInput.type).toBe("email");
|
|
});
|
|
});
|
|
|
|
describe("M12 demo inventory", () => {
|
|
const expected = [
|
|
"default",
|
|
"variants",
|
|
"on-surface",
|
|
"with-loading-suffix",
|
|
"required",
|
|
"disabled",
|
|
"full-width",
|
|
"with-text-prefix",
|
|
"with-text-suffix",
|
|
"with-icon-prefix-and-text-suffix",
|
|
"with-copy-suffix",
|
|
"with-icon-prefix-and-copy-suffix",
|
|
"password-with-toggle",
|
|
"with-keyboard-shortcut",
|
|
"with-badge-suffix",
|
|
"invalid",
|
|
"with-prefix-icon",
|
|
"with-suffix-icon",
|
|
"with-prefix-and-suffix",
|
|
"with-textarea",
|
|
"custom-styles",
|
|
];
|
|
|
|
it("includes every preview from the pinned upstream documents", () => {
|
|
const document = readFileSync(
|
|
"apps/docs/components/input-group.md",
|
|
"utf8",
|
|
);
|
|
const previewNames = [
|
|
...document.matchAll(/<ComponentPreview name="input-group-([^"]+)"/g),
|
|
].map((m) => m[1]);
|
|
expect(previewNames).toEqual(expected);
|
|
for (const name of expected) {
|
|
const source = readFileSync(
|
|
`apps/docs/demos/input-group-${name}.vue`,
|
|
"utf8",
|
|
);
|
|
expect(source).toContain("<template>");
|
|
}
|
|
});
|
|
|
|
it("documents Vue class binding instead of React className", () => {
|
|
const document = readFileSync(
|
|
"apps/docs/components/input-group.md",
|
|
"utf8",
|
|
);
|
|
expect(document).not.toMatch(/\|\s*`className`\s*\|/);
|
|
expect(document).not.toMatch(/\|\s*`children`\s*\|/);
|
|
expect(document).toContain("`class`");
|
|
expect(document).toContain("Vue class 绑定");
|
|
expect(document).toMatch(/string\s*\\\|\s*Array\s*\\\|\s*Object/);
|
|
for (const name of ["default", "custom-styles", "with-textarea"]) {
|
|
const source = readFileSync(
|
|
`apps/docs/demos/input-group-${name}.vue`,
|
|
"utf8",
|
|
);
|
|
expect(source).not.toContain("className");
|
|
expect(source).not.toContain("@gravity-ui/icons");
|
|
expect(source).not.toContain('from "reka-ui"');
|
|
}
|
|
});
|
|
|
|
it("uses Tabler icons and library form parts in demos", () => {
|
|
const demo = readFileSync(
|
|
"apps/docs/demos/input-group-with-copy-suffix.vue",
|
|
"utf8",
|
|
);
|
|
expect(demo).toContain("@tabler/icons-vue");
|
|
expect(demo).toContain("Button");
|
|
expect(demo).not.toContain("<path");
|
|
|
|
const textarea = readFileSync(
|
|
"apps/docs/demos/input-group-with-textarea.vue",
|
|
"utf8",
|
|
);
|
|
expect(textarea).toContain("InputGroupTextArea");
|
|
expect(textarea).not.toContain("Tooltip");
|
|
});
|
|
});
|
|
|
|
describe("InputGroup independent integration regression", () => {
|
|
it("retains controlled values and respects canceled reset", async () => {
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, { onReset: (e: Event) => e.preventDefault() }, () => [
|
|
groupField({ modelValue: "fixed", name: "controlled" }),
|
|
groupField({ defaultValue: "initial", name: "editable" }),
|
|
]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
const [controlled, editable] = w.findAll("input");
|
|
await controlled.setValue("rejected");
|
|
await settle();
|
|
expect((controlled.element as HTMLInputElement).value).toBe("fixed");
|
|
await editable.setValue("retained");
|
|
(w.get("form").element as HTMLFormElement).reset();
|
|
await settle();
|
|
expect((editable.element as HTMLInputElement).value).toBe("retained");
|
|
w.unmount();
|
|
});
|
|
|
|
it("enforces native constraints while aria mode permits the submit callback", async () => {
|
|
const mode = ref<"native" | "aria">("native");
|
|
const submit = vi.fn((e: Event) => e.preventDefault());
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, { onSubmit: submit }, () => [
|
|
h(
|
|
TextField,
|
|
{ name: "q", isRequired: true, validationBehavior: mode.value },
|
|
() => [
|
|
h(InputGroup, {}, () => [h(InputGroupInput)]),
|
|
h(FieldError),
|
|
],
|
|
),
|
|
]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
(w.get("form").element as HTMLFormElement).requestSubmit();
|
|
await settle();
|
|
expect(submit).not.toHaveBeenCalled();
|
|
expect(w.get("input").attributes("aria-invalid")).toBe("true");
|
|
expect(w.get("[data-slot=input-group]").attributes("data-invalid")).toBe(
|
|
"true",
|
|
);
|
|
mode.value = "aria";
|
|
await settle();
|
|
(w.get("form").element as HTMLFormElement).requestSubmit();
|
|
await settle();
|
|
expect(submit).toHaveBeenCalledOnce();
|
|
w.unmount();
|
|
});
|
|
|
|
it("hydrates explicit IDs and keeps dynamic slots, labels and descriptions current", async () => {
|
|
const id = ref("group-a");
|
|
const show = ref(true);
|
|
const view = defineComponent({
|
|
render: () =>
|
|
h(TextField, {}, () => [
|
|
h(Label, {}, () => "Email"),
|
|
h(InputGroup, {}, () => [
|
|
show.value ? h(InputGroupPrefix, {}, () => "@") : null,
|
|
h(InputGroupInput, { id: id.value }),
|
|
]),
|
|
h(Description, {}, () => "Hint"),
|
|
]),
|
|
});
|
|
const host = document.createElement("div");
|
|
host.innerHTML = await renderToString(createSSRApp(view));
|
|
document.body.append(host);
|
|
const errors = vi.spyOn(console, "error");
|
|
const warnings = vi.spyOn(console, "warn");
|
|
const app = createSSRApp(view);
|
|
try {
|
|
app.mount(host);
|
|
await settle();
|
|
expect(host.querySelector("label")?.htmlFor).toBe("group-a");
|
|
id.value = "group-b";
|
|
show.value = false;
|
|
await settle();
|
|
expect(host.querySelector("label")?.htmlFor).toBe("group-b");
|
|
expect(host.querySelector("input")?.id).toBe("group-b");
|
|
expect(host.querySelector("[data-slot=input-group-prefix]")).toBeNull();
|
|
const refs =
|
|
host
|
|
.querySelector("input")
|
|
?.getAttribute("aria-describedby")
|
|
?.split(" ") ?? [];
|
|
expect(refs.length).toBeGreaterThan(0);
|
|
for (const ref of refs)
|
|
expect(document.getElementById(ref)).not.toBeNull();
|
|
expect(errors).not.toHaveBeenCalled();
|
|
expect(warnings).not.toHaveBeenCalled();
|
|
} finally {
|
|
app.unmount();
|
|
host.remove();
|
|
errors.mockRestore();
|
|
warnings.mockRestore();
|
|
}
|
|
});
|
|
});
|