Files
heroui-vue/tests/time-field-demos.test.ts
T

107 lines
3.6 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { readFileSync } from "node:fs";
const demos = import.meta.glob("../apps/docs/demos/time-field-*.vue", {
eager: true,
import: "default",
});
const expected = [
"basic",
"with-prefix-icon",
"with-suffix-icon",
"with-prefix-and-suffix",
"on-surface",
"with-description",
"required",
"disabled",
"full-width",
"invalid",
"controlled",
"form-example",
"with-validation",
"render-function",
"custom-styles",
];
afterEach(() => vi.useRealTimers());
describe("TimeField documentation", () => {
it("retains the fifteen fixed-source previews in the same order", () => {
const md = readFileSync("apps/docs/components/time-field.md", "utf8");
expect(
[...md.matchAll(/name="time-field-([^"]+)"/g)].map((m) => m[1]),
).toEqual(expected);
expect(Object.keys(demos)).toHaveLength(15);
expect(md).not.toContain("@heroui/react");
expect(md).not.toContain("继承 React Aria");
});
it("mounts every real preview without warnings or missing time controls", async () => {
const warnings: string[] = [];
for (const demo of Object.values(demos)) {
const w = mount(demo as never, {
global: { config: { warnHandler: (m) => warnings.push(m) } },
});
try {
await flushPromises();
expect(w.find('[data-slot="time-field"]').exists()).toBe(true);
expect(w.find('[data-type="hour"]').exists()).toBe(true);
expect(w.find('[data-type="year"]').exists()).toBe(false);
} finally {
w.unmount();
}
}
expect(warnings).toEqual([]);
});
it("sets and clears the controlled value through real action buttons", async () => {
const w = mount(
demos["../apps/docs/demos/time-field-controlled.vue"] as never,
);
try {
await w.findAll("button")[0].trigger("click");
await flushPromises();
expect(
(w.get('[data-slot="time-field-value"]').element as HTMLInputElement)
.value,
).toMatch(/^\d{2}:\d{2}:\d{2}/);
expect(w.get('[data-slot="description"]').text()).not.toContain("(空)");
await w.findAll("button")[1].trigger("click");
await flushPromises();
expect(w.get('[data-slot="description"]').text()).toContain("(空)");
expect(w.get('[data-type="hour"]').attributes("data-placeholder")).toBe(
"true",
);
} finally {
w.unmount();
}
});
it("validates business hours and submits the actual value with loading/reset feedback", async () => {
const w = mount(
demos["../apps/docs/demos/time-field-form-example.vue"] as never,
);
try {
const input = w.get('[data-slot="time-field-value"]');
await input.setValue("08:30");
await flushPromises();
expect(w.get('[data-slot="field-error"]').text()).toContain("9:00");
expect((w.get("button").element as HTMLButtonElement).disabled).toBe(
true,
);
await input.setValue("14:30:45");
await flushPromises();
expect(w.find('[data-slot="field-error"]').exists()).toBe(false);
expect((w.get("button").element as HTMLButtonElement).disabled).toBe(
false,
);
vi.useFakeTimers();
await w.get("form").trigger("submit");
expect(w.get("button").text()).toBe("提交中…");
await vi.advanceTimersByTimeAsync(1500);
expect(w.get("output").text()).toBe("已提交:14:30:45");
expect((input.element as HTMLInputElement).value).toBe("");
expect(w.get('[data-type="hour"]').attributes("data-placeholder")).toBe(
"true",
);
} finally {
w.unmount();
}
});
});