|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { matchDate, matchDateArray } from "./matchDate"; |
| 4 | + |
| 5 | +describe("matchDate", () => { |
| 6 | + // ─── Exact Date ────────────────────────────────────── |
| 7 | + it("matches exact Date", () => { |
| 8 | + const date = new Date(2024, 0, 15); |
| 9 | + expect(matchDate(date, new Date(2024, 0, 15))).toBe(true); |
| 10 | + expect(matchDate(date, new Date(2024, 0, 16))).toBe(false); |
| 11 | + }); |
| 12 | + |
| 13 | + it("ignores time when comparing exact Date", () => { |
| 14 | + const date = new Date(2024, 0, 15, 10, 30); |
| 15 | + expect(matchDate(date, new Date(2024, 0, 15, 23, 59))).toBe(true); |
| 16 | + }); |
| 17 | + |
| 18 | + // ─── Date[] ────────────────────────────────────────── |
| 19 | + it("matches Date[]", () => { |
| 20 | + const date = new Date(2024, 0, 15); |
| 21 | + const dates = [new Date(2024, 0, 10), new Date(2024, 0, 15)]; |
| 22 | + expect(matchDate(date, dates)).toBe(true); |
| 23 | + expect(matchDate(new Date(2024, 0, 1), dates)).toBe(false); |
| 24 | + }); |
| 25 | + |
| 26 | + it("returns false for empty Date[]", () => { |
| 27 | + expect(matchDate(new Date(2024, 0, 15), [])).toBe(false); |
| 28 | + }); |
| 29 | + |
| 30 | + // ─── { before } ───────────────────────────────────── |
| 31 | + it("matches { before }", () => { |
| 32 | + const jan10 = new Date(2024, 0, 10); |
| 33 | + expect(matchDate(new Date(2024, 0, 5), { before: jan10 })).toBe(true); |
| 34 | + expect(matchDate(new Date(2024, 0, 15), { before: jan10 })).toBe(false); |
| 35 | + }); |
| 36 | + |
| 37 | + it("{ before } is exclusive (same day returns false)", () => { |
| 38 | + const jan10 = new Date(2024, 0, 10); |
| 39 | + expect(matchDate(new Date(2024, 0, 10), { before: jan10 })).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + // ─── { after } ────────────────────────────────────── |
| 43 | + it("matches { after }", () => { |
| 44 | + const jan10 = new Date(2024, 0, 10); |
| 45 | + expect(matchDate(new Date(2024, 0, 15), { after: jan10 })).toBe(true); |
| 46 | + expect(matchDate(new Date(2024, 0, 5), { after: jan10 })).toBe(false); |
| 47 | + }); |
| 48 | + |
| 49 | + it("{ after } is exclusive (same day returns false)", () => { |
| 50 | + const jan10 = new Date(2024, 0, 10); |
| 51 | + expect(matchDate(new Date(2024, 0, 10), { after: jan10 })).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + // ─── { from, to } ─────────────────────────────────── |
| 55 | + it("matches { from, to } inclusive on boundaries", () => { |
| 56 | + const range = { from: new Date(2024, 0, 10), to: new Date(2024, 0, 20) }; |
| 57 | + expect(matchDate(new Date(2024, 0, 10), range)).toBe(true); |
| 58 | + expect(matchDate(new Date(2024, 0, 20), range)).toBe(true); |
| 59 | + expect(matchDate(new Date(2024, 0, 15), range)).toBe(true); |
| 60 | + expect(matchDate(new Date(2024, 0, 5), range)).toBe(false); |
| 61 | + expect(matchDate(new Date(2024, 0, 25), range)).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it("matches { from, to } when from === to (single day range)", () => { |
| 65 | + const sameDay = { from: new Date(2024, 0, 15), to: new Date(2024, 0, 15) }; |
| 66 | + expect(matchDate(new Date(2024, 0, 15), sameDay)).toBe(true); |
| 67 | + expect(matchDate(new Date(2024, 0, 14), sameDay)).toBe(false); |
| 68 | + expect(matchDate(new Date(2024, 0, 16), sameDay)).toBe(false); |
| 69 | + }); |
| 70 | + |
| 71 | + // ─── { dayOfWeek } ────────────────────────────────── |
| 72 | + it("matches { dayOfWeek }", () => { |
| 73 | + // 2024-01-15 is Monday (1) |
| 74 | + const monday = new Date(2024, 0, 15); |
| 75 | + expect(matchDate(monday, { dayOfWeek: [1, 3, 5] })).toBe(true); |
| 76 | + expect(matchDate(monday, { dayOfWeek: [0, 6] })).toBe(false); |
| 77 | + }); |
| 78 | + |
| 79 | + it("returns false for empty { dayOfWeek: [] }", () => { |
| 80 | + expect(matchDate(new Date(2024, 0, 15), { dayOfWeek: [] })).toBe(false); |
| 81 | + }); |
| 82 | + |
| 83 | + // ─── Function matcher ─────────────────────────────── |
| 84 | + it("matches function matcher", () => { |
| 85 | + const isWeekend = (d: Date) => d.getDay() === 0 || d.getDay() === 6; |
| 86 | + // 2024-01-14 is Sunday |
| 87 | + expect(matchDate(new Date(2024, 0, 14), isWeekend)).toBe(true); |
| 88 | + // 2024-01-15 is Monday |
| 89 | + expect(matchDate(new Date(2024, 0, 15), isWeekend)).toBe(false); |
| 90 | + }); |
| 91 | + |
| 92 | + it("matches function that always returns true", () => { |
| 93 | + expect(matchDate(new Date(2024, 0, 15), () => true)).toBe(true); |
| 94 | + }); |
| 95 | + |
| 96 | + it("matches function that always returns false", () => { |
| 97 | + expect(matchDate(new Date(2024, 0, 15), () => false)).toBe(false); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe("matchDateArray", () => { |
| 102 | + it("returns false for undefined", () => { |
| 103 | + expect(matchDateArray(new Date(), undefined)).toBe(false); |
| 104 | + }); |
| 105 | + |
| 106 | + it("handles single Matcher", () => { |
| 107 | + expect(matchDateArray(new Date(2024, 0, 5), { before: new Date(2024, 0, 10) })).toBe(true); |
| 108 | + }); |
| 109 | + |
| 110 | + it("handles Matcher[]", () => { |
| 111 | + const matchers = [{ before: new Date(2024, 0, 5) }, { after: new Date(2024, 0, 20) }]; |
| 112 | + expect(matchDateArray(new Date(2024, 0, 3), matchers)).toBe(true); |
| 113 | + expect(matchDateArray(new Date(2024, 0, 25), matchers)).toBe(true); |
| 114 | + expect(matchDateArray(new Date(2024, 0, 10), matchers)).toBe(false); |
| 115 | + }); |
| 116 | + |
| 117 | + it("handles Date[] as a single Matcher (not Matcher[])", () => { |
| 118 | + const dates = [new Date(2024, 0, 10), new Date(2024, 0, 15)]; |
| 119 | + expect(matchDateArray(new Date(2024, 0, 10), dates)).toBe(true); |
| 120 | + expect(matchDateArray(new Date(2024, 0, 15), dates)).toBe(true); |
| 121 | + expect(matchDateArray(new Date(2024, 0, 12), dates)).toBe(false); |
| 122 | + }); |
| 123 | + |
| 124 | + it("handles empty Matcher[] (returns false)", () => { |
| 125 | + expect(matchDateArray(new Date(2024, 0, 15), [])).toBe(false); |
| 126 | + }); |
| 127 | + |
| 128 | + it("returns true if any matcher in array matches", () => { |
| 129 | + const matchers = [new Date(2024, 0, 10), { dayOfWeek: [0, 6] }, (d: Date) => d.getDate() === 25]; |
| 130 | + // exact date match |
| 131 | + expect(matchDateArray(new Date(2024, 0, 10), matchers)).toBe(true); |
| 132 | + // dayOfWeek match (2024-01-14 is Sunday) |
| 133 | + expect(matchDateArray(new Date(2024, 0, 14), matchers)).toBe(true); |
| 134 | + // function match |
| 135 | + expect(matchDateArray(new Date(2024, 0, 25), matchers)).toBe(true); |
| 136 | + // no match |
| 137 | + expect(matchDateArray(new Date(2024, 0, 16), matchers)).toBe(false); |
| 138 | + }); |
| 139 | +}); |
0 commit comments