import { describe, expect, it, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { TablePager } from "./table-pager";
import { DataTable } from "./data-table";
// fit-admin-listing-pages: one pager for every paged listing, pinned to the bottom of the screen.
describe("TablePager", () => {
it("shows the range and page count, and pages back and forward", async () => {
const onPageChange = vi.fn();
const user = userEvent.setup();
render();
expect(screen.getByText("11–20 of 35")).toBeInTheDocument();
expect(screen.getByText(/Page/)).toHaveTextContent("Page 2 of 4");
await user.click(screen.getByRole("button", { name: /previous/i }));
await user.click(screen.getByRole("button", { name: /next/i }));
expect(onPageChange.mock.calls).toEqual([[1], [3]]);
});
it("disables Previous on the first page and Next on the last", () => {
render( {}} />);
expect(screen.getByRole("button", { name: /previous/i })).toBeDisabled();
expect(screen.getByRole("button", { name: /next/i })).toBeDisabled();
expect(screen.getByText("1–8 of 8")).toBeInTheDocument();
});
it("reads 0 of 0 when there are no rows", () => {
render( {}} />);
expect(screen.getByText("0 of 0")).toBeInTheDocument();
});
it("sticks to the bottom of the screen with an opaque background", () => {
render( {}} />);
const pager = document.querySelector('[data-slot="table-pager"]') as HTMLElement;
expect(pager).toHaveClass("sticky", "bottom-0", "bg-background", "border-t");
});
});
describe("DataTable uses TablePager", () => {
it("renders the shared pager and hands its root to tableRef", () => {
const tableRef = vi.fn();
render(
label="t" columns={[{ accessorKey: "id", header: "Id" }]} data={[{ id: "a" }]}
total={1} page={1} pageSize={20} onPageChange={() => {}} rowKey={r => r.id} tableRef={tableRef} />,
);
expect(document.querySelector('[data-slot="table-pager"]')).not.toBeNull();
expect(tableRef).toHaveBeenCalledWith(expect.any(HTMLDivElement));
});
});