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

148 lines
5.2 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 baseline console feedback", async () => {
const w = mount(
demos["../apps/docs/demos/time-field-form-example.vue"] as never,
);
const log = vi.spyOn(console, "log").mockImplementation(() => {});
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);
// Baseline feedback: the submitted time is logged to the console and
// the field clears; there is no inline output row.
expect(log).toHaveBeenCalledTimes(1);
expect(log.mock.calls[0][0]).toBe("Time submitted:");
expect(String((log.mock.calls[0][1] as { time: unknown }).time)).toBe(
"14:30:45",
);
expect(w.find("output").exists()).toBe(false);
expect((input.element as HTMLInputElement).value).toBe("");
expect(w.get('[data-type="hour"]').attributes("data-placeholder")).toBe(
"true",
);
} finally {
log.mockRestore();
w.unmount();
}
});
});
it.each(["15:30:00", "08:00:00"])(
"keeps the submitted time-field snapshot when pending input changes to %s",
async (changed) => {
const { default: Demo } =
await import("../apps/docs/demos/time-field-form-example.vue");
const w = mount(Demo, { attachTo: document.body });
const log = vi.spyOn(console, "log").mockImplementation(() => {});
vi.useFakeTimers();
try {
const input = w.get("[data-slot=time-field-value]");
await input.setValue("14:30:00");
await input.trigger("blur");
await w.get("form").trigger("submit");
expect(w.get('button[type="submit"]').text()).toContain("…");
await input.setValue(changed);
await input.trigger("blur");
await w.get("form").trigger("submit");
await vi.advanceTimersByTimeAsync(1500);
expect(log).toHaveBeenCalledTimes(1);
expect(log.mock.calls[0][0]).toBe("Time submitted:");
expect(String((log.mock.calls[0][1] as { time: unknown }).time)).toBe(
"14:30:00",
);
expect((input.element as HTMLInputElement).value).toBe("");
} finally {
w.unmount();
log.mockRestore();
vi.useRealTimers();
}
},
);