145 lines
4.7 KiB
TypeScript
145 lines
4.7 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { mount } from "@vue/test-utils";
|
|
import { defineComponent, nextTick, ref } from "vue";
|
|
import { Header, HeaderRoot } from "../packages/vue/src";
|
|
|
|
import HeaderBasicDemo from "../apps/docs/demos/header-basic.vue";
|
|
import HeaderCustomClassDemo from "../apps/docs/demos/header-custom-class.vue";
|
|
import HeaderSemanticDemo from "../apps/docs/demos/header-semantic.vue";
|
|
|
|
afterEach(() => {
|
|
document.body.innerHTML = "";
|
|
});
|
|
|
|
describe("Header basics", () => {
|
|
it("renders a native header with baseline class and data-slot", () => {
|
|
const w = mount(Header, {
|
|
props: {},
|
|
slots: { default: "操作" },
|
|
attachTo: document.body,
|
|
});
|
|
const root = w.get('[data-slot="header"]');
|
|
expect(root.element.tagName).toBe("HEADER");
|
|
expect(root.classes().join(" ")).toContain("hv-header");
|
|
expect(root.text()).toBe("操作");
|
|
w.unmount();
|
|
});
|
|
|
|
it("exports Header as HeaderRoot alias", () => {
|
|
expect(Header).toBe(HeaderRoot);
|
|
});
|
|
|
|
it("supports as override without inventing heading levels", () => {
|
|
const w = mount(Header, {
|
|
props: { as: "div" },
|
|
slots: { default: "分组" },
|
|
attachTo: document.body,
|
|
});
|
|
const root = w.get('[data-slot="header"]');
|
|
expect(root.element.tagName).toBe("DIV");
|
|
expect(root.attributes("role")).toBeUndefined();
|
|
expect(root.attributes("aria-level")).toBeUndefined();
|
|
w.unmount();
|
|
});
|
|
|
|
it("merges class and attrs, including dynamic add/update/delete/re-add", async () => {
|
|
const extra = ref<Record<string, unknown>>({});
|
|
const w = mount(
|
|
defineComponent({
|
|
components: { Header },
|
|
setup: () => ({ extra }),
|
|
template: `<Header v-bind="extra" class="base-header">分组</Header>`,
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
let root = w.get('[data-slot="header"]');
|
|
expect(root.classes()).toContain("hv-header");
|
|
expect(root.classes()).toContain("base-header");
|
|
|
|
extra.value = {
|
|
"data-testid": "h1",
|
|
"aria-label": "分组标题",
|
|
id: "group-title",
|
|
};
|
|
await nextTick();
|
|
root = w.get('[data-slot="header"]');
|
|
expect(root.attributes("data-testid")).toBe("h1");
|
|
expect(root.attributes("aria-label")).toBe("分组标题");
|
|
expect(root.attributes("id")).toBe("group-title");
|
|
|
|
extra.value = { "data-testid": "h2", title: "updated" };
|
|
await nextTick();
|
|
root = w.get('[data-slot="header"]');
|
|
expect(root.attributes("data-testid")).toBe("h2");
|
|
expect(root.attributes("title")).toBe("updated");
|
|
expect(root.attributes("aria-label")).toBeUndefined();
|
|
expect(root.attributes("id")).toBeUndefined();
|
|
|
|
extra.value = {};
|
|
await nextTick();
|
|
root = w.get("[data-slot=header]");
|
|
expect(root.attributes("data-testid")).toBeUndefined();
|
|
expect(root.attributes("title")).toBeUndefined();
|
|
|
|
extra.value = { style: { color: "red" } };
|
|
await nextTick();
|
|
root = w.get("[data-slot=header]");
|
|
expect(root.attributes("style")).toContain("color: red");
|
|
w.unmount();
|
|
});
|
|
|
|
it("updates default slot content and keeps multiple instances independent", async () => {
|
|
const label = ref("A 组");
|
|
const w = mount(
|
|
defineComponent({
|
|
components: { Header },
|
|
setup: () => ({ label }),
|
|
template: `
|
|
<div>
|
|
<Header data-testid="one">{{ label }}</Header>
|
|
<Header data-testid="two">B 组</Header>
|
|
</div>
|
|
`,
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
expect(w.get('[data-testid="one"]').text()).toBe("A 组");
|
|
expect(w.get('[data-testid="two"]').text()).toBe("B 组");
|
|
|
|
label.value = "A 组已更新";
|
|
await nextTick();
|
|
expect(w.get('[data-testid="one"]').text()).toBe("A 组已更新");
|
|
expect(w.get('[data-testid="two"]').text()).toBe("B 组");
|
|
w.unmount();
|
|
});
|
|
});
|
|
|
|
describe("Header demos", () => {
|
|
it("mounts all self-authored demos without Vue warnings", async () => {
|
|
const warnings: string[] = [];
|
|
const spy = vi.spyOn(console, "warn").mockImplementation((...args) => {
|
|
warnings.push(args.map(String).join(" "));
|
|
});
|
|
for (const demo of [
|
|
HeaderBasicDemo,
|
|
HeaderCustomClassDemo,
|
|
HeaderSemanticDemo,
|
|
]) {
|
|
const w = mount(demo, { attachTo: document.body });
|
|
await nextTick();
|
|
expect(w.html().length).toBeGreaterThan(0);
|
|
expect(w.find('[data-slot="header"]').exists()).toBe(true);
|
|
w.unmount();
|
|
}
|
|
spy.mockRestore();
|
|
expect(warnings).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("Header types", () => {
|
|
it("exposes HeaderProps alias", () => {
|
|
const ok: InstanceType<typeof HeaderRoot>["$props"] = { as: "header" };
|
|
expect(ok.as).toBe("header");
|
|
});
|
|
});
|