|
| 1 | +import { parseDate } from "@internationalized/date" |
| 2 | +import { describe, expect, test } from "vitest" |
| 3 | +import { adjustStartAndEndDate, isDateWithinRange, sortDates } from "../src/date-picker.utils" |
| 4 | + |
| 5 | +describe("DatePicker Utils", () => { |
| 6 | + describe("sortDates", () => { |
| 7 | + test("should sort dates in ascending order", () => { |
| 8 | + const date1 = parseDate("2024-01-15") |
| 9 | + const date2 = parseDate("2024-01-20") |
| 10 | + const date3 = parseDate("2024-01-10") |
| 11 | + const values = [date1, date2, date3] |
| 12 | + |
| 13 | + const sorted = sortDates(values) |
| 14 | + expect(sorted[0]).toEqual(date3) |
| 15 | + expect(sorted[1]).toEqual(date1) |
| 16 | + expect(sorted[2]).toEqual(date2) |
| 17 | + }) |
| 18 | + |
| 19 | + test("should filter out null values and sort remaining dates", () => { |
| 20 | + const date1 = parseDate("2024-01-15") |
| 21 | + const date2 = parseDate("2024-01-20") |
| 22 | + const values = [date1, null, date2, null] |
| 23 | + |
| 24 | + const sorted = sortDates(values) |
| 25 | + expect(sorted).toHaveLength(2) |
| 26 | + expect(sorted[0]).toEqual(date1) |
| 27 | + expect(sorted[1]).toEqual(date2) |
| 28 | + }) |
| 29 | + |
| 30 | + test("should return empty array when all values are null", () => { |
| 31 | + const values = [null, null, null] |
| 32 | + const sorted = sortDates(values) |
| 33 | + expect(sorted).toEqual([]) |
| 34 | + }) |
| 35 | + |
| 36 | + test("should filter out undefined values and sort remaining dates", () => { |
| 37 | + const date1 = parseDate("2024-01-15") |
| 38 | + const date2 = parseDate("2024-01-20") |
| 39 | + const values = [date1, undefined, date2, undefined] |
| 40 | + |
| 41 | + const sorted = sortDates(values) |
| 42 | + expect(sorted).toHaveLength(2) |
| 43 | + expect(sorted[0]).toEqual(date1) |
| 44 | + expect(sorted[1]).toEqual(date2) |
| 45 | + }) |
| 46 | + |
| 47 | + test("should not mutate the original array", () => { |
| 48 | + const date1 = parseDate("2024-01-15") |
| 49 | + const date2 = parseDate("2024-01-20") |
| 50 | + const values = [date2, date1] |
| 51 | + const original = [...values] |
| 52 | + |
| 53 | + sortDates(values) |
| 54 | + expect(values).toEqual(original) |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + describe("adjustStartAndEndDate", () => { |
| 59 | + test("should return value as-is when dates are already in order", () => { |
| 60 | + const startDate = parseDate("2024-01-10") |
| 61 | + const endDate = parseDate("2024-01-20") |
| 62 | + const value = [startDate, endDate] |
| 63 | + |
| 64 | + const result = adjustStartAndEndDate(value) |
| 65 | + expect(result).toEqual([startDate, endDate]) |
| 66 | + }) |
| 67 | + |
| 68 | + test("should swap dates when end date is before start date", () => { |
| 69 | + const startDate = parseDate("2024-01-20") |
| 70 | + const endDate = parseDate("2024-01-10") |
| 71 | + const value = [startDate, endDate] |
| 72 | + |
| 73 | + const result = adjustStartAndEndDate(value) |
| 74 | + expect(result).toEqual([endDate, startDate]) |
| 75 | + }) |
| 76 | + |
| 77 | + test("should return value as-is when start date is null", () => { |
| 78 | + const endDate = parseDate("2024-01-20") |
| 79 | + const value = [null, endDate] |
| 80 | + |
| 81 | + const result = adjustStartAndEndDate(value) |
| 82 | + expect(result).toEqual([null, endDate]) |
| 83 | + }) |
| 84 | + |
| 85 | + test("should return value as-is when end date is null", () => { |
| 86 | + const startDate = parseDate("2024-01-10") |
| 87 | + const value = [startDate, null] |
| 88 | + |
| 89 | + const result = adjustStartAndEndDate(value) |
| 90 | + expect(result).toEqual([startDate, null]) |
| 91 | + }) |
| 92 | + |
| 93 | + test("should return value as-is when both dates are null", () => { |
| 94 | + const value = [null, null] |
| 95 | + const result = adjustStartAndEndDate(value) |
| 96 | + expect(result).toEqual([null, null]) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + describe("isDateWithinRange", () => { |
| 101 | + test("should return true when date is within range", () => { |
| 102 | + const startDate = parseDate("2024-01-10") |
| 103 | + const endDate = parseDate("2024-01-20") |
| 104 | + const date = parseDate("2024-01-15") |
| 105 | + const value = [startDate, endDate] |
| 106 | + |
| 107 | + expect(isDateWithinRange(date, value)).toBe(true) |
| 108 | + }) |
| 109 | + |
| 110 | + test("should return false when date is before start date", () => { |
| 111 | + const startDate = parseDate("2024-01-10") |
| 112 | + const endDate = parseDate("2024-01-20") |
| 113 | + const date = parseDate("2024-01-05") |
| 114 | + const value = [startDate, endDate] |
| 115 | + |
| 116 | + expect(isDateWithinRange(date, value)).toBe(false) |
| 117 | + }) |
| 118 | + |
| 119 | + test("should return false when date is after end date", () => { |
| 120 | + const startDate = parseDate("2024-01-10") |
| 121 | + const endDate = parseDate("2024-01-20") |
| 122 | + const date = parseDate("2024-01-25") |
| 123 | + const value = [startDate, endDate] |
| 124 | + |
| 125 | + expect(isDateWithinRange(date, value)).toBe(false) |
| 126 | + }) |
| 127 | + |
| 128 | + test("should return false when start date is null", () => { |
| 129 | + const endDate = parseDate("2024-01-20") |
| 130 | + const date = parseDate("2024-01-15") |
| 131 | + const value = [null, endDate] |
| 132 | + |
| 133 | + expect(isDateWithinRange(date, value)).toBe(false) |
| 134 | + }) |
| 135 | + |
| 136 | + test("should return false when end date is null", () => { |
| 137 | + const startDate = parseDate("2024-01-10") |
| 138 | + const date = parseDate("2024-01-15") |
| 139 | + const value = [startDate, null] |
| 140 | + |
| 141 | + expect(isDateWithinRange(date, value)).toBe(false) |
| 142 | + }) |
| 143 | + |
| 144 | + test("should return false when both dates are null", () => { |
| 145 | + const date = parseDate("2024-01-15") |
| 146 | + const value = [null, null] |
| 147 | + |
| 148 | + expect(isDateWithinRange(date, value)).toBe(false) |
| 149 | + }) |
| 150 | + }) |
| 151 | +}) |
0 commit comments