dateRangePickerUtils.test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* @vitest-environment node */
  2. import { describe, it, expect } from "vitest";
  3. import {
  4. normalizeDayClickArgs,
  5. buildCalendarState,
  6. } from "./dateRangePickerUtils.js";
  7. describe("lib/frontend/search/dateRangePickerUtils", () => {
  8. describe("normalizeDayClickArgs", () => {
  9. it("supports (day, modifiers) signature", () => {
  10. const day = new Date(2026, 0, 10);
  11. const modifiers = { disabled: false };
  12. const out = normalizeDayClickArgs([day, modifiers]);
  13. expect(out.day).toBe(day);
  14. expect(out.modifiers).toBe(modifiers);
  15. });
  16. it("supports (event, day, modifiers) signature", () => {
  17. const day = new Date(2026, 0, 10);
  18. const modifiers = { disabled: true };
  19. const out = normalizeDayClickArgs([{}, day, modifiers]);
  20. expect(out.day).toBe(day);
  21. expect(out.modifiers).toBe(modifiers);
  22. });
  23. it("returns nulls for unknown inputs", () => {
  24. const out = normalizeDayClickArgs([{}, {}, {}]);
  25. expect(out).toEqual({ day: null, modifiers: null });
  26. });
  27. });
  28. describe("buildCalendarState", () => {
  29. it("builds a valid selected range", () => {
  30. const from = new Date(2026, 0, 1);
  31. const to = new Date(2026, 0, 5);
  32. const out = buildCalendarState({
  33. fromDate: from,
  34. toDate: to,
  35. isRangeInvalid: false,
  36. });
  37. expect(out.calendarSelected).toEqual({ from, to });
  38. expect(out.calendarModifiers).toBe(undefined);
  39. });
  40. it("builds an invalid interval and modifiers when range is invalid", () => {
  41. const from = new Date(2026, 0, 10);
  42. const to = new Date(2026, 0, 5);
  43. const out = buildCalendarState({
  44. fromDate: from,
  45. toDate: to,
  46. isRangeInvalid: true,
  47. });
  48. expect(out.calendarSelected.from <= out.calendarSelected.to).toBe(true);
  49. expect(out.calendarModifiers).toBeTruthy();
  50. expect(out.calendarModifiersClassNames).toBeTruthy();
  51. });
  52. it("supports open range (from only)", () => {
  53. const from = new Date(2026, 0, 2);
  54. const out = buildCalendarState({
  55. fromDate: from,
  56. toDate: null,
  57. isRangeInvalid: false,
  58. });
  59. expect(out.calendarSelected).toEqual({ from, to: undefined });
  60. });
  61. it("supports to-only (single-day range)", () => {
  62. const to = new Date(2026, 0, 2);
  63. const out = buildCalendarState({
  64. fromDate: null,
  65. toDate: to,
  66. isRangeInvalid: false,
  67. });
  68. expect(out.calendarSelected).toEqual({ from: to, to });
  69. });
  70. });
  71. });