resultsSorting.test.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* @vitest-environment node */
  2. import { describe, it, expect } from "vitest";
  3. import {
  4. SEARCH_RESULTS_SORT,
  5. toSearchItemIsoDateKey,
  6. formatSearchItemDateDe,
  7. sortSearchItems,
  8. } from "./resultsSorting.js";
  9. describe("lib/frontend/search/resultsSorting", () => {
  10. it("builds ISO date keys (YYYY-MM-DD) with zero padding", () => {
  11. expect(toSearchItemIsoDateKey({ year: "2025", month: "1", day: "2" })).toBe(
  12. "2025-01-02"
  13. );
  14. });
  15. it("formats German date strings (DD.MM.YYYY) with zero padding", () => {
  16. expect(formatSearchItemDateDe({ year: "2025", month: "1", day: "2" })).toBe(
  17. "02.01.2025"
  18. );
  19. });
  20. it("RELEVANCE keeps backend order", () => {
  21. const items = [{ filename: "b.pdf" }, { filename: "a.pdf" }];
  22. const out = sortSearchItems(items, SEARCH_RESULTS_SORT.RELEVANCE);
  23. expect(out.map((x) => x.filename)).toEqual(["b.pdf", "a.pdf"]);
  24. });
  25. it("DATE_DESC sorts newest date first, then filename asc", () => {
  26. const items = [
  27. { year: "2025", month: "12", day: "18", filename: "b.pdf" },
  28. { year: "2025", month: "12", day: "18", filename: "a.pdf" },
  29. { year: "2025", month: "01", day: "02", filename: "z.pdf" },
  30. ];
  31. const out = sortSearchItems(items, SEARCH_RESULTS_SORT.DATE_DESC);
  32. expect(
  33. out.map((x) => `${toSearchItemIsoDateKey(x)}|${x.filename}`)
  34. ).toEqual(["2025-12-18|a.pdf", "2025-12-18|b.pdf", "2025-01-02|z.pdf"]);
  35. });
  36. it("FILENAME_ASC sorts by filename", () => {
  37. const items = [
  38. { filename: "b.pdf" },
  39. { filename: "a.pdf" },
  40. { filename: "c.pdf" },
  41. ];
  42. const out = sortSearchItems(items, SEARCH_RESULTS_SORT.FILENAME_ASC);
  43. expect(out.map((x) => x.filename)).toEqual(["a.pdf", "b.pdf", "c.pdf"]);
  44. });
  45. });