forked from baron/baron-sso
common 정렬 헬퍼 공통화 및 devfront 목록 정렬 추가
This commit is contained in:
72
adminfront/src/lib/sort.test.ts
Normal file
72
adminfront/src/lib/sort.test.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
compareNullableValues,
|
||||
sortItems,
|
||||
toggleSort,
|
||||
type SortConfig,
|
||||
} from "../../../common/core/utils";
|
||||
|
||||
describe("shared sort helpers", () => {
|
||||
it("toggles sort direction for the same key", () => {
|
||||
expect(toggleSort<string>(null, "name")).toEqual({
|
||||
key: "name",
|
||||
direction: "asc",
|
||||
});
|
||||
|
||||
expect(
|
||||
toggleSort<string>({ key: "name", direction: "asc" }, "name"),
|
||||
).toEqual({
|
||||
key: "name",
|
||||
direction: "desc",
|
||||
});
|
||||
|
||||
expect(
|
||||
toggleSort<string>({ key: "name", direction: "desc" }, "status"),
|
||||
).toEqual({
|
||||
key: "status",
|
||||
direction: "asc",
|
||||
});
|
||||
});
|
||||
|
||||
it("compares nullable values with nulls last", () => {
|
||||
expect(compareNullableValues("a", "b", "asc")).toBeLessThan(0);
|
||||
expect(compareNullableValues("a", "b", "desc")).toBeGreaterThan(0);
|
||||
expect(compareNullableValues(null, "b", "asc")).toBeGreaterThan(0);
|
||||
expect(compareNullableValues("b", null, "asc")).toBeLessThan(0);
|
||||
});
|
||||
|
||||
it("sorts items with resolver maps", () => {
|
||||
const items = [
|
||||
{
|
||||
id: "2",
|
||||
name: "Beta",
|
||||
metadata: { score: 2 },
|
||||
},
|
||||
{
|
||||
id: "3",
|
||||
name: "gamma",
|
||||
metadata: {},
|
||||
},
|
||||
{
|
||||
id: "1",
|
||||
name: "alpha",
|
||||
metadata: { score: 1 },
|
||||
},
|
||||
];
|
||||
|
||||
const nameSort: SortConfig<"name"> = { key: "name", direction: "asc" };
|
||||
expect(sortItems(items, nameSort).map((item) => item.id)).toEqual([
|
||||
"1",
|
||||
"2",
|
||||
"3",
|
||||
]);
|
||||
|
||||
const scoreSort: SortConfig<"score"> = { key: "score", direction: "asc" };
|
||||
expect(
|
||||
sortItems(items, scoreSort, {
|
||||
score: (item) =>
|
||||
typeof item.metadata.score === "number" ? item.metadata.score : null,
|
||||
}).map((item) => item.id),
|
||||
).toEqual(["1", "2", "3"]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user