feat: initialize Vue component library and aligned documentation
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
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 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("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.get("[role=status]").text()).toContain("上传完成");
|
||||
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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user