71 lines
2.4 KiB
TypeScript
71 lines
2.4 KiB
TypeScript
// @vitest-environment node
|
|
import { describe, expect, it } from "vitest";
|
|
import { createSSRApp, defineComponent, h } from "vue";
|
|
import { renderToString } from "@vue/server-renderer";
|
|
import {
|
|
ListBox,
|
|
ListBoxItem,
|
|
ListBoxItemIndicator,
|
|
ListBoxSection,
|
|
Label,
|
|
Description,
|
|
Header,
|
|
} from "../packages/vue/src";
|
|
|
|
describe("ListBox public node SSR", () => {
|
|
it("renders selection, option names, descriptions and section names without browser globals", async () => {
|
|
expect(typeof window).toBe("undefined");
|
|
const app = createSSRApp(
|
|
defineComponent({
|
|
render: () =>
|
|
h(ListBox, { defaultValue: [0], "aria-label": "People" }, () => [
|
|
h(ListBoxSection, {}, () => [
|
|
h(Header, {}, () => "Team"),
|
|
h(ListBoxItem, { id: 0 }, () => [
|
|
h(Label, {}, () => "Bob"),
|
|
h(Description, {}, () => "Details"),
|
|
h(ListBoxItemIndicator),
|
|
]),
|
|
]),
|
|
h(ListBoxItem, { id: "0" }, () => "Fred"),
|
|
]),
|
|
}),
|
|
);
|
|
const html = await renderToString(app);
|
|
expect(html).toContain('aria-selected="true"');
|
|
expect(html).toContain('aria-selected="false"');
|
|
expect(html).toContain('role="listbox"');
|
|
expect(html).toContain("aria-describedby=");
|
|
expect(html).toContain("aria-labelledby=");
|
|
const ids = [...html.matchAll(/ id="([^"]+)"/g)].map((match) => match[1]);
|
|
for (const match of html.matchAll(
|
|
/aria-(?:labelledby|describedby)="([^"]+)"/g,
|
|
))
|
|
for (const id of match[1].split(" ")) expect(ids).toContain(id);
|
|
expect(new Set(ids).size).toBe(ids.length);
|
|
});
|
|
|
|
it("isolates keys/IDs across two lists and independent SSR requests", async () => {
|
|
const app = () =>
|
|
createSSRApp({
|
|
render: () =>
|
|
h(
|
|
"main",
|
|
Array.from({ length: 2 }, () =>
|
|
h(ListBox, { defaultValue: [""] }, () =>
|
|
h(ListBoxItem, { id: "" }, () =>
|
|
h(Label, {}, () => "Empty key"),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
});
|
|
const first = await renderToString(app()),
|
|
second = await renderToString(app());
|
|
expect(first).toBe(second);
|
|
const ids = [...first.matchAll(/ id="([^"]+)"/g)].map((match) => match[1]);
|
|
expect(new Set(ids).size).toBe(ids.length);
|
|
expect(first.match(/aria-selected="true"/g)).toHaveLength(2);
|
|
});
|
|
});
|