147 lines
4.6 KiB
TypeScript
147 lines
4.6 KiB
TypeScript
// @vitest-environment node
|
|
import { it, expect } from "vitest";
|
|
import { createSSRApp, h } from "vue";
|
|
import { renderToString } from "@vue/server-renderer";
|
|
import {
|
|
Button,
|
|
Card,
|
|
Input,
|
|
Switch,
|
|
Surface,
|
|
Link,
|
|
LinkIcon,
|
|
Spinner,
|
|
TextArea,
|
|
} from "../packages/vue/src";
|
|
it("renders the initial components without browser globals", async () => {
|
|
const html = await renderToString(
|
|
createSSRApp({
|
|
render: () =>
|
|
h(Card, () => [
|
|
h(Button, () => "保存"),
|
|
h(Input, { modelValue: "名称" }),
|
|
h(Switch, { checked: true, "aria-label": "通知" }),
|
|
h(Surface, { variant: "secondary" }, () => h("span", "表面")),
|
|
h(Link, { href: "#" }, () => ["链接", h(LinkIcon)]),
|
|
h(Spinner),
|
|
h(TextArea, { modelValue: "备注" }),
|
|
]),
|
|
}),
|
|
);
|
|
expect(html).toContain("保存");
|
|
expect(html).toContain('value="名称"');
|
|
expect(html).toContain('role="switch"');
|
|
expect(html).toContain("hv-surface--secondary");
|
|
expect(html).toContain('data-slot="link"');
|
|
expect(html).toContain('role="status"');
|
|
expect(html).toContain('data-slot="textarea"');
|
|
});
|
|
|
|
it("renders complete field associations without browser globals", async () => {
|
|
const { TextField, Label, Description, FieldError, Form } =
|
|
await import("../packages/vue/src");
|
|
const html = await renderToString(
|
|
createSSRApp({
|
|
render: () =>
|
|
h(Form, {}, () => [
|
|
h(
|
|
TextField,
|
|
{ name: "email", defaultValue: "a@example.com", isInvalid: true },
|
|
() => [
|
|
h(Label, {}, () => "Email"),
|
|
h(Input),
|
|
h(Description, {}, () => "Hint"),
|
|
h(FieldError, {}, () => "Error"),
|
|
],
|
|
),
|
|
h(TextField, { name: "notes" }, () => [
|
|
h(Label, {}, () => "Notes"),
|
|
h(TextArea),
|
|
h(Description, {}, () => "Other hint"),
|
|
]),
|
|
]),
|
|
}),
|
|
);
|
|
const ids = [...html.matchAll(/\sid="([^"]+)"/g)].map((match) => match[1]);
|
|
expect(new Set(ids).size).toBe(ids.length);
|
|
const refs = [
|
|
...html.matchAll(/(?:for|aria-describedby)="([^"]+)"/g),
|
|
].flatMap((match) => match[1].split(" "));
|
|
expect(refs.length).toBeGreaterThan(3);
|
|
for (const id of refs) expect(ids).toContain(id);
|
|
expect(html).toContain('value="a@example.com"');
|
|
});
|
|
|
|
it("associates labels with explicit grouped control IDs on the server", async () => {
|
|
const {
|
|
TextField,
|
|
Label,
|
|
InputGroup,
|
|
InputGroupInput,
|
|
InputGroupTextArea,
|
|
Description,
|
|
} = await import("../packages/vue/src");
|
|
const html = await renderToString(
|
|
createSSRApp({
|
|
render: () =>
|
|
h("div", {}, [
|
|
h(TextField, {}, () => [
|
|
h(Label, {}, () => "Email"),
|
|
h(InputGroup, {}, () => [
|
|
h(InputGroupInput, { id: "grouped-email" }),
|
|
]),
|
|
h(Description, {}, () => "Hint"),
|
|
]),
|
|
h(TextField, {}, () => [
|
|
h(Label, {}, () => "Notes"),
|
|
h(InputGroup, {}, () => [
|
|
h(InputGroupTextArea, { id: "grouped-notes" }),
|
|
]),
|
|
]),
|
|
]),
|
|
}),
|
|
);
|
|
expect(html).toContain('for="grouped-email"');
|
|
expect(html).toContain('for="grouped-notes"');
|
|
const ids = [...html.matchAll(/\sid="([^"]+)"/g)].map((m) => m[1]);
|
|
const refs = [
|
|
...html.matchAll(/(?:for|aria-describedby)="([^"]+)"/g),
|
|
].flatMap((m) => m[1].split(" "));
|
|
expect(new Set(ids).size).toBe(ids.length);
|
|
for (const id of refs) expect(ids).toContain(id);
|
|
});
|
|
|
|
it("renders disabled fieldsets and search fields without browser globals", async () => {
|
|
const {
|
|
Fieldset,
|
|
FieldsetLegend,
|
|
SearchField,
|
|
SearchFieldGroup,
|
|
SearchFieldInput,
|
|
SearchFieldClearButton,
|
|
Label,
|
|
Description,
|
|
} = await import("../packages/vue/src");
|
|
const html = await renderToString(
|
|
createSSRApp({
|
|
render: () =>
|
|
h(Fieldset, { disabled: true }, () => [
|
|
h(FieldsetLegend, {}, () => h(Button, {}, () => "Exempt")),
|
|
h(SearchField, { defaultValue: "query", name: "q" }, () => [
|
|
h(Label, {}, () => "Query"),
|
|
h(SearchFieldGroup, {}, () => [
|
|
h(SearchFieldInput, { id: "ssr-search" }),
|
|
h(SearchFieldClearButton),
|
|
]),
|
|
h(Description, {}, () => "Hint"),
|
|
]),
|
|
]),
|
|
}),
|
|
);
|
|
expect(html).toContain('for="ssr-search"');
|
|
expect(html).toContain('type="search"');
|
|
expect(html).toContain('value="query"');
|
|
expect(html).toContain('data-slot="search-field-clear-button"');
|
|
expect(html).toContain('data-disabled="true"');
|
|
});
|