feat: initialize Vue component library and aligned documentation
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { Button, Input, Switch } from "../packages/vue/src";
|
||||
describe("Button", () => {
|
||||
it("defaults to a non-submit button and blocks loading interactions", async () => {
|
||||
const w = mount(Button, {
|
||||
props: { loading: true },
|
||||
slots: { default: "保存" },
|
||||
});
|
||||
expect(w.attributes("type")).toBe("button");
|
||||
expect(w.attributes("disabled")).toBeUndefined();
|
||||
expect(w.attributes("data-pending")).toBe("true");
|
||||
expect(w.attributes("aria-busy")).toBe("true");
|
||||
});
|
||||
});
|
||||
describe("Input", () => {
|
||||
it("emits values without mutating a controlled value", async () => {
|
||||
const w = mount(Input, {
|
||||
props: { modelValue: "原值" },
|
||||
attrs: { "aria-label": "名称", name: "name" },
|
||||
});
|
||||
await w.get("input").setValue("新值");
|
||||
expect(w.emitted("update:modelValue")).toEqual([["新值"]]);
|
||||
await w.setProps({ modelValue: "父级值" });
|
||||
expect(w.element.value).toBe("父级值");
|
||||
expect(w.attributes("name")).toBe("name");
|
||||
});
|
||||
it("supports uncontrolled initial values and native form reset", async () => {
|
||||
const form = document.createElement("form");
|
||||
document.body.append(form);
|
||||
const w = mount(Input, { props: { defaultValue: "初始" }, attachTo: form });
|
||||
await w.get("input").setValue("编辑");
|
||||
expect(w.element.value).toBe("编辑");
|
||||
form.reset();
|
||||
await w.vm.$nextTick();
|
||||
expect(w.element.value).toBe("初始");
|
||||
w.unmount();
|
||||
form.remove();
|
||||
});
|
||||
});
|
||||
describe("Switch", () => {
|
||||
it("emits the checked model and exposes accessible state", async () => {
|
||||
const w = mount(Switch, {
|
||||
props: { checked: false },
|
||||
attrs: { "aria-label": "通知" },
|
||||
});
|
||||
expect(w.get("[role=switch]").attributes("role")).toBe("switch");
|
||||
await w.get("[role=switch]").trigger("click");
|
||||
expect(w.emitted("update:checked")).toEqual([[true]]);
|
||||
await w.setProps({ checked: true });
|
||||
expect(w.get("[role=switch]").attributes("aria-checked")).toBe("true");
|
||||
});
|
||||
it("supports uncontrolled defaultChecked", async () => {
|
||||
const w = mount(Switch, { props: { defaultChecked: true } });
|
||||
expect(w.get("[role=switch]").attributes("aria-checked")).toBe("true");
|
||||
await w.get("[role=switch]").trigger("click");
|
||||
expect(w.get("[role=switch]").attributes("aria-checked")).toBe("false");
|
||||
});
|
||||
it("prevents toggling when disabled", async () => {
|
||||
const w = mount(Switch, { props: { disabled: true, checked: false } });
|
||||
await w.get("[role=switch]").trigger("click");
|
||||
expect(w.emitted("update:checked")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("upstream component contracts", () => {
|
||||
it("keeps pending buttons focusable and suppresses repeated activation", async () => {
|
||||
let clicks = 0;
|
||||
const w = mount(Button, {
|
||||
props: { isPending: true },
|
||||
attrs: { onClick: () => clicks++ },
|
||||
});
|
||||
expect(w.attributes("disabled")).toBeUndefined();
|
||||
expect(w.attributes("data-pending")).toBe("true");
|
||||
await w.trigger("click");
|
||||
expect(clicks).toBe(0);
|
||||
await w.setProps({ isPending: false });
|
||||
await w.trigger("click");
|
||||
expect(clicks).toBe(1);
|
||||
});
|
||||
it("applies icon-only and soft danger variants without leaking props to the DOM", () => {
|
||||
const w = mount(Button, {
|
||||
props: { variant: "danger-soft", isIconOnly: true, isDisabled: true },
|
||||
attrs: { "aria-label": "删除" },
|
||||
});
|
||||
expect(w.classes()).toContain("hv-button--danger-soft");
|
||||
expect(w.classes()).toContain("hv-button--icon-only");
|
||||
expect(w.attributes("disabled")).toBeDefined();
|
||||
expect(w.attributes("isicononly")).toBeUndefined();
|
||||
});
|
||||
it("maps Input secondary and invalid states to the documented CSS hooks", () => {
|
||||
const w = mount(Input, {
|
||||
props: { variant: "secondary", fullWidth: true, invalid: true },
|
||||
});
|
||||
expect(w.classes()).toContain("hv-input--secondary");
|
||||
expect(w.classes()).toContain("hv-input--full-width");
|
||||
expect(w.attributes("data-invalid")).toBe("true");
|
||||
expect(w.attributes("aria-invalid")).toBe("true");
|
||||
});
|
||||
it("preserves readonly switch values and gives isSelected precedence", async () => {
|
||||
const w = mount(Switch, {
|
||||
props: { isSelected: true, checked: false, isReadOnly: true },
|
||||
});
|
||||
const control = w.get("[role=switch]");
|
||||
expect(control.attributes("aria-checked")).toBe("true");
|
||||
expect(control.attributes("aria-readonly")).toBe("true");
|
||||
await control.trigger("click");
|
||||
expect(w.emitted("change")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user