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,
Input,
InputGroup,
InputGroupInput,
Label,
TextArea,
TextField,
} from "../packages/vue/src";
afterEach(() => {
document.body.innerHTML = "";
});
const settle = async () => {
await nextTick();
await nextTick();
};
describe("TextArea", () => {
it("renders native textarea with default rows and variant classes", () => {
const w = mount(TextArea, {
props: { rows: 5, variant: "secondary", fullWidth: true },
});
const el = w.get("[data-slot=textarea]");
expect(el.element.tagName).toBe("TEXTAREA");
expect((el.element as HTMLTextAreaElement).rows).toBe(5);
expect(el.classes()).toContain("hv-textarea");
expect(el.classes()).toContain("hv-textarea--secondary");
expect(el.classes()).toContain("hv-textarea--full-width");
});
it("defaults rows to 3", () => {
const w = mount(TextArea);
expect((w.get("textarea").element as HTMLTextAreaElement).rows).toBe(3);
});
it("submits FormData, respects disabled/readonly and resets uncontrolled values", async () => {
const w = mount(
defineComponent({
render: () =>
h(Form, {}, () => [
h(TextField, { name: "bio", defaultValue: "start" }, () => [
h(TextArea),
]),
h(
TextField,
{ name: "off", defaultValue: "no", isDisabled: true },
() => [h(TextArea)],
),
h(
TextField,
{ name: "ro", defaultValue: "yes", isReadOnly: true },
() => [h(TextArea)],
),
]),
}),
{ attachTo: document.body },
);
const areas = w.findAll("textarea");
await areas[0].setValue("changed");
expect(
Object.fromEntries(
new FormData(w.get("form").element as HTMLFormElement),
),
).toEqual({ bio: "changed", ro: "yes" });
(w.get("form").element as HTMLFormElement).reset();
await settle();
expect((areas[0].element as HTMLTextAreaElement).value).toBe("start");
w.unmount();
});
it("restores DOM value when the parent rejects updates", async () => {
const value = ref("fixed");
const w = mount(
defineComponent({
components: { TextField, TextArea },
setup: () => ({ value }),
template: ``,
}),
{ attachTo: document.body },
);
const area = w.get("textarea");
await area.setValue("typing");
await settle();
expect((area.element as HTMLTextAreaElement).value).toBe("fixed");
w.unmount();
});
it("respects canceled form reset", async () => {
const w = mount(
defineComponent({
render: () =>
h(Form, {}, () => [
h(TextField, { name: "t", defaultValue: "init" }, () => [
h(TextArea),
]),
]),
}),
{ attachTo: document.body },
);
const area = w.get("textarea");
await area.setValue("edited");
const form = w.get("form").element as HTMLFormElement;
form.addEventListener("reset", (e) => e.preventDefault());
form.reset();
await settle();
expect((area.element as HTMLTextAreaElement).value).toBe("edited");
w.unmount();
});
it("associates Label, Description and FieldError through TextField", async () => {
const w = mount(
defineComponent({
render: () =>
h(TextField, { isInvalid: true, name: "x" }, () => [
h(Label, {}, () => "Bio"),
h(TextArea, { rows: 2 }),
h(Description, {}, () => "hint"),
h(FieldError),
]),
}),
{ attachTo: document.body },
);
await settle();
const area = w.get("textarea");
const label = w.get("label");
expect(label.attributes("for")).toBe(area.attributes("id"));
const described = area.attributes("aria-describedby")!.split(" ");
expect(described.length).toBeGreaterThanOrEqual(2);
for (const id of described) {
expect(w.find(`[id="${id}"]`).exists()).toBe(true);
}
expect(w.find("[data-slot=field-error]").exists()).toBe(true);
w.unmount();
});
it("keeps InputGroup integration working next to TextArea", async () => {
const w = mount(
defineComponent({
render: () =>
h(Form, {}, () => [
h(TextField, { name: "email", defaultValue: "a@b.c" }, () => [
h(InputGroup, {}, () => [h(InputGroupInput)]),
]),
h(TextField, { name: "note", defaultValue: "hi" }, () => [
h(TextArea, { rows: 2 }),
]),
]),
}),
{ attachTo: document.body },
);
expect(
Object.fromEntries(
new FormData(w.get("form").element as HTMLFormElement),
),
).toEqual({ email: "a@b.c", note: "hi" });
w.unmount();
});
it("keeps public prop types constrained", async () => {
const { TextArea: Area } = await import("../packages/vue/src");
// @ts-expect-error rows must be a number
const badRows: InstanceType["$props"]["rows"] = "3";
// @ts-expect-error variant must be primary | secondary
const badVariant: InstanceType["$props"]["variant"] = "ghost";
void badRows;
void badVariant;
const ok: InstanceType["$props"] = {
rows: 6,
variant: "secondary",
fullWidth: true,
modelValue: "x",
};
expect(ok.rows).toBe(6);
});
});
describe("M13 field docs demos", () => {
const expected: Record = {
textfield: [
"basic",
"on-surface",
"with-description",
"required",
"disabled",
"full-width",
"validation",
"controlled",
"with-error",
"textarea",
"input-types",
"render-function",
"text-field-custom-styles",
],
label: ["basic", "custom-styles"],
description: ["basic", "custom-styles"],
"field-error": ["basic", "custom-styles"],
textarea: [
"basic",
"variants",
"on-surface",
"full-width",
"controlled",
"rows",
"custom-styles",
],
};
const docFiles: Record = {
textfield: "text-field",
label: "label",
description: "description",
"field-error": "field-error",
textarea: "textarea",
};
it("includes every preview from the pinned upstream documents", () => {
for (const [key, names] of Object.entries(expected)) {
const docName = docFiles[key];
const document = readFileSync(
`apps/docs/components/${docName}.md`,
"utf8",
);
const previewNames = [
...document.matchAll(/ m[1]);
const ids =
key === "textfield"
? names.map((n) =>
n === "text-field-custom-styles" ? n : `textfield-${n}`,
)
: names.map((n) => `${key}-${n}`);
expect(previewNames).toEqual(ids);
for (const id of ids) {
const source = readFileSync(`apps/docs/demos/${id}.vue`, "utf8");
expect(source).toContain("");
}
}
});
it("documents Vue class binding and slots instead of React APIs", () => {
for (const docName of [
"text-field",
"label",
"description",
"field-error",
"textarea",
]) {
const document = readFileSync(
`apps/docs/components/${docName}.md`,
"utf8",
);
expect(document).not.toMatch(/\|\s*`className`\s*\|/);
expect(document).not.toMatch(/\|\s*`children`\s*\|/);
expect(document).toContain("`class`");
}
const fieldError = readFileSync(
"apps/docs/components/field-error.md",
"utf8",
);
expect(fieldError).toContain("validationErrors");
expect(fieldError).toContain("不提供完整 React Aria `validationDetails`");
expect(fieldError).toContain("作用域插槽");
});
it("keeps the three shared textfield demos and new pages aligned", () => {
for (const name of [
"textfield-basic",
"textfield-with-description",
"textfield-validation",
]) {
const source = readFileSync(`apps/docs/demos/${name}.vue`, "utf8");
expect(source).toContain("TextField");
}
const fieldsGuide = readFileSync("apps/docs/fields.md", "utf8");
expect(fieldsGuide).toContain("textfield-basic");
// Independent pages exist alongside the infrastructure guide
for (const page of [
"text-field",
"label",
"description",
"field-error",
"textarea",
]) {
expect(readFileSync(`apps/docs/components/${page}.md`, "utf8")).toContain(
"ComponentPreview",
);
}
});
it("uses Tabler-free demos with library parts for field docs", () => {
for (const name of [
"label-basic",
"description-basic",
"field-error-basic",
"textarea-basic",
]) {
const source = readFileSync(`apps/docs/demos/${name}.vue`, "utf8");
expect(source).not.toContain("className");
expect(source).not.toContain("@gravity-ui/icons");
expect(source).toContain("@heroui-vue/components");
}
});
});
function extractVueBlocks(markdown: string): string[] {
return [...markdown.matchAll(/```vue\n([\s\S]*?)```/g)].map((m) => m[1]);
}
function scriptBindings(block: string): Set {
const script =
block.match(/