|
| 1 | +import { getNextClosestDay } from "./generateScheduleSendDate"; |
| 2 | + |
| 3 | +describe("getNextClosestDay", () => { |
| 4 | + describe("Input validation", () => { |
| 5 | + it("should throw error when no working days are provided", () => { |
| 6 | + const date = new Date("2024-03-15T10:00:00"); // Friday |
| 7 | + expect(() => getNextClosestDay([], date)).toThrow( |
| 8 | + "No working days enabled in schedule" |
| 9 | + ); |
| 10 | + }); |
| 11 | + |
| 12 | + it("should throw error when only weekend days are provided", () => { |
| 13 | + const date = new Date("2024-03-15T10:00:00"); // Friday |
| 14 | + expect(() => getNextClosestDay(["saturday", "sunday"], date)).toThrow( |
| 15 | + "No working days enabled in schedule" |
| 16 | + ); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should handle case insensitive day names", () => { |
| 20 | + const date = new Date("2024-03-15T10:00:00"); // Friday |
| 21 | + const result = getNextClosestDay( |
| 22 | + ["MONDAY", "Tuesday", "wEdNeSdAy"], |
| 23 | + date |
| 24 | + ); |
| 25 | + expect(result).toBe(1); // Monday |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe("Weekend cases", () => { |
| 30 | + it("should return first available day when current day is Saturday", () => { |
| 31 | + const date = new Date("2024-03-16T10:00:00"); // Saturday |
| 32 | + const result = getNextClosestDay(["monday", "wednesday", "friday"], date); |
| 33 | + expect(result).toBe(1); // Monday |
| 34 | + }); |
| 35 | + |
| 36 | + it("should return first available day when current day is Sunday", () => { |
| 37 | + const date = new Date("2024-03-17T10:00:00"); // Sunday |
| 38 | + const result = getNextClosestDay(["tuesday", "thursday"], date); |
| 39 | + expect(result).toBe(2); // Tuesday |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe("Friday after hours cases", () => { |
| 44 | + it("should return first available day when Friday at exactly 19h", () => { |
| 45 | + const date = new Date("2024-03-15T19:00:00"); // Friday 19h |
| 46 | + const result = getNextClosestDay(["monday", "wednesday"], date); |
| 47 | + expect(result).toBe(1); // Monday |
| 48 | + }); |
| 49 | + |
| 50 | + it("should return first available day when Friday after 19h", () => { |
| 51 | + const date = new Date("2024-03-15T20:30:00"); // Friday 20h30 |
| 52 | + const result = getNextClosestDay(["tuesday", "thursday"], date); |
| 53 | + expect(result).toBe(2); // Tuesday |
| 54 | + }); |
| 55 | + |
| 56 | + it("should not trigger after hours logic when Friday before 19h", () => { |
| 57 | + const date = new Date("2024-03-15T18:59:00"); // Friday 18h59 |
| 58 | + const result = getNextClosestDay(["friday", "monday"], date); |
| 59 | + expect(result).toBe(5); // Friday (same day) |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe("Weekday before 19h cases", () => { |
| 64 | + it("should return same day if available and before 19h on Monday", () => { |
| 65 | + const date = new Date("2024-03-11T10:00:00"); // Monday 10h |
| 66 | + const result = getNextClosestDay(["monday", "wednesday"], date); |
| 67 | + expect(result).toBe(1); // Monday |
| 68 | + }); |
| 69 | + |
| 70 | + it("should return same day if available and before 19h on Tuesday", () => { |
| 71 | + const date = new Date("2024-03-12T15:00:00"); // Tuesday 15h |
| 72 | + const result = getNextClosestDay(["tuesday", "friday"], date); |
| 73 | + expect(result).toBe(2); // Tuesday |
| 74 | + }); |
| 75 | + |
| 76 | + it("should return next available day if current day not in schedule", () => { |
| 77 | + const date = new Date("2024-03-12T10:00:00"); // Tuesday 10h |
| 78 | + const result = getNextClosestDay(["wednesday", "friday"], date); |
| 79 | + expect(result).toBe(3); // Wednesday |
| 80 | + }); |
| 81 | + |
| 82 | + it("should wrap to first day of next week if no more days this week", () => { |
| 83 | + const date = new Date("2024-03-14T10:00:00"); // Thursday 10h |
| 84 | + const result = getNextClosestDay(["monday", "tuesday"], date); |
| 85 | + expect(result).toBe(1); // Monday (next week) |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe("Weekday after 19h cases", () => { |
| 90 | + it("should return next day when Monday after 19h", () => { |
| 91 | + const date = new Date("2024-03-11T19:30:00"); // Monday 19h30 |
| 92 | + const result = getNextClosestDay( |
| 93 | + ["monday", "tuesday", "wednesday"], |
| 94 | + date |
| 95 | + ); |
| 96 | + expect(result).toBe(2); // Tuesday |
| 97 | + }); |
| 98 | + |
| 99 | + it("should return next day when Tuesday after 19h", () => { |
| 100 | + const date = new Date("2024-03-12T20:00:00"); // Tuesday 20h |
| 101 | + const result = getNextClosestDay(["tuesday", "thursday"], date); |
| 102 | + expect(result).toBe(4); // Thursday |
| 103 | + }); |
| 104 | + |
| 105 | + it("should wrap to next week when Thursday after 19h and Friday not available", () => { |
| 106 | + const date = new Date("2024-03-14T19:30:00"); // Thursday 19h30 |
| 107 | + const result = getNextClosestDay(["monday", "tuesday", "thursday"], date); |
| 108 | + expect(result).toBe(1); // Monday (next week) |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe("Complex scheduling scenarios", () => { |
| 113 | + it("should handle case: Thursday enabled, current day is Friday - should return Thursday next week", () => { |
| 114 | + const date = new Date("2024-03-15T10:00:00"); // Friday 10h |
| 115 | + const result = getNextClosestDay(["thursday"], date); |
| 116 | + expect(result).toBe(4); // Thursday (next week) |
| 117 | + }); |
| 118 | + |
| 119 | + it("should handle multiple non-consecutive days", () => { |
| 120 | + const date = new Date("2024-03-12T20:00:00"); // Tuesday after 19h |
| 121 | + const result = getNextClosestDay(["monday", "thursday", "friday"], date); |
| 122 | + expect(result).toBe(4); // Thursday |
| 123 | + }); |
| 124 | + |
| 125 | + it("should handle single day scheduling", () => { |
| 126 | + const date = new Date("2024-03-13T10:00:00"); // Wednesday 10h |
| 127 | + const result = getNextClosestDay(["friday"], date); |
| 128 | + expect(result).toBe(5); // Friday |
| 129 | + }); |
| 130 | + |
| 131 | + it("should handle all working days enabled", () => { |
| 132 | + const date = new Date("2024-03-13T20:00:00"); // Wednesday after 19h |
| 133 | + const result = getNextClosestDay( |
| 134 | + ["monday", "tuesday", "wednesday", "thursday", "friday"], |
| 135 | + date |
| 136 | + ); |
| 137 | + expect(result).toBe(4); // Thursday |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe("Edge cases with hour boundaries", () => { |
| 142 | + it("should handle exactly 18h59 (before cutoff)", () => { |
| 143 | + const date = new Date("2024-03-13T18:59:59"); // Wednesday 18h59m59s |
| 144 | + const result = getNextClosestDay(["wednesday", "friday"], date); |
| 145 | + expect(result).toBe(3); // Wednesday (same day) |
| 146 | + }); |
| 147 | + |
| 148 | + it("should handle exactly 19h00 (at cutoff)", () => { |
| 149 | + const date = new Date("2024-03-13T19:00:00"); // Wednesday 19h00 |
| 150 | + const result = getNextClosestDay(["wednesday", "friday"], date); |
| 151 | + expect(result).toBe(5); // Friday (next day) |
| 152 | + }); |
| 153 | + |
| 154 | + it("should handle midnight (0h00)", () => { |
| 155 | + const date = new Date("2024-03-13T00:00:00"); // Wednesday 0h00 |
| 156 | + const result = getNextClosestDay(["wednesday", "friday"], date); |
| 157 | + expect(result).toBe(3); // Wednesday (same day) |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments