Files
heroui-vue/tests/calendar-demos.test.ts
T
2026-10-24 20:59:19 +08:00

67 lines
2.2 KiB
TypeScript

import { afterEach, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { createSSRApp, defineComponent, h, type Component } from "vue";
import { renderToString } from "@vue/server-renderer";
import { CalendarDate } from "@internationalized/date";
import {
Calendar,
CalendarGrid,
CalendarGridHeader,
CalendarGridBody,
} from "../packages/vue/src/index";
const demos = import.meta.glob("../apps/docs/demos/calendar-*.vue", {
eager: true,
import: "default",
}) as Record<string, Component>;
const wrappers: ReturnType<typeof mount>[] = [];
afterEach(() => {
wrappers.splice(0).forEach((w) => w.unmount());
document.body.innerHTML = "";
vi.restoreAllMocks();
});
it("mounts all nineteen baseline demos together without duplicate IDs or Vue warnings", async () => {
expect(Object.keys(demos)).toHaveLength(19);
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
const w = mount(
defineComponent({
render: () =>
h(
"div",
Object.entries(demos).map(([key, demo]) => h(demo, { key })),
),
}),
{ attachTo: document.body },
);
wrappers.push(w);
await flushPromises();
const ids = Array.from(document.querySelectorAll("[id]")).map((e) => e.id);
expect(new Set(ids).size).toBe(ids.length);
expect(warn).not.toHaveBeenCalled();
});
it("hydrates an explicitly dated calendar without replacing the server grid", async () => {
const Root = defineComponent({
render: () =>
h(
Calendar,
{
defaultFocusedValue: new CalendarDate(2024, 2, 10),
locale: "en-US",
timeZone: "UTC",
},
() =>
h(CalendarGrid, () => [h(CalendarGridHeader), h(CalendarGridBody)]),
),
});
const host = document.createElement("div");
host.innerHTML = await renderToString(createSSRApp(Root));
document.body.append(host);
const grid = host.querySelector("table");
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
const app = createSSRApp(Root);
app.mount(host);
await flushPromises();
expect(host.querySelector("table")).toBe(grid);
expect(warn).not.toHaveBeenCalled();
app.unmount();
});