Files
aniplay-api/src/libs/sortByProperty.spec.ts
Rushil Perera 1140ffa8b8 refactor!: migrate away from bun
- migrate package management to pnpm
- migrate test suite to vitest
- also remove Anify integration
2025-12-16 07:50:38 -05:00

56 lines
1.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { sortByProperty } from "./sortByProperty";
describe("sortByProperty", () => {
it("ascending order, array stays same", () => {
const sorted = [{ key: "a" }, { key: "b" }, { key: "c" }].sort(
sortByProperty({ a: 1, b: 2, c: 3 }, "key"),
);
expect(sorted).toEqual([{ key: "a" }, { key: "b" }, { key: "c" }]);
});
it("descending order, array is reversed", () => {
const sorted = [{ key: "a" }, { key: "b" }, { key: "c" }].sort(
sortByProperty({ a: 3, b: 2, c: 1 }, "key"),
);
expect(sorted).toEqual([{ key: "c" }, { key: "b" }, { key: "a" }]);
});
it("randomized order, array is randomized", () => {
const sorted = [
{ key: "a" },
{ key: "b" },
{ key: "c" },
{ key: "d" },
{ key: "e" },
].sort(sortByProperty({ a: 3, b: 4, c: 1, d: 2, e: 5 }, "key"));
expect(sorted).toEqual([
{ key: "c" },
{ key: "d" },
{ key: "a" },
{ key: "b" },
{ key: "e" },
]);
});
it("object without expected property gets put to end of array", () => {
const sorted = [
{ key: "a" },
{ different: "key" },
{ key: "b" },
{ key: "c" },
].sort(sortByProperty({ a: 3, b: 2, c: 1, key: 0 }, "key"));
expect(sorted).toEqual([
{ key: "c" },
{ key: "b" },
{ key: "a" },
{ different: "key" },
]);
});
});