content-explorer/ui/lib/sort-rows.test.ts

24 lines
1 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from "vitest";
import { nextSort, sortRows } from "./sort-rows";
const rows = [
{ title: "Emergence" },
{ title: "Feedback" },
{ title: "Requisite variety" },
];
describe("sortRows", () => {
it("orders by the selected key and flips with the direction", () => {
const ascending = sortRows(rows, { key: "title", dir: 1 }, (row, key) => row[key as "title"]);
expect(ascending.map((row) => row.title)).toEqual(["Emergence", "Feedback", "Requisite variety"]);
const descending = sortRows(rows, { key: "title", dir: -1 }, (row, key) => row[key as "title"]);
expect(descending.map((row) => row.title)).toEqual(["Requisite variety", "Feedback", "Emergence"]);
});
it("toggles direction when the same key is chosen again", () => {
expect(nextSort(undefined, "title")).toEqual({ key: "title", dir: 1 });
expect(nextSort({ key: "title", dir: 1 }, "title")).toEqual({ key: "title", dir: -1 });
expect(nextSort({ key: "title", dir: -1 }, "kindLabel")).toEqual({ key: "kindLabel", dir: 1 });
});
});