import { describe, it, expect, vi } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import type { SortingState } from "@tanstack/react-table";
import { Table, TableHeader, TableRow } from "./table";
import { SortableHead } from "./sortable-head";
function renderHead(sorting: SortingState, onSortingChange = vi.fn()) {
render(
,
);
return onSortingChange;
}
describe("SortableHead", () => {
it.each([
[[], "none"],
[[{ id: "amount", desc: false }], "ascending"],
[[{ id: "amount", desc: true }], "descending"],
[[{ id: "other", desc: false }], "none"],
] as [SortingState, string][])("aria-sort for %j is %s", (sorting, expected) => {
renderHead(sorting);
expect(screen.getByRole("columnheader")).toHaveAttribute("aria-sort", expected);
});
it("cycles none -> asc", () => {
const fn = renderHead([]);
fireEvent.click(screen.getByRole("button", { name: /Amount/ }));
expect(fn).toHaveBeenCalledWith([{ id: "amount", desc: false }]);
});
it("cycles asc -> desc", () => {
const fn = renderHead([{ id: "amount", desc: false }]);
fireEvent.click(screen.getByRole("button", { name: /Amount/ }));
expect(fn).toHaveBeenCalledWith([{ id: "amount", desc: true }]);
});
it("cycles desc -> none", () => {
const fn = renderHead([{ id: "amount", desc: true }]);
fireEvent.click(screen.getByRole("button", { name: /Amount/ }));
expect(fn).toHaveBeenCalledWith([]);
});
it("sorting by another column starts at asc", () => {
const fn = renderHead([{ id: "other", desc: true }]);
fireEvent.click(screen.getByRole("button", { name: /Amount/ }));
expect(fn).toHaveBeenCalledWith([{ id: "amount", desc: false }]);
});
});