Skip to content

Commit 0058661

Browse files
author
songsang
committed
test(calendar): extract matchDate tests and add edge cases
Move matchDate/matchDateArray tests from useSelection.test.ts into a dedicated matchDate.test.ts, consistent with other utility tests. Add edge case coverage: - empty Date[] and { dayOfWeek: [] } - { before }/{ after } boundary exclusivity - { from, to } with same-day range - time-insensitive Date comparison - always true/false function matchers - Date[] treated as single Matcher vs Matcher[] - mixed Matcher array with multiple matcher types
1 parent 98c700c commit 0058661

2 files changed

Lines changed: 139 additions & 68 deletions

File tree

packages/calendar/src/useSelection.test.ts

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { describe, expect, it } from "vitest";
33

44
import type { CalendarBody } from "./models/Selection";
55
import { useSelection } from "./useSelection";
6-
import { matchDate, matchDateArray } from "./utils/matchDate";
76

87
const emptyBody: CalendarBody = { value: [] };
98

@@ -23,73 +22,6 @@ function createMockBody(dates: Date[][]): CalendarBody {
2322
};
2423
}
2524

26-
describe("matchDate", () => {
27-
it("matches exact Date", () => {
28-
const date = new Date(2024, 0, 15);
29-
expect(matchDate(date, new Date(2024, 0, 15))).toBe(true);
30-
expect(matchDate(date, new Date(2024, 0, 16))).toBe(false);
31-
});
32-
33-
it("matches Date[]", () => {
34-
const date = new Date(2024, 0, 15);
35-
const dates = [new Date(2024, 0, 10), new Date(2024, 0, 15)];
36-
expect(matchDate(date, dates)).toBe(true);
37-
expect(matchDate(new Date(2024, 0, 1), dates)).toBe(false);
38-
});
39-
40-
it("matches { before }", () => {
41-
const jan10 = new Date(2024, 0, 10);
42-
expect(matchDate(new Date(2024, 0, 5), { before: jan10 })).toBe(true);
43-
expect(matchDate(new Date(2024, 0, 15), { before: jan10 })).toBe(false);
44-
});
45-
46-
it("matches { after }", () => {
47-
const jan10 = new Date(2024, 0, 10);
48-
expect(matchDate(new Date(2024, 0, 15), { after: jan10 })).toBe(true);
49-
expect(matchDate(new Date(2024, 0, 5), { after: jan10 })).toBe(false);
50-
});
51-
52-
it("matches { from, to }", () => {
53-
const range = { from: new Date(2024, 0, 10), to: new Date(2024, 0, 20) };
54-
expect(matchDate(new Date(2024, 0, 15), range)).toBe(true);
55-
expect(matchDate(new Date(2024, 0, 10), range)).toBe(true);
56-
expect(matchDate(new Date(2024, 0, 20), range)).toBe(true);
57-
expect(matchDate(new Date(2024, 0, 5), range)).toBe(false);
58-
});
59-
60-
it("matches { dayOfWeek }", () => {
61-
// 2024-01-15 is Monday (1)
62-
const monday = new Date(2024, 0, 15);
63-
expect(matchDate(monday, { dayOfWeek: [1, 3, 5] })).toBe(true);
64-
expect(matchDate(monday, { dayOfWeek: [0, 6] })).toBe(false);
65-
});
66-
67-
it("matches function matcher", () => {
68-
const isWeekend = (d: Date) => d.getDay() === 0 || d.getDay() === 6;
69-
// 2024-01-14 is Sunday
70-
expect(matchDate(new Date(2024, 0, 14), isWeekend)).toBe(true);
71-
// 2024-01-15 is Monday
72-
expect(matchDate(new Date(2024, 0, 15), isWeekend)).toBe(false);
73-
});
74-
});
75-
76-
describe("matchDateArray", () => {
77-
it("returns false for undefined", () => {
78-
expect(matchDateArray(new Date(), undefined)).toBe(false);
79-
});
80-
81-
it("handles single Matcher", () => {
82-
expect(matchDateArray(new Date(2024, 0, 5), { before: new Date(2024, 0, 10) })).toBe(true);
83-
});
84-
85-
it("handles Matcher[]", () => {
86-
const matchers = [{ before: new Date(2024, 0, 5) }, { after: new Date(2024, 0, 20) }];
87-
expect(matchDateArray(new Date(2024, 0, 3), matchers)).toBe(true);
88-
expect(matchDateArray(new Date(2024, 0, 25), matchers)).toBe(true);
89-
expect(matchDateArray(new Date(2024, 0, 10), matchers)).toBe(false);
90-
});
91-
});
92-
9325
describe("useSelection - single mode", () => {
9426
it("starts with undefined selected", () => {
9527
// Given
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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

Comments
 (0)