Files
heroui-vue/tests/pagination.test.ts

585 lines
20 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { afterEach, describe, expect, it, vi } from "vitest";
import { mount } from "@vue/test-utils";
import { defineComponent, h, nextTick, ref } from "vue";
import {
Pagination,
PaginationContent,
PaginationEllipsis,
PaginationItem,
PaginationLink,
PaginationNext,
PaginationNextIcon,
PaginationPrevious,
PaginationPreviousIcon,
PaginationRoot,
PaginationSummary,
} from "../packages/vue/src";
import PaginationBasicDemo from "../apps/docs/demos/pagination-basic.vue";
import PaginationSizesDemo from "../apps/docs/demos/pagination-sizes.vue";
import PaginationDisabledDemo from "../apps/docs/demos/pagination-disabled.vue";
import PaginationSimplePrevNextDemo from "../apps/docs/demos/pagination-simple-prev-next.vue";
import PaginationControlledDemo from "../apps/docs/demos/pagination-controlled.vue";
import PaginationWithEllipsisDemo from "../apps/docs/demos/pagination-with-ellipsis.vue";
import PaginationWithSummaryDemo from "../apps/docs/demos/pagination-with-summary.vue";
import PaginationCustomIconsDemo from "../apps/docs/demos/pagination-custom-icons.vue";
import PaginationCustomStylesDemo from "../apps/docs/demos/pagination-custom-styles.vue";
afterEach(() => {
document.body.innerHTML = "";
});
function mountFull(props: Record<string, unknown> = {}, rootProps = {}) {
return mount(
defineComponent({
render: () =>
h(Pagination, { ...rootProps }, () =>
h(PaginationContent, () => [
h(PaginationItem, () =>
h(PaginationPrevious, props.previous ?? {}, () => [
h(PaginationPreviousIcon),
h("span", "上一页"),
]),
),
h(PaginationItem, () =>
h(PaginationLink, props.link ?? {}, () => "1"),
),
h(PaginationItem, () => h(PaginationEllipsis)),
h(PaginationItem, () =>
h(PaginationNext, props.next ?? {}, () => [
h("span", "下一页"),
h(PaginationNextIcon),
]),
),
]),
),
}),
{ attachTo: document.body },
);
}
describe("Pagination structure and ARIA", () => {
it("renders nav/list/listitem with baseline slots and classes", () => {
const w = mountFull({}, { size: "md" });
const root = w.get('[data-slot="pagination"]');
expect(root.element.tagName).toBe("NAV");
expect(root.attributes("role")).toBe("navigation");
expect(root.attributes("aria-label")).toBe("pagination");
expect(root.classes().join(" ")).toContain("hv-pagination");
expect(root.classes().join(" ")).toContain("hv-pagination--md");
const content = w.get('[data-slot="pagination-content"]');
expect(content.element.tagName).toBe("UL");
expect(content.classes().join(" ")).toContain("hv-pagination__content");
const items = w.findAll('[data-slot="pagination-item"]');
expect(items).toHaveLength(4);
for (const item of items) {
expect(item.element.tagName).toBe("LI");
expect(item.classes().join(" ")).toContain("hv-pagination__item");
}
const link = w.get('[data-slot="pagination-link"]');
expect(link.element.tagName).toBe("BUTTON");
expect(link.attributes("type")).toBe("button");
expect(link.classes().join(" ")).toContain("hv-pagination__link");
expect(link.classes().join(" ")).not.toContain("hv-button");
const previous = w.get('[data-slot="pagination-previous"]');
expect(previous.element.tagName).toBe("BUTTON");
expect(previous.attributes("type")).toBe("button");
expect(previous.classes().join(" ")).toContain("hv-pagination__link");
expect(previous.classes().join(" ")).toContain("hv-pagination__link--nav");
expect(previous.classes().join(" ")).not.toContain("hv-button");
const next = w.get('[data-slot="pagination-next"]');
expect(next.classes().join(" ")).toContain("hv-pagination__link--nav");
const ellipsis = w.get('[data-slot="pagination-ellipsis"]');
expect(ellipsis.element.tagName).toBe("SPAN");
expect(ellipsis.attributes("aria-hidden")).toBe("true");
expect(ellipsis.text()).toBe("…");
const prevIcon = w.get('[data-slot="pagination-previous-icon"]');
expect(prevIcon.attributes("aria-hidden")).toBe("true");
expect(prevIcon.find("svg").exists()).toBe(true);
const nextIcon = w.get('[data-slot="pagination-next-icon"]');
expect(nextIcon.attributes("aria-hidden")).toBe("true");
expect(nextIcon.find("svg").exists()).toBe(true);
w.unmount();
});
it("exposes Pagination as Root alias and isolated instances", () => {
expect(Pagination).toBe(PaginationRoot);
const w = mount(
defineComponent({
render: () =>
h("div", () => [
h(Pagination, { "data-testid": "a" }, () =>
h(PaginationContent, () =>
h(PaginationItem, () =>
h(PaginationLink, { isActive: true }, () => "1"),
),
),
),
h(Pagination, { "data-testid": "b", size: "lg" }, () =>
h(PaginationContent, () =>
h(PaginationItem, () =>
h(PaginationLink, { isActive: true }, () => "1"),
),
),
),
]),
}),
{ attachTo: document.body },
);
const roots = w.findAll('[data-slot="pagination"]');
expect(roots).toHaveLength(2);
expect(roots[0].classes().join(" ")).toContain("hv-pagination--md");
expect(roots[1].classes().join(" ")).toContain("hv-pagination--lg");
expect(roots[0].attributes("aria-label")).toBe("pagination");
expect(roots[0].attributes("data-testid")).toBe("a");
expect(roots[1].attributes("data-testid")).toBe("b");
w.unmount();
});
it("updates size context dynamically and keeps custom classes", async () => {
const size = ref<"sm" | "md" | "lg">("sm");
const w = mount(
defineComponent({
setup: () => ({ size }),
render: () =>
h(Pagination, { size: size.value, class: "custom-root" }, () =>
h(PaginationContent, { class: "custom-content" }, () =>
h(PaginationItem, { class: "custom-item" }, () =>
h(PaginationLink, { class: "custom-link" }, () => "1"),
),
),
),
}),
{ attachTo: document.body },
);
let root = w.get('[data-slot="pagination"]');
expect(root.classes().join(" ")).toContain("hv-pagination--sm");
expect(root.classes().join(" ")).toContain("custom-root");
expect(w.get('[data-slot="pagination-content"]').classes()).toContain(
"custom-content",
);
expect(w.get('[data-slot="pagination-item"]').classes()).toContain(
"custom-item",
);
expect(w.get('[data-slot="pagination-link"]').classes()).toContain(
"custom-link",
);
size.value = "lg";
await nextTick();
root = w.get('[data-slot="pagination"]');
expect(root.classes().join(" ")).toContain("hv-pagination--lg");
expect(root.classes().join(" ")).not.toContain("hv-pagination--sm");
expect(root.classes().join(" ")).toContain("custom-root");
w.unmount();
});
it("keeps nested pagination size contexts independent", () => {
const w = mount(
defineComponent({
render: () =>
h(Pagination, { size: "sm", "data-testid": "outer" }, () =>
h(PaginationContent, () => [
h(PaginationItem, () => h(PaginationLink, () => "1")),
h(PaginationItem, () =>
h(Pagination, { size: "lg", "data-testid": "inner" }, () =>
h(PaginationContent, () =>
h(PaginationItem, () => h(PaginationLink, () => "2")),
),
),
),
]),
),
}),
{ attachTo: document.body },
);
const outer = w.get('[data-testid="outer"]');
const inner = w.get('[data-testid="inner"]');
expect(outer.classes().join(" ")).toContain("hv-pagination--sm");
expect(inner.classes().join(" ")).toContain("hv-pagination--lg");
w.unmount();
});
});
describe("Pagination active and summary states", () => {
it("toggles aria-current and data-active with isActive", async () => {
const isActive = ref(true);
const w = mount(
defineComponent({
setup: () => ({ isActive }),
render: () =>
h(Pagination, () =>
h(PaginationContent, () =>
h(PaginationItem, () =>
h(PaginationLink, { isActive: isActive.value }, () => "3"),
),
),
),
}),
{ attachTo: document.body },
);
let link = w.get('[data-slot="pagination-link"]');
expect(link.attributes("aria-current")).toBe("page");
expect(link.attributes("data-active")).toBe("true");
isActive.value = false;
await nextTick();
link = w.get('[data-slot="pagination-link"]');
expect(link.attributes("aria-current")).toBeUndefined();
expect(link.attributes("data-active")).toBeUndefined();
isActive.value = true;
await nextTick();
link = w.get('[data-slot="pagination-link"]');
expect(link.attributes("aria-current")).toBe("page");
expect(link.attributes("data-active")).toBe("true");
w.unmount();
});
it("summary defaults to dir=auto and allows explicit dir override", async () => {
const dir = ref<string | undefined>(undefined);
const w = mount(
defineComponent({
setup: () => ({ dir }),
render: () =>
h(Pagination, () =>
h(PaginationSummary, { dir: dir.value }, () => "1–5 of 10"),
),
}),
{ attachTo: document.body },
);
let summary = w.get('[data-slot="pagination-summary"]');
expect(summary.element.tagName).toBe("DIV");
expect(summary.attributes("dir")).toBe("auto");
expect(summary.classes().join(" ")).toContain("hv-pagination__summary");
dir.value = "rtl";
await nextTick();
summary = w.get('[data-slot="pagination-summary"]');
expect(summary.attributes("dir")).toBe("rtl");
dir.value = undefined;
await nextTick();
summary = w.get('[data-slot="pagination-summary"]');
expect(summary.attributes("dir")).toBe("auto");
w.unmount();
});
});
describe("Pagination button behavior", () => {
it("activates once on click and does not when disabled", async () => {
const clicks = vi.fn();
const w = mount(
defineComponent({
render: () =>
h(Pagination, () =>
h(PaginationContent, () =>
h(PaginationItem, () =>
h(
PaginationLink,
{ isDisabled: false, onClick: clicks },
() => "1",
),
),
),
),
}),
{ attachTo: document.body },
);
await w.get('[data-slot="pagination-link"]').trigger("click");
expect(clicks).toHaveBeenCalledTimes(1);
await w.unmount();
const disabledClicks = vi.fn();
const w2 = mount(
defineComponent({
render: () =>
h(Pagination, () =>
h(PaginationContent, () =>
h(PaginationItem, () =>
h(
PaginationLink,
{ isDisabled: true, onClick: disabledClicks },
() => "1",
),
),
),
),
}),
{ attachTo: document.body },
);
const disabledLink = w2.get('[data-slot="pagination-link"]');
expect(disabledLink.attributes("disabled")).toBeDefined();
expect((disabledLink.element as HTMLButtonElement).disabled).toBe(true);
await disabledLink.trigger("click");
expect(disabledClicks).not.toHaveBeenCalled();
w2.unmount();
});
it("supports handler arrays, capture and stopImmediatePropagation", async () => {
const order: string[] = [];
const w = mount(
defineComponent({
render: () =>
h(Pagination, () =>
h(PaginationContent, () =>
h(PaginationItem, () =>
h(
PaginationLink,
{
onClick: [
() => order.push("a"),
(e: Event) => {
order.push("b");
e.stopImmediatePropagation();
},
],
},
() => "1",
),
),
),
),
}),
{ attachTo: document.body },
);
await w.get('[data-slot="pagination-link"]').trigger("click");
expect(order).toEqual(["a", "b"]);
w.unmount();
});
it("keeps Previous/Next disabled state on native buttons", async () => {
const w = mountFull({
previous: { isDisabled: true },
next: { disabled: true },
});
const previous = w.get('[data-slot="pagination-previous"]');
const next = w.get('[data-slot="pagination-next"]');
expect((previous.element as HTMLButtonElement).disabled).toBe(true);
expect((next.element as HTMLButtonElement).disabled).toBe(true);
w.unmount();
});
});
describe("Pagination attrs merge", () => {
it("adds, updates, deletes and re-adds root and leaf attrs", async () => {
const extra = ref<Record<string, unknown>>({});
const w = mount(
defineComponent({
components: {
Pagination,
PaginationContent,
PaginationItem,
PaginationLink,
},
setup: () => ({ extra }),
template: `
<Pagination v-bind="extra" class="base-root">
<PaginationContent v-bind="extra">
<PaginationItem v-bind="extra">
<PaginationLink v-bind="extra">1</PaginationLink>
</PaginationItem>
</PaginationContent>
</Pagination>
`,
}),
{ attachTo: document.body },
);
let root = w.get('[data-slot="pagination"]');
expect(root.classes()).toContain("base-root");
expect(root.attributes("data-testid")).toBeUndefined();
extra.value = {
"data-testid": "p1",
"aria-label": "页码",
tabindex: 0,
onClick: () => {},
};
await nextTick();
root = w.get('[data-slot="pagination"]');
expect(root.attributes("data-testid")).toBe("p1");
expect(root.attributes("aria-label")).toBe("页码");
expect(root.attributes("tabindex")).toBe("0");
const link = w.get('[data-slot="pagination-link"]');
expect(link.attributes("data-testid")).toBe("p1");
expect(link.attributes("aria-label")).toBe("页码");
expect(link.attributes("tabindex")).toBe("0");
extra.value = { "data-testid": "p2", title: "updated" };
await nextTick();
root = w.get('[data-slot="pagination"]');
expect(root.attributes("data-testid")).toBe("p2");
expect(root.attributes("title")).toBe("updated");
expect(root.attributes("aria-label")).toBe("pagination");
expect(root.attributes("tabindex")).toBeUndefined();
extra.value = {};
await nextTick();
root = w.get('[data-slot="pagination"]');
expect(root.attributes("data-testid")).toBeUndefined();
expect(root.attributes("title")).toBeUndefined();
extra.value = { style: { color: "red" }, "aria-current": "true" };
await nextTick();
root = w.get("[data-slot=pagination]");
expect(root.attributes("style")).toContain("color: red");
w.unmount();
});
it("preserves asChild native child attributes", async () => {
const w = mount(
defineComponent({
render: () =>
h(Pagination, () =>
h(PaginationContent, () =>
h(PaginationItem, () =>
h(
PaginationLink,
{
asChild: true,
isActive: true,
"data-custom": "keep",
tabindex: 0,
},
{
default: () =>
h("a", { href: "#page-1", id: "native-link" }, "1"),
},
),
),
),
),
}),
{ attachTo: document.body },
);
const link = w.get("#native-link");
expect(link.element.tagName).toBe("A");
expect(link.attributes("href")).toBe("#page-1");
expect(link.attributes("data-custom")).toBe("keep");
expect(link.attributes("tabindex")).toBe("0");
expect(link.attributes("aria-current")).toBe("page");
expect(link.attributes("data-active")).toBe("true");
expect(link.attributes("data-slot")).toBe("pagination-link");
w.unmount();
});
});
describe("Pagination demos", () => {
it("controlled demo pages, disables at bounds and keeps summary in sync", async () => {
const w = mount(PaginationControlledDemo, { attachTo: document.body });
const previous = w.get('[data-slot="pagination-previous"]');
const next = w.get('[data-slot="pagination-next"]');
expect((previous.element as HTMLButtonElement).disabled).toBe(true);
expect((next.element as HTMLButtonElement).disabled).toBe(false);
expect(w.get('[data-slot="pagination-summary"]').text()).toContain(
"第 1–10 条",
);
expect(
w.get('[data-slot="pagination-link"][data-active="true"]').text(),
).toBe("1");
await next.trigger("click");
await nextTick();
expect(w.get('[data-slot="pagination-summary"]').text()).toContain(
"第 11–20 条",
);
expect(
w.get('[data-slot="pagination-link"][data-active="true"]').text(),
).toBe("2");
expect(
(w.get('[data-slot="pagination-previous"]').element as HTMLButtonElement)
.disabled,
).toBe(false);
for (let i = 0; i < 20; i++) {
const nextBtn = w.get('[data-slot="pagination-next"]');
if ((nextBtn.element as HTMLButtonElement).disabled) break;
await nextBtn.trigger("click");
}
await nextTick();
expect(
(w.get('[data-slot="pagination-next"]').element as HTMLButtonElement)
.disabled,
).toBe(true);
expect(w.get('[data-slot="pagination-summary"]').text()).toContain(
"第 111–120 条",
);
expect(
w.get('[data-slot="pagination-link"][data-active="true"]').text(),
).toBe("12");
expect(
w.findAll('[data-slot="pagination-ellipsis"]').length,
).toBeGreaterThan(0);
w.unmount();
});
it("basic demo updates the active page", async () => {
const w = mount(PaginationBasicDemo, { attachTo: document.body });
const links = w.findAll('[data-slot="pagination-link"]');
expect(links[0].attributes("data-active")).toBe("true");
await links[2].trigger("click");
await nextTick();
expect(links[2].attributes("data-active")).toBe("true");
expect(links[0].attributes("data-active")).toBeUndefined();
w.unmount();
});
it("mounts all 9 pagination demos without Vue warnings", async () => {
const warnings: string[] = [];
const spy = vi.spyOn(console, "warn").mockImplementation((...args) => {
warnings.push(args.map(String).join(" "));
});
const demos = [
PaginationBasicDemo,
PaginationSizesDemo,
PaginationDisabledDemo,
PaginationSimplePrevNextDemo,
PaginationControlledDemo,
PaginationWithEllipsisDemo,
PaginationWithSummaryDemo,
PaginationCustomIconsDemo,
PaginationCustomStylesDemo,
];
expect(demos).toHaveLength(9);
for (const demo of demos) {
const w = mount(demo, { attachTo: document.body });
await nextTick();
expect(w.html().length).toBeGreaterThan(0);
w.unmount();
}
spy.mockRestore();
expect(warnings).toEqual([]);
});
});
describe("Pagination types", () => {
it("accepts legal props and rejects illegal size values", () => {
const bad: InstanceType<typeof PaginationRoot>["$props"] = {
// @ts-expect-error size must be sm | md | lg
size: "xl",
};
expect((bad as { size?: string }).size).toBe("xl");
const ok: InstanceType<typeof PaginationRoot>["$props"] = {
size: "lg",
};
expect(ok.size).toBe("lg");
const okLink: InstanceType<typeof PaginationLink>["$props"] = {
isActive: true,
isDisabled: false,
};
expect(okLink.isActive).toBe(true);
});
});