Files
heroui-vue/tests/select-ssr.test.ts
T

87 lines
2.8 KiB
TypeScript

// @vitest-environment node
import { describe, expect, it } from "vitest";
import { createSSRApp, h } from "vue";
import { renderToString } from "@vue/server-renderer";
import {
Select,
SelectTrigger,
SelectValue,
SelectPopover,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Label,
Description,
} from "../packages/vue/src";
function select() {
return h(
Select,
{ defaultValue: [0, "0", ""], selectionMode: "multiple", name: "choices" },
() => [
h(Label, {}, () => "选择"),
h(SelectTrigger, {}, () => h(SelectValue)),
h(Description, {}, () => "帮助"),
h(SelectPopover, {}, () =>
h(ListBox, {}, () => [
h(ListBoxItem, { id: 0 }, () => [
h(Label, {}, () => "Number"),
h(Description, {}, () => "Excluded"),
h(ListBoxItemIndicator),
]),
h(ListBoxItem, { id: "0" }, () => "String"),
h(ListBoxItem, { id: "" }, () => "Empty"),
]),
),
],
);
}
describe("Select server rendering", () => {
it("renders closed metadata and repeated native selected values without browser globals", async () => {
expect(typeof window).toBe("undefined");
const html = await renderToString(
createSSRApp({ render: () => h("form", [select(), select()]) }),
);
expect(html.match(/Number, String, Empty/g)).toHaveLength(2);
expect(html.match(/data-index="\d" selected/g)).toHaveLength(6);
expect(html).not.toContain('role="listbox"');
expect(html).not.toContain("Excluded");
const ids = [...html.matchAll(/ id="([^"]+)"/g)].map((m) => m[1]);
expect(new Set(ids).size).toBe(ids.length);
for (const match of html.matchAll(
/aria-(?:labelledby|describedby)="([^"]+)"/g,
))
for (const id of match[1].split(" ")) expect(ids).toContain(id);
});
it("uses explicit metadata for opaque options and isolates SSR request IDs", async () => {
const app = () =>
createSSRApp({
render: () =>
h(
Select,
{ defaultValue: 0, items: [{ id: 0, textValue: "Known" }] },
() => h(SelectTrigger, {}, () => h(SelectValue)),
),
});
const first = await renderToString(app());
expect(first).toContain("Known");
expect(first).toBe(await renderToString(app()));
});
it("rejects duplicate or malformed keys before mounting", async () => {
const app = (items: { id: string | number; textValue: string }[]) =>
createSSRApp({
render: () =>
h(Select, { items }, () =>
h(SelectTrigger, {}, () => h(SelectValue)),
),
});
await expect(
renderToString(
app([
{ id: "a", textValue: "A" },
{ id: "a", textValue: "B" },
]),
),
).rejects.toThrow("Duplicate collection key");
});
});