import { describe, it, expect, beforeEach } from "vitest"; import { PettyPagination } from "../src/components/pagination/index"; import { h } from "./helpers"; describe("petty-pagination", () => { let el: PettyPagination; beforeEach(() => { document.body.textContent = ""; el = document.createElement("petty-pagination") as PettyPagination; el.setAttribute("total", "50"); el.setAttribute("page-size", "10"); el.appendChild(h("petty-pagination-item", { type: "prev" }, "Prev")); el.appendChild(h("petty-pagination-item", { value: "1" }, "1")); el.appendChild(h("petty-pagination-item", { value: "2" }, "2")); el.appendChild(h("petty-pagination-item", { value: "3" }, "3")); el.appendChild(h("petty-pagination-item", { type: "next" }, "Next")); document.body.appendChild(el); }); it("registers and calculates totalPages", () => { expect(customElements.get("petty-pagination")).toBe(PettyPagination); expect(el.totalPages).toBe(5); expect(el.currentPage).toBe(1); }); it("marks page 1 as active with aria-current", () => { const item1 = el.querySelector("[value='1']")!; expect(item1.getAttribute("data-state")).toBe("active"); expect(item1.getAttribute("aria-current")).toBe("page"); }); it("disables prev on first page", () => { expect(el.querySelector("[type=prev]")!.hasAttribute("disabled")).toBe(true); }); it("clamps goToPage to valid range", () => { el.goToPage(0); expect(el.currentPage).toBe(1); el.goToPage(999); expect(el.currentPage).toBe(5); }); it("fires petty-change on goToPage", () => { let detail: { page: number } | null = null; el.addEventListener("petty-change", ((e: CustomEvent) => { detail = e.detail; }) as EventListener); el.goToPage(3); expect(detail).toEqual({ page: 3 }); expect(el.currentPage).toBe(3); }); it("disables next on last page", () => { el.goToPage(5); expect(el.querySelector("[type=next]")!.hasAttribute("disabled")).toBe(true); }); });