Files
heroui-vue/tests/aligned-components.test.ts
T
2026-10-21 13:04:06 +08:00

181 lines
6.0 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { mount } from "@vue/test-utils";
import { h, nextTick, ref } from "vue";
import { readFileSync } from "node:fs";
import {
Input,
Label,
Switch,
SwitchContent,
SwitchControl,
SwitchThumb,
Description,
FieldError,
} from "../packages/vue/src";
import SwitchForm from "../apps/docs/demos/switch-form.vue";
import InputTypes from "../apps/docs/demos/input-types.vue";
import CardForm from "../apps/docs/demos/card-with-form.vue";
const inventory = {
card: [
"default",
"variants",
"horizontal",
"with-avatar",
"with-images",
"with-form",
"custom-styles",
],
input: [
"basic",
"variants",
"on-surface",
"full-width",
"types",
"controlled",
"custom-styles",
],
link: [
"basic",
"icon-placement",
"underline-and-offset",
"custom-icon",
"render-function",
"custom-styles",
],
spinner: ["basic", "colors", "sizes", "speed", "custom-styles"],
surface: ["basic", "variants", "with-form-components", "custom-styles"],
switch: [
"basic",
"sizes",
"with-icons",
"disabled",
"without-label",
"with-description",
"default-selected",
"controlled",
"label-position",
"group",
"group-horizontal",
"form",
"render-props",
"render-function",
"custom-styles",
],
};
describe("aligned examples", () => {
for (const [component, names] of Object.entries(inventory)) {
it(`${component} preserves the pinned preview inventory and order`, () => {
const text = readFileSync(`apps/docs/components/${component}.md`, "utf8");
expect(
[...text.matchAll(/<ComponentPreview name="([^"]+)"/g)].map(
(m) => m[1],
),
).toEqual(names.map((n) => `${component}-${n}`));
});
}
it("associates each input with its visible label and generates unique IDs per instance", () => {
const w = mount({ render: () => [h(InputTypes), h(InputTypes)] });
const ids = w.findAll("input").map((e) => e.attributes("id"));
expect(new Set(ids).size).toBe(6);
for (const label of w.findAll("label"))
expect(ids).toContain(label.attributes("for"));
w.unmount();
});
it("restores controlled Input DOM when the parent declines an update", async () => {
const w = mount(Input, { props: { modelValue: "固定" } });
await w.get("input").setValue("编辑");
await nextTick();
expect(w.element.value).toBe("固定");
w.unmount();
});
it("submits actual selected values and restores Switch defaults on form reset", async () => {
const w = mount(SwitchForm, { attachTo: document.body });
const form = w.get("form").element as HTMLFormElement;
const switches = w.findAll("[role=switch]");
expect(new FormData(form).get("newsletter")).toBe("on");
await switches[0].trigger("click");
await switches[1].trigger("click");
expect(new FormData(form).get("notifications")).toBe("on");
expect(new FormData(form).has("newsletter")).toBe(false);
await w.get("form").trigger("submit");
expect(w.get("output").text()).toContain("notifications: on");
form.reset();
await nextTick();
await nextTick();
expect(switches[0].attributes("aria-checked")).toBe("false");
expect(switches[1].attributes("aria-checked")).toBe("true");
w.unmount();
});
it("passes root ARIA attributes into composed SwitchContent and preserves container styles", async () => {
const name = ref("静音");
const w = mount({
render: () =>
h(
Switch,
{ "aria-label": name.value, class: "custom", style: "gap:20px" },
() => h(SwitchContent, () => h(SwitchControl, () => h(SwitchThumb))),
),
});
expect(w.get("[role=switch]").attributes("aria-label")).toBe("静音");
expect(w.get("[data-slot=switch]").classes()).toContain("custom");
name.value = "音量";
await nextTick();
expect(w.get("[role=switch]").attributes("aria-label")).toBe("音量");
w.unmount();
});
it("blocks readonly keyboard activation and supports native and ARIA validation", async () => {
const w = mount(Switch, {
attachTo: document.body,
props: {
isReadOnly: true,
isRequired: true,
validate: (v) => (v ? true : "请启用"),
},
slots: {
default: () => [
h(SwitchContent, () => [
h(SwitchControl, () => h(SwitchThumb)),
h(Label, () => "通知"),
]),
h(Description, () => "描述一"),
h(Description, () => "描述二"),
h(FieldError),
],
},
});
const control = w.get("[role=switch]");
await control.trigger("keydown", { key: "Enter" });
expect(w.emitted("change")).toBeUndefined();
const ids = w
.findAll("[data-slot=description]")
.map((e) => e.attributes("id"));
expect(new Set(ids).size).toBe(2);
for (const id of ids)
expect(control.attributes("aria-describedby")).toContain(id);
await w.setProps({ isReadOnly: false });
expect((w.get("input").element as HTMLInputElement).checkValidity()).toBe(
false,
);
await nextTick();
expect(w.get("[role=alert]").text()).toBe("请启用");
expect(document.activeElement).toBe(control.element);
await control.trigger("click");
expect(w.find("[role=alert]").exists()).toBe(false);
await w.setProps({ checked: false, validationBehavior: "aria" });
expect(control.attributes("aria-invalid")).toBe("true");
expect((w.get("input").element as HTMLInputElement).checkValidity()).toBe(
true,
);
w.unmount();
});
it("renders a working Card form using labelled native fields", async () => {
const w = mount(CardForm);
expect(w.findAll("label")).toHaveLength(2);
await w.get("input[type=email]").setValue("test@example.com");
await w.get("input[type=password]").setValue("test-pass");
await w.get("form").trigger("submit");
expect(w.get("[role=status]").text()).toContain("表单提交成功");
w.unmount();
});
});