Files
heroui-vue/tests/time-field-demos.test.ts
T
maoyingjie 53e76f676d fix(demos): restore form, overlay and toast demos to the pinned cn baseline
HD-02 scope, all against heroui ce90ea3 cn sources:

- card: add the text-primary class to the coin icons (source parity);
  card-with-form drops the added required validation and inline business
  note, restores the plain href="#" forgot-password link and the baseline
  alert("表单提交成功!") submit feedback (autocomplete kept: no visible
  impact, allowed by the task card)
- input-group-with-textarea: restore the four Tooltips and the Enter Kbd
  hint; drop the stale "Tooltip 组件尚未迁移" note from input-group.md
- alert-dialog: restore the h-px top light line in custom-styles and the
  truncated/missing description text in sizes/custom-portal/
  custom-animations (+ controlled); align the fluid-slide icon with the
  modal mapping (IconArrowBarUp for gravity-ui:arrow-up-from-line)
- toast: restore the upstream demo layout shells (variants/simple/
  custom-indicator/custom-queue) and the action button default variant:
  ToastActionButton now falls back to the Button defaults (primary/md)
  like upstream ToastActionButton, so variants' actions render primary,
  danger stays danger and default stays tertiary; toast.md defaults fixed
- submit demos: restore baseline feedback and structure — alert() for
  card/fieldset/form/checkbox/checkbox-group/switch/select/combo-box/
  autocomplete and console.log for search/number/time field form examples;
  modal-with-form returns to the close-only footer (no submit/result row);
  drop the added inline status/result rows and the max-w-full/
  max-sm:min-w-0 width guards from the restored demos
- radio-group-validation: add the missing text-muted to the result line;
  label.md invalid example gives the real control its invalid state and
  matches the baseline input shape

Tests re-pinned to the baseline contracts (alert/console/close behavior,
four tooltips + Kbd, positional modal footer); full suite 110 files /
1617 tests, typecheck, build:docs and the m35-m36 tarball contract pass.
2026-10-28 14:10:10 +08:00

116 lines
4.1 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();
}
});
});