dateRange.test.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* @vitest-environment node */
  2. import { describe, it, expect } from "vitest";
  3. import {
  4. isValidIsoDateYmd,
  5. normalizeIsoDateYmdOrNull,
  6. compareIsoDatesYmd,
  7. isInvalidIsoDateRange,
  8. formatIsoDateDe,
  9. formatIsoDateRangeLabelDe,
  10. } from "./dateRange.js";
  11. describe("lib/frontend/search/dateRange", () => {
  12. describe("isValidIsoDateYmd", () => {
  13. it("accepts strict YYYY-MM-DD", () => {
  14. expect(isValidIsoDateYmd("2025-12-01")).toBe(true);
  15. expect(isValidIsoDateYmd("2025-01-31")).toBe(true);
  16. });
  17. it("rejects invalid formats and obvious invalid ranges", () => {
  18. expect(isValidIsoDateYmd("2025/12/01")).toBe(false);
  19. expect(isValidIsoDateYmd("2025-1-01")).toBe(false);
  20. expect(isValidIsoDateYmd("2025-01-1")).toBe(false);
  21. expect(isValidIsoDateYmd("2025-13-01")).toBe(false);
  22. expect(isValidIsoDateYmd("2025-00-01")).toBe(false);
  23. expect(isValidIsoDateYmd("2025-12-00")).toBe(false);
  24. expect(isValidIsoDateYmd("2025-12-32")).toBe(false);
  25. });
  26. });
  27. describe("normalizeIsoDateYmdOrNull", () => {
  28. it("trims and returns null for invalid values", () => {
  29. expect(normalizeIsoDateYmdOrNull(" 2025-12-01 ")).toBe("2025-12-01");
  30. expect(normalizeIsoDateYmdOrNull("")).toBe(null);
  31. expect(normalizeIsoDateYmdOrNull("2025/12/01")).toBe(null);
  32. expect(normalizeIsoDateYmdOrNull(null)).toBe(null);
  33. });
  34. });
  35. describe("compareIsoDatesYmd", () => {
  36. it("compares lexicographically", () => {
  37. expect(compareIsoDatesYmd("2025-01-01", "2025-01-01")).toBe(0);
  38. expect(compareIsoDatesYmd("2025-01-01", "2025-01-02")).toBe(-1);
  39. expect(compareIsoDatesYmd("2025-12-31", "2025-01-01")).toBe(1);
  40. });
  41. });
  42. describe("isInvalidIsoDateRange", () => {
  43. it("is false for open ranges and equal dates", () => {
  44. expect(isInvalidIsoDateRange("2025-12-01", null)).toBe(false);
  45. expect(isInvalidIsoDateRange(null, "2025-12-31")).toBe(false);
  46. expect(isInvalidIsoDateRange("2025-12-01", "2025-12-01")).toBe(false);
  47. });
  48. it("is true when from > to", () => {
  49. expect(isInvalidIsoDateRange("2025-12-31", "2025-12-01")).toBe(true);
  50. });
  51. });
  52. describe("formatIsoDateDe / formatIsoDateRangeLabelDe", () => {
  53. it("formats single dates as DD.MM.YYYY", () => {
  54. expect(formatIsoDateDe("2025-12-01")).toBe("01.12.2025");
  55. expect(formatIsoDateDe("invalid")).toBe(null);
  56. });
  57. it("formats date range labels", () => {
  58. expect(
  59. formatIsoDateRangeLabelDe({ from: "2025-12-01", to: "2025-12-31" })
  60. ).toBe("01.12.2025 – 31.12.2025");
  61. // Same day => single day label (and this must be treated as a valid one-day search)
  62. expect(
  63. formatIsoDateRangeLabelDe({ from: "2025-12-01", to: "2025-12-01" })
  64. ).toBe("01.12.2025");
  65. expect(formatIsoDateRangeLabelDe({ from: "2025-12-01", to: null })).toBe(
  66. "ab 01.12.2025"
  67. );
  68. expect(formatIsoDateRangeLabelDe({ from: null, to: "2025-12-31" })).toBe(
  69. "bis 31.12.2025"
  70. );
  71. expect(formatIsoDateRangeLabelDe({ from: null, to: null })).toBe(null);
  72. });
  73. });
  74. });