// @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"'); }); it("renders choice groups and nested content associations without browser globals", async () => { const { CheckboxGroup, Checkbox, CheckboxContent, RadioGroup, Radio, RadioContent, Label, Description, } = await import("../packages/vue/src"); const html = await renderToString( createSSRApp({ render: () => h("form", {}, [ h( CheckboxGroup, { name: "features", defaultValue: ["a"], isRequired: true }, () => [ h(Label, {}, () => "Features"), h(Checkbox, { value: "a" }, () => h(CheckboxContent, {}, () => [ "A", h(Description, {}, () => "Nested hint"), ]), ), ], ), h(RadioGroup, { name: "plan", defaultValue: "pro" }, () => [ h(Label, {}, () => "Plan"), h(Radio, { value: "basic" }, () => h(RadioContent, {}, () => "Basic"), ), h(Radio, { value: "pro" }, () => h(RadioContent, {}, () => "Pro")), ]), ]), }), ); expect(html).toContain('name="features"'); expect(html).toContain('name="plan"'); expect(html).toContain('value="pro" checked'); const ids = [...html.matchAll(/\sid="([^"]+)"/g)].map((m) => m[1]); const refs = [ ...html.matchAll(/aria-(?:labelledby|describedby)="([^"]+)"/g), ].flatMap((m) => m[1].split(" ")); expect(new Set(ids).size).toBe(ids.length); expect(refs.length).toBeGreaterThan(2); for (const id of refs) expect(ids).toContain(id); }); it("renders typography, empty state and breadcrumbs without browser globals", async () => { const { Typography, TypographyHeading, TypographyParagraph, TypographyCode, TypographyProse, EmptyState, Breadcrumbs, BreadcrumbsItem, } = await import("../packages/vue/src"); const html = await renderToString( createSSRApp({ render: () => h("div", {}, [ h(Typography, { type: "h2" }, () => "排版标题"), h(TypographyHeading, { level: 3 }, () => "三级标题"), h(TypographyParagraph, { size: "sm" }, () => "正文"), h(TypographyCode, {}, () => "code"), h(TypographyProse, {}, () => h("p", "富文本段落")), h(EmptyState), h(Breadcrumbs, {}, () => [ h(BreadcrumbsItem, { href: "#home" }, () => "首页"), h(BreadcrumbsItem, null, () => "当前页"), ]), ]), }), ); expect(html).toContain(" { const { Label, Meter, MeterFill, MeterOutput, MeterTrack, ProgressBar, ProgressBarFill, ProgressBarOutput, ProgressBarTrack, ProgressCircle, ProgressCircleFillCircle, ProgressCircleTrack, ProgressCircleTrackCircle, } = await import("../packages/vue/src"); const html = await renderToString( createSSRApp({ render: () => h("div", {}, [ h(ProgressBar, { value: 60 }, () => [ h(Label, null, () => "加载中"), h(ProgressBarOutput), h(ProgressBarTrack, null, () => h(ProgressBarFill)), ]), h( ProgressCircle, { isIndeterminate: true, "aria-label": "加载中" }, () => [ h(ProgressCircleTrack, null, () => [ h(ProgressCircleTrackCircle), h(ProgressCircleFillCircle), ]), ], ), h(Meter, { value: 40, "aria-label": "存储空间" }, () => [ h(MeterOutput), h(MeterTrack, null, () => h(MeterFill)), ]), ]), }), ); expect(html).toContain('role="progressbar"'); expect(html).toContain('aria-valuenow="60"'); expect(html).toContain('aria-valuetext="60%"'); expect(html).toContain("hv-progress-bar__fill"); expect(html).toContain("hv-progress-circle__fill-circle"); // Indeterminate progress must not expose aria-valuenow. expect(html.match(/aria-valuenow/g)).toHaveLength(2); expect(html).toContain('role="meter progressbar"'); expect(html).toContain('aria-valuenow="40"'); expect(html).toContain('data-slot="meter-output"'); // Label association ids are present in the server HTML and resolve to labels. const labelledby = html.match(/aria-labelledby="([^"]+)"/)?.[1]; expect(labelledby).toBeTruthy(); for (const id of labelledby!.split(/\s+/)) { expect(html).toMatch(new RegExp(`id="${id}"[^>]*data-slot="label"`)); } }); it("renders disclosure group and accordion without browser globals", async () => { const { Accordion, AccordionBody, AccordionHeading, AccordionIndicator, AccordionItem, AccordionPanel, AccordionTrigger, Disclosure, DisclosureContent, DisclosureGroup, DisclosureHeading, DisclosureTrigger, } = await import("../packages/vue/src"); const html = await renderToString( createSSRApp({ render: () => h("div", {}, [ h(DisclosureGroup, { defaultExpandedKeys: ["a"] }, () => [ h(Disclosure, { id: "a" }, () => [ h(DisclosureHeading, {}, () => h(DisclosureTrigger, {}, () => "标题A"), ), h(DisclosureContent, {}, () => "内容A"), ]), ]), h(Accordion, { defaultExpandedKeys: ["x"] }, () => [ h(AccordionItem, { id: "x" }, () => [ h(AccordionHeading, {}, () => h(AccordionTrigger, {}, () => "手风琴"), ), h(AccordionPanel, {}, () => h(AccordionBody, {}, () => "正文")), ]), ]), ]), }), ); expect(html).toContain('data-slot="disclosure-group"'); expect(html).toContain('aria-expanded="true"'); expect(html).toContain('data-slot="accordion-item"'); expect(html).toContain("正文"); const controls = [...html.matchAll(/aria-controls="([^"]+)"/g)].map( (m) => m[1], ); expect(controls.length).toBeGreaterThan(0); for (const id of controls) { expect(html).toContain(`id="${id}"`); } }); it("renders overlay state and closed/open roots without accessing browser globals", async () => { const { useOverlayState, ConfigProvider } = await import("../packages/vue/src"); const { default: OverlayDialogRoot } = await import("../packages/vue/src/components/overlay/OverlayDialogRoot.vue"); const { default: OverlayPortal } = await import("../packages/vue/src/components/overlay/OverlayPortal.vue"); const { default: OverlayDialogContent } = await import("../packages/vue/src/components/overlay/OverlayDialogContent.vue"); const { DialogTrigger, DialogTitle, DialogDescription } = await import("../packages/vue/src/components/overlay/primitives"); for (const initiallyOpen of [false, true]) { const html = await renderToString( createSSRApp({ setup() { const state = useOverlayState({ defaultOpen: initiallyOpen }); return () => h( ConfigProvider, { dir: "rtl", locale: "ar", teleportTo: "#portals" }, () => h(OverlayDialogRoot, { state }, () => [ h(DialogTrigger, {}, () => "Open"), h(OverlayPortal, {}, () => h(OverlayDialogContent, {}, () => [ h(DialogTitle, {}, () => "Title"), h(DialogDescription, {}, () => "Hint"), ]), ), ]), ); }, }), ); expect(html).toContain(`aria-expanded="${initiallyOpen}"`); expect(html).not.toContain('role="dialog"'); // Portals deliberately render after client mount. } }); it("renders public Modal Popover and Tooltip roots without browser globals", async () => { const { Modal, ModalBackdrop, ModalContainer, ModalDialog, ModalHeading, Popover, PopoverContent, PopoverDialog, PopoverHeading, Tooltip, TooltipContent, } = await import("../packages/vue/src"); const html = await renderToString( createSSRApp({ render: () => h("div", {}, [ h(Modal, { defaultOpen: true }, () => [ h(Button, {}, () => "Modal"), h(ModalBackdrop, {}, () => h(ModalContainer, {}, () => h(ModalDialog, {}, () => h(ModalHeading, {}, () => "Heading")), ), ), ]), h(Popover, { defaultOpen: true }, () => [ h(Button, {}, () => "Popover"), h(PopoverContent, {}, () => h(PopoverDialog, {}, () => h(PopoverHeading, {}, () => "Heading"), ), ), ]), h(Tooltip, { defaultOpen: true }, () => [ h(Button, {}, () => "Tooltip"), h(TooltipContent, {}, () => "Hint"), ]), ]), }), ); expect(html).toContain('aria-expanded="true"'); expect(html).toContain("aria-describedby="); expect(html).not.toContain('role="dialog"'); expect(html).not.toContain('role="tooltip"'); }); it("renders ScrollShadow and Tabs without browser globals and resolves initial panel IDs", async () => { const { ScrollShadow, Tabs, TabsList, TabsTab, TabsPanel, TabsIndicator } = await import("../packages/vue/src"); const html = await renderToString( createSSRApp({ render: () => h(Tabs, {}, () => [ h(TabsList, { "aria-label": "SSR tabs" }, () => [ h(TabsTab, { value: "a" }, () => ["A", h(TabsIndicator)]), h(TabsTab, { value: "b" }, () => ["B", h(TabsIndicator)]), ]), h(TabsPanel, { value: "a" }, () => h(ScrollShadow, { visibility: "bottom" }, () => "SSR content"), ), h(TabsPanel, { value: "b" }, () => "not selected"), ]), }), ); expect(html).toContain('aria-selected="true"'); expect(html).toContain('data-bottom-scroll="true"'); expect(html).toContain("SSR content"); expect(html).not.toContain("not selected"); const controls = html.match(/aria-controls="([^"]+)"/)?.[1]; expect(controls).toBeTruthy(); expect(html).toContain(`id="${controls}"`); }); it("renders ButtonGroup and Toggle families without browser globals", async () => { const { Button, ButtonGroup, ButtonGroupSeparator, ToggleButton, ToggleButtonGroup, ToggleButtonGroupSeparator, } = await import("../packages/vue/src"); const html = await renderToString( createSSRApp({ render: () => [ h(ButtonGroup, { size: "sm", variant: "secondary" }, () => [ h(Button, () => "First"), h(Button, () => [h(ButtonGroupSeparator), "Second"]), ]), h(ToggleButton, { defaultSelected: true }, () => "Like"), h( ToggleButtonGroup, { selectionMode: "multiple", defaultSelectedKeys: ["bold"] }, () => [ h(ToggleButton, { id: "bold" }, () => "Bold"), h(ToggleButton, { id: "italic" }, () => [ h(ToggleButtonGroupSeparator), "Italic", ]), ], ), ], }), ); expect(html).toContain('data-slot="button-group"'); expect(html).toContain("hv-button--sm"); expect(html).toContain("hv-button--secondary"); expect(html).toContain('data-slot="button-group-separator"'); expect(html).toContain('aria-pressed="true"'); expect(html).toContain('data-slot="toggle-button-group"'); expect(html).toContain('role="toolbar"'); expect(html).toContain('data-selected="true"'); expect(html).toContain('data-slot="toggle-button-group-separator"'); }); it("keeps ToggleButton group keys out of DOM ids on the server", async () => { const { ToggleButton, ToggleButtonGroup } = await import("../packages/vue/src"); const html = await renderToString( createSSRApp({ render: () => h(ToggleButtonGroup, { selectionMode: "multiple" }, () => [ h(ToggleButton, { id: 1 }, () => "number"), h(ToggleButton, { id: "1" }, () => "string"), ]), }), ); expect(html).not.toMatch(/id="1"/); expect(html).toContain("number"); expect(html).toContain("string"); const ids = [...html.matchAll(/id="([^"]+)"/g)].map((m) => m[1]); expect(new Set(ids).size).toBe(ids.length); }); it("keeps standalone ToggleButton DOM ids stable on the server", async () => { const { ToggleButton } = await import("../packages/vue/src"); const html = await renderToString( createSSRApp({ render: () => h(ToggleButton, { id: "like-toggle" }, () => "Like"), }), ); expect(html).toContain('id="like-toggle"'); }); it("renders Toolbar with nested groups without browser globals", async () => { const { Toolbar, Button, ButtonGroup, Separator, ToggleButton, ToggleButtonGroup, } = await import("../packages/vue/src"); const html = await renderToString( createSSRApp({ render: () => h( Toolbar, { "aria-label": "文本格式", orientation: "vertical", isAttached: true, }, () => [ h(ToggleButtonGroup, { selectionMode: "multiple" }, () => [ h(ToggleButton, { id: "bold" }, () => "Bold"), ]), h(Separator), h(ButtonGroup, () => h(Button, () => "Copy")), ], ), }), ); expect(html).toContain('role="toolbar"'); expect(html).toContain('aria-orientation="vertical"'); expect(html).toContain("hv-toolbar--attached"); expect(html).toContain('data-slot="toggle-button-group"'); expect(html).toContain("hv-separator--horizontal"); expect(html).toContain('data-slot="button-group"'); });