Files
heroui-vue/tests/range-calendar-demos.test.ts
T

151 lines
4.9 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, h, type Component } from "vue";
import { readFileSync } from "node:fs";
const demos = import.meta.glob("../apps/docs/demos/range-calendar-*.vue", {
eager: true,
import: "default",
}) as Record<string, Component>;
const expected = [
"basic",
"disabled",
"year-picker",
"default-value",
"controlled",
"min-max-dates",
"unavailable-dates",
"anchor-unavailable-dates",
"weeks-in-month",
"week-view",
"day-view",
"allows-non-contiguous-ranges",
"read-only",
"invalid",
"focused-value",
"with-indicators",
"booking-calendar",
"multiple-months",
"international-calendar",
"custom-styles",
];
const wrappers: ReturnType<typeof mount>[] = [];
afterEach(() => {
wrappers.splice(0).forEach((w) => w.unmount());
document.body.innerHTML = "";
vi.restoreAllMocks();
});
function demo(name: string) {
const w = mount(demos[`../apps/docs/demos/range-calendar-${name}.vue`], {
attachTo: document.body,
});
wrappers.push(w);
return w;
}
describe("RangeCalendar fixed-source documentation", () => {
it("retains all twenty previews in baseline order with public-only demo imports", () => {
const md = readFileSync("apps/docs/components/range-calendar.md", "utf8");
expect(
[...md.matchAll(/name="range-calendar-([^"]+)"/g)].map((m) => m[1]),
).toEqual(expected);
expect(Object.keys(demos)).toHaveLength(20);
for (const key of Object.keys(demos)) {
const source = readFileSync(key.replace("../", ""), "utf8");
expect(source).not.toContain("packages/vue");
expect(source).not.toContain("@heroui/react");
}
});
it("mounts all real compositions together with unique IDs and no warnings", async () => {
const warnings: string[] = [];
const warn = vi
.spyOn(console, "warn")
.mockImplementation((...args) => warnings.push(args.join(" ")));
const w = mount(
defineComponent({
render: () =>
h(
"div",
Object.entries(demos).map(([key, d]) => h(d, { key })),
),
}),
{ attachTo: document.body },
);
wrappers.push(w);
await flushPromises();
expect(w.findAll('[data-slot="range-calendar"]')).toHaveLength(20);
const ids = Array.from(document.querySelectorAll("[id]")).map((e) => e.id);
expect(new Set(ids).size).toBe(ids.length);
expect(warnings).toEqual([]);
expect(warn).not.toHaveBeenCalled();
});
it("sets a full holiday range and clears the controlled value through real actions", async () => {
const w = demo("controlled");
await w
.findAll("button")
.find((b) => b.text() === "设为节假日")!
.trigger("click");
await flushPromises();
expect(w.get('[data-slot="description"]').text()).toContain(
"2025-12-20 -> 2025-12-31",
);
expect(
w.findAll(
'[data-slot="range-calendar-cell"][data-selected]:not([data-outside-month])',
),
).toHaveLength(12);
await w
.findAll("button")
.find((b) => b.text() === "清空")!
.trigger("click");
await flushPromises();
expect(w.get('[data-slot="description"]').text()).toContain("(无)");
expect(w.find("[data-selected]").exists()).toBe(false);
});
it("changes focused month and retains exact baseline default/custom selections", async () => {
const w = demo("focused-value");
await w
.findAll("button")
.find((b) => b.text() === "跳转到圣诞节")!
.trigger("click");
await flushPromises();
expect(w.get('[data-slot="description"]').text()).toContain("2025-12-25");
const d = demo("default-value");
expect(d.get("[data-selection-start]").attributes("data-date")).toBe(
"2025-02-03",
);
expect(d.get("[data-selection-end]").attributes("data-date")).toBe(
"2025-02-12",
);
const c = demo("custom-styles");
expect(c.get("[data-selection-start]").attributes("data-date")).toBe(
"2025-02-08",
);
expect(c.get("[data-selection-end]").attributes("data-date")).toBe(
"2025-02-14",
);
expect(c.get("[data-selection-start]").classes()).toContain(
"data-[selected=true]:bg-success-soft",
);
});
it("shows two offset months, day/week layouts, indicators and the international year trigger", () => {
expect(
demo("multiple-months").findAll('table[data-slot="range-calendar-grid"]'),
).toHaveLength(2);
expect(demo("week-view").findAll("tbody tr")).toHaveLength(1);
expect(
demo("day-view").findAll(
'[data-slot="range-calendar-cell"]:not([disabled])',
),
).toHaveLength(5);
expect(
demo("with-indicators")
.find('[data-slot="range-calendar-cell-indicator"]')
.exists(),
).toBe(true);
expect(
demo("international-calendar")
.get('[data-slot="calendar-year-picker-trigger"]')
.text(),
).toMatch(/[\u0900-\u097f]/);
});
});