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 = {}, 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: ` A `, }), ); 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: ``, }), ); 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: ` `, }), ); 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: ` `, }), { 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>({}); const classValue = ref("group-custom"); const w = mount( defineComponent({ components: { InputGroup, InputGroupInput }, setup: () => ({ extra, classValue }), template: ` `, }), { 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["$props"]["variant"] = "ghost"; // @ts-expect-error role must be group | region | presentation const badRole: InstanceType["$props"]["role"] = "banner"; void badVariant; void badRole; const ok: InstanceType["$props"] = { variant: "secondary", fullWidth: true, role: "group", }; const okInput: InstanceType["$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(/ 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("