Files
heroui-vue/tests/button-demos.test.ts
T
maoyingjie 896bac2cd7 fix(docs): restore button, toggle and nav demos to the pinned cn baseline
- button-group: restore all 9 cn demos incl. merge dropdown menu copy,
  custom-styles merge dropdown, sizes variant=secondary, orientation icon
  groups, full-width second group and the disabled child override
- toggle-button / toggle-button-group: restore cn copy, structure, button
  counts, theme custom-styles classes and aria-labels
- accordion-custom-styles: restore the 3-item collectibles scenario with the
  original 3dicons images, surface variant, selectors and hover motion
- disclosure-group-controlled: restore HeroUI Native / Expo copy and image
  semantics (Expo brand icon adapted to Tabler IconBrandReactNative)
- button-basic / button-loading-state: converge to baseline (console report,
  no added status paragraph) keeping the duplicate-trigger guard
- restore per-demo root gap/alignment/wrap classes instead of demo-row
- icon semantics per task card: search glyph kept on 搜索 buttons,
  rotate-back glyph restored (IconRefresh), person-search glyph restored
  (IconUserSearch); accordion item icons sized to the baseline 16px
2026-10-28 09:42:17 +08:00

131 lines
5.1 KiB
TypeScript

import { afterEach, describe, it, expect, vi } from "vitest";
import { mount } from "@vue/test-utils";
import { h } from "vue";
import { readFileSync } from "node:fs";
import { Button } from "../packages/vue/src";
import Basic from "../apps/docs/demos/button-basic.vue";
import LoadingState from "../apps/docs/demos/button-loading-state.vue";
import Ripple from "../apps/docs/demos/button-ripple-effect.vue";
const expected = [
"basic",
"variants",
"sizes",
"with-icons",
"icon-only",
"loading",
"loading-state",
"full-width",
"disabled",
"social",
"render-function",
"custom-variants",
"ripple-effect",
"custom-styles",
];
afterEach(() => vi.useRealTimers());
describe("Button examples", () => {
it("includes every preview in the pinned upstream Button document", () => {
const document = readFileSync("apps/docs/components/button.md", "utf8");
const names = [
...document.matchAll(/<ComponentPreview name="button-([^"]+)"/g),
].map((match) => match[1]);
expect(names).toEqual(expected);
for (const name of names)
expect(
readFileSync(`apps/docs/demos/button-${name}.vue`, "utf8"),
).toContain("<template>");
});
it("keeps the baseline label on press and reports the action instead", async () => {
const log = vi.spyOn(console, "log").mockImplementation(() => {});
const w = mount(Basic, { attachTo: document.body });
const button = w.get("button");
expect(button.text()).toBe("点我");
await button.trigger("click");
await button.trigger("click");
expect(button.text()).toBe("点我");
expect(log).toHaveBeenCalledWith("按钮已按下");
expect(log).toHaveBeenCalledTimes(2);
log.mockRestore();
w.unmount();
});
it("allows upload, suppresses duplicate activation, restores the button and allows another upload", async () => {
vi.useFakeTimers();
const w = mount(LoadingState, { attachTo: document.body });
const button = w.get("button");
expect(button.text()).toBe("上传文件");
expect(button.attributes("disabled")).toBeUndefined();
(button.element as HTMLButtonElement).focus();
await button.trigger("click");
expect(button.text()).toBe("上传中…");
expect(button.attributes("aria-disabled")).toBe("true");
expect(document.activeElement).toBe(button.element);
expect(button.attributes("disabled")).toBeUndefined();
await vi.advanceTimersByTimeAsync(1000);
await button.trigger("click");
await vi.advanceTimersByTimeAsync(1000);
expect(button.text()).toBe("上传文件");
expect(button.attributes("aria-disabled")).toBeUndefined();
expect(w.find("[role=status]").exists()).toBe(false);
expect(w.findAll("p")).toHaveLength(0);
await button.trigger("click");
expect(button.text()).toBe("上传中…");
w.unmount();
expect(vi.getTimerCount()).toBe(0);
});
it("treats loading as pending, not native disabled, while preventing repeat clicks", async () => {
const click = vi.fn();
const w = mount(Button, {
props: { loading: true },
attrs: { onClick: click },
});
await w.trigger("click");
expect(click).not.toHaveBeenCalled();
expect(w.attributes("disabled")).toBeUndefined();
await w.setProps({ loading: false });
await w.trigger("click");
expect(click).toHaveBeenCalledOnce();
});
it("clears hover and pressed states when loading starts and does not restore stale states", async () => {
const w = mount(Button);
await w.trigger("pointerenter");
await w.trigger("pointerdown");
expect(w.attributes("data-hovered")).toBe("true");
expect(w.attributes("data-pressed")).toBe("true");
await w.setProps({ isPending: true });
expect(w.attributes("data-hovered")).toBeUndefined();
expect(w.attributes("data-pressed")).toBeUndefined();
await w.trigger("keydown", { key: "Enter" });
expect(w.attributes("data-pressed")).toBeUndefined();
await w.setProps({ isPending: false });
expect(w.attributes("data-hovered")).toBeUndefined();
expect(w.attributes("data-pressed")).toBeUndefined();
w.unmount();
});
it("merges interaction and classes onto a custom native button and exposes pressed slot state", async () => {
const w = mount(Button, {
props: { asChild: true },
slots: {
default: (state) =>
h(
"button",
{ "data-custom": state.isPressed ? "pressed" : "bar" },
"点按",
),
},
});
expect(w.findAll("button")).toHaveLength(1);
expect(w.get("button").classes()).toContain("hv-button");
await w.get("button").trigger("pointerdown");
expect(w.get("button").attributes("data-custom")).toBe("pressed");
await w.get("button").trigger("pointerup");
expect(w.get("button").attributes("data-custom")).toBe("bar");
});
it("creates a keyboard ripple and removes the finished animation", async () => {
const w = mount(Ripple);
await w.get("button").trigger("keydown", { key: "Enter" });
expect(w.findAll(".ripple-wave")).toHaveLength(1);
await w.get(".ripple-wave").trigger("animationend");
expect(w.findAll(".ripple-wave")).toHaveLength(0);
});
});