@klinking/colander 3.1.0-alpha.4 → 3.1.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/formats/date.cjs +1 -1
  2. package/dist/formats/date.d.cts +1 -1
  3. package/dist/formats/date.d.mts +1 -1
  4. package/dist/formats/date.mjs +1 -1
  5. package/dist/formats/object.cjs +1 -1
  6. package/dist/formats/object.d.cts +1 -1
  7. package/dist/formats/object.d.mts +1 -1
  8. package/dist/formats/object.mjs +1 -1
  9. package/dist/formats/plain-date-time.cjs +1 -1
  10. package/dist/formats/plain-date-time.d.cts +1 -1
  11. package/dist/formats/plain-date-time.d.mts +1 -1
  12. package/dist/formats/plain-date-time.mjs +1 -1
  13. package/dist/formats/plain-date.cjs +1 -1
  14. package/dist/formats/plain-date.d.cts +1 -1
  15. package/dist/formats/plain-date.d.mts +1 -1
  16. package/dist/formats/plain-date.mjs +1 -1
  17. package/dist/formats/plain-month-day.cjs +1 -1
  18. package/dist/formats/plain-month-day.d.cts +1 -1
  19. package/dist/formats/plain-month-day.d.mts +1 -1
  20. package/dist/formats/plain-month-day.mjs +1 -1
  21. package/dist/formats/plain-year-month.cjs +1 -1
  22. package/dist/formats/plain-year-month.d.cts +1 -1
  23. package/dist/formats/plain-year-month.d.mts +1 -1
  24. package/dist/formats/plain-year-month.mjs +1 -1
  25. package/dist/formats/zoned-date-time.cjs +1 -1
  26. package/dist/formats/zoned-date-time.d.cts +1 -1
  27. package/dist/formats/zoned-date-time.d.mts +1 -1
  28. package/dist/formats/zoned-date-time.mjs +1 -1
  29. package/dist/{weeks-view-context-CUCy8njs.d.cts → index-DnFCKyyw.d.cts} +71 -8
  30. package/dist/{weeks-view-context-E1NwyNgQ.d.mts → index-q3cQKJdA.d.mts} +71 -8
  31. package/dist/index.cjs +1 -1
  32. package/dist/index.d.cts +2 -11
  33. package/dist/index.d.mts +2 -11
  34. package/dist/index.mjs +1 -1
  35. package/dist/{weeks-navigation-elCpdfQ3.mjs → weeks-navigation-Dn3UtG76.mjs} +578 -589
  36. package/dist/{weeks-navigation-DV538__x.cjs → weeks-navigation-SuKJZJCM.cjs} +578 -589
  37. package/package.json +1 -1
  38. package/src/calendar-provider.tsx +10 -55
  39. package/src/day-cell-state.test.ts +214 -0
  40. package/src/day-cell-state.ts +314 -0
  41. package/src/day-cell.tsx +29 -383
  42. package/src/month-view-navigation.ts +180 -0
  43. package/src/month-view-types.ts +3 -0
  44. package/src/month-view.bounds.test.tsx +136 -0
  45. package/src/month-view.tsx +37 -205
  46. package/src/month-window.test.ts +207 -0
  47. package/src/month-window.ts +93 -0
  48. package/src/navigation.tsx +15 -74
  49. package/src/range-preview.test.ts +120 -0
  50. package/src/root-selection.ts +99 -97
  51. package/src/weeks-navigation.tsx +7 -32
  52. package/src/weeks-view-state.ts +24 -81
  53. package/src/weeks-view-types.ts +4 -1
  54. package/src/weeks-window.test.ts +43 -0
  55. package/src/weeks-window.ts +64 -0
@@ -0,0 +1,136 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { act, cleanup, render, screen } from "@testing-library/react";
3
+ import { afterEach, describe, expect, it, vi } from "vitest";
4
+ import { MonthView } from "./month-view";
5
+ import type { MonthViewProps } from "./month-view";
6
+ import { useMonthViewStable, useMonthViewState } from "./month-view-context";
7
+ import { useViewContext } from "./view-context";
8
+
9
+ const T = Temporal;
10
+
11
+ let go: { next: () => void; prev: () => void };
12
+
13
+ function Probe() {
14
+ const { goNextMonth, goPrevMonth } = useMonthViewStable();
15
+ const { currentMonth } = useMonthViewState();
16
+ const { focusedDate } = useViewContext();
17
+ go = { next: goNextMonth, prev: goPrevMonth };
18
+ return (
19
+ <>
20
+ <span data-testid="month">
21
+ {currentMonth.year}-{String(currentMonth.month).padStart(2, "0")}
22
+ </span>
23
+ <span data-testid="focus">{focusedDate.toString()}</span>
24
+ </>
25
+ );
26
+ }
27
+
28
+ const base = {
29
+ temporal: T,
30
+ timeZone: "UTC",
31
+ defaultValue: T.PlainDate.from("2026-03-15"),
32
+ } as const;
33
+
34
+ const month = () => screen.getByTestId("month").textContent;
35
+ const focus = () => screen.getByTestId("focus").textContent;
36
+
37
+ afterEach(cleanup);
38
+
39
+ describe("MonthView goNextMonth/goPrevMonth honor outOfRangeBehavior", () => {
40
+ it.each([
41
+ {
42
+ description: "stop: goPrevMonth into the min month moves",
43
+ props: {
44
+ min: T.PlainDate.from("2026-02-10"),
45
+ outOfRangeBehavior: "stop",
46
+ },
47
+ call: "prev" as const,
48
+ expected: { month: "2026-02", focus: "2026-02-01" },
49
+ },
50
+ {
51
+ description: "stop: goPrevMonth before the min month does nothing",
52
+ props: {
53
+ min: T.PlainDate.from("2026-03-10"),
54
+ outOfRangeBehavior: "stop",
55
+ },
56
+ call: "prev" as const,
57
+ expected: { month: "2026-03", focus: "2026-03-15" },
58
+ },
59
+ {
60
+ description: "stop: goNextMonth past the max month does nothing",
61
+ props: {
62
+ max: T.PlainDate.from("2026-03-20"),
63
+ outOfRangeBehavior: "stop",
64
+ },
65
+ call: "next" as const,
66
+ expected: { month: "2026-03", focus: "2026-03-15" },
67
+ },
68
+ {
69
+ description: "stop: goNextMonth with only min set still moves",
70
+ props: {
71
+ min: T.PlainDate.from("2026-03-10"),
72
+ outOfRangeBehavior: "stop",
73
+ },
74
+ call: "next" as const,
75
+ expected: { month: "2026-04", focus: "2026-04-01" },
76
+ },
77
+ {
78
+ description: "unbounded: goNextMonth moves past max",
79
+ props: { max: T.PlainDate.from("2026-03-20") },
80
+ call: "next" as const,
81
+ expected: { month: "2026-04", focus: "2026-04-01" },
82
+ },
83
+ ] satisfies {
84
+ description: string;
85
+ props: Partial<MonthViewProps>;
86
+ call: "next" | "prev";
87
+ expected: { month: string; focus: string };
88
+ }[])("uncontrolled, $description", ({ props, call, expected }) => {
89
+ render(
90
+ <MonthView {...base} {...props}>
91
+ <Probe />
92
+ </MonthView>,
93
+ );
94
+
95
+ act(() => go[call]());
96
+
97
+ expect({ month: month(), focus: focus() }).toEqual(expected);
98
+ });
99
+
100
+ it.each([
101
+ {
102
+ description: "stop: blocked step does not notify the parent",
103
+ props: {
104
+ max: T.PlainDate.from("2026-03-20"),
105
+ outOfRangeBehavior: "stop",
106
+ },
107
+ expected: { calls: 0 },
108
+ },
109
+ {
110
+ description: "unbounded: step notifies the parent once",
111
+ props: { max: T.PlainDate.from("2026-03-20") },
112
+ expected: { calls: 1 },
113
+ },
114
+ ] satisfies {
115
+ description: string;
116
+ props: Partial<MonthViewProps>;
117
+ expected: { calls: number };
118
+ }[])("controlled, $description", ({ props, expected }) => {
119
+ const onMonthChange = vi.fn();
120
+ render(
121
+ <MonthView
122
+ {...base}
123
+ {...props}
124
+ month={T.PlainYearMonth.from("2026-03")}
125
+ onMonthChange={onMonthChange}
126
+ >
127
+ <Probe />
128
+ </MonthView>,
129
+ );
130
+
131
+ act(() => go.next());
132
+
133
+ expect({ calls: onMonthChange.mock.calls.length }).toEqual(expected);
134
+ expect(month()).toBe("2026-03");
135
+ });
136
+ });
@@ -1,5 +1,5 @@
1
1
  import type { Temporal } from "@js-temporal/polyfill";
2
- import { useCallback, useEffect, useMemo, useRef, useState } from "react";
2
+ import { useCallback, useMemo, useRef, useState } from "react";
3
3
  import { useCalendarStable, useCalendarState } from "./calendar-context";
4
4
  import { CalendarProvider } from "./calendar-provider";
5
5
  import type { CalendarProviderProps } from "./calendar-types";
@@ -7,20 +7,19 @@ import {
7
7
  MonthViewStableContext,
8
8
  MonthViewStateContext,
9
9
  } from "./month-view-context";
10
+ import { useMonthNavigation } from "./month-view-navigation";
10
11
  import type {
11
12
  MonthViewRootProps,
12
13
  MonthViewStableContextValue,
13
14
  MonthViewStateContextValue,
14
15
  } from "./month-view-types";
16
+ import { createMonthWindowPolicy } from "./month-window";
15
17
  import type { MonthData, RootState, ValueFormat } from "./types";
16
18
  import {
17
- computeAdjacentMonth,
18
- focusedDateForMonth,
19
19
  getMonthWeeks,
20
20
  resolveFocusTarget,
21
- selectedToZdt,
22
- shiftWindowToMonth,
23
21
  resolveInitialAnchor,
22
+ selectedToZdt,
24
23
  } from "./utils";
25
24
  import {
26
25
  ViewContext,
@@ -59,24 +58,6 @@ function MonthViewRoot(props: MonthViewRootProps) {
59
58
  const T = calStable.temporal;
60
59
  const { timeZone, today, weekStartDay, isDateDisabled } = calStable;
61
60
 
62
- // --- Month navigation state (controlled/uncontrolled) ---
63
- const isMonthControlled = monthProp !== undefined;
64
-
65
- const [internalMonth, setInternalMonth] = useState<{
66
- year: number;
67
- month: number;
68
- }>(() => {
69
- if (monthProp) return { year: monthProp.year, month: monthProp.month };
70
- if (defaultMonthProp)
71
- return { year: defaultMonthProp.year, month: defaultMonthProp.month };
72
- const anchor = resolveInitialAnchor(calState.selected, today, timeZone, T);
73
- return { year: anchor.year, month: anchor.month };
74
- });
75
-
76
- const currentMonth = isMonthControlled
77
- ? { year: monthProp.year, month: monthProp.month }
78
- : internalMonth;
79
-
80
61
  // --- Focus state ---
81
62
  const [focusedDate, setFocusedDate] = useState<Temporal.PlainDate>(() =>
82
63
  resolveInitialAnchor(calState.selected, today, timeZone, T),
@@ -85,6 +66,37 @@ function MonthViewRoot(props: MonthViewRootProps) {
85
66
  const [gridHasFocus, setGridHasFocus] = useState(false);
86
67
  const gridFocusedRef = useRef(false);
87
68
 
69
+ // --- Month window and navigation ---
70
+ const { minValue, maxValue } = calStable;
71
+ const windowPolicy = useMemo(
72
+ () =>
73
+ createMonthWindowPolicy({
74
+ numberOfMonths,
75
+ behavior: outOfRangeBehavior,
76
+ min: minValue,
77
+ max: maxValue,
78
+ T,
79
+ }),
80
+ [numberOfMonths, outOfRangeBehavior, minValue, maxValue, T],
81
+ );
82
+
83
+ const initialAnchor = useCallback(
84
+ () => resolveInitialAnchor(calState.selected, today, timeZone, T),
85
+ [calState.selected, today, timeZone, T],
86
+ );
87
+
88
+ const { currentMonth, viewingYearMonth, goNextMonth, goPrevMonth } =
89
+ useMonthNavigation({
90
+ month: monthProp,
91
+ defaultMonth: defaultMonthProp,
92
+ initialAnchor,
93
+ onMonthChange,
94
+ policy: windowPolicy,
95
+ focusedDate,
96
+ setFocusedDate,
97
+ T,
98
+ });
99
+
88
100
  // --- Grid label IDs ---
89
101
  const [gridLabelIds, setGridLabelIds] = useState<Record<number, string>>({});
90
102
  const setGridLabelId = useCallback(
@@ -103,162 +115,6 @@ function MonthViewRoot(props: MonthViewRootProps) {
103
115
  [],
104
116
  );
105
117
 
106
- // Always-current ref so callbacks/effects don't re-subscribe on every
107
- // onMonthChange identity change.
108
- const onMonthChangeRef = useRef(onMonthChange);
109
- onMonthChangeRef.current = onMonthChange;
110
-
111
- // Request the parent move the view to `{year, month}` (controlled mode).
112
- // Built in ISO — `year`/`month` are ISO numbers, and the locale calendar
113
- // only affects display (see MonthYearString). Passing `calendar` here would
114
- // reinterpret the ISO numbers as locale-calendar fields (e.g. Buddhist),
115
- // shifting the value by centuries.
116
- const notifyMonth = useCallback(
117
- (year: number, month: number) => {
118
- onMonthChangeRef.current?.(T.PlainYearMonth.from({ year, month }));
119
- },
120
- [T],
121
- );
122
-
123
- // Whether `{year, month}` falls within the currently displayed window.
124
- const isMonthVisible = useCallback(
125
- (targetYear: number, targetMonth: number) => {
126
- for (let i = 0; i < numberOfMonths; i++) {
127
- const totalMonths =
128
- currentMonth.year * 12 + (currentMonth.month - 1) + i;
129
- const y = Math.floor(totalMonths / 12);
130
- const m = (totalMonths % 12) + 1;
131
- if (targetYear === y && targetMonth === m) return true;
132
- }
133
- return false;
134
- },
135
- [currentMonth.year, currentMonth.month, numberOfMonths],
136
- );
137
-
138
- // --- navigateToMonth: shift the window by the minimum needed to reveal the
139
- // target month (keeps keyboard crossing symmetric with the Prev/Next
140
- // buttons, which step one month at a time). ---
141
- const navigateToMonth = useCallback(
142
- (targetYear: number, targetMonth: number) => {
143
- if (isMonthControlled) return; // controlled mode: parent manages
144
- setInternalMonth((prev) => {
145
- const next = shiftWindowToMonth(
146
- prev,
147
- { year: targetYear, month: targetMonth },
148
- numberOfMonths,
149
- );
150
- if (next.year === prev.year && next.month === prev.month) return prev;
151
- return next;
152
- });
153
- },
154
- [isMonthControlled, numberOfMonths],
155
- );
156
-
157
- // Button navigation in controlled mode notifies the parent directly, then
158
- // moves focus into the target month. This ref holds that focus value so the
159
- // focus-sync effect skips re-notifying for it. Keyed by the value (not a
160
- // one-shot flag) so it can't strand and suppress a later keyboard crossing
161
- // if the button's focus move is a no-op or the parent ignores the change.
162
- const skipFocusSyncForRef = useRef<Temporal.PlainDate | null>(null);
163
- const prevFocusedRef = useRef(focusedDate);
164
-
165
- // Clear a stale skip target whenever the parent actually moves the view.
166
- useEffect(() => {
167
- skipFocusSyncForRef.current = null;
168
- }, [monthProp?.year, monthProp?.month]);
169
-
170
- // Sync month when the focused date crosses a month boundary. Uncontrolled:
171
- // shift internal state. Controlled: ask the parent to move the view, but
172
- // only when focus actually moved out of the visible window (keyboard nav) —
173
- // never as an echo of an external `month` update or a button click.
174
- useEffect(() => {
175
- const prevFocused = prevFocusedRef.current;
176
- prevFocusedRef.current = focusedDate;
177
- if (isMonthControlled) {
178
- if (skipFocusSyncForRef.current === focusedDate) {
179
- skipFocusSyncForRef.current = null;
180
- return;
181
- }
182
- if (prevFocused === focusedDate) return;
183
- if (isMonthVisible(focusedDate.year, focusedDate.month)) return;
184
- // Emit the minimally-shifted first-visible month (what the parent binds
185
- // to `month`), not the focused month — so a controlled window scrolls by
186
- // the minimum, matching uncontrolled mode and the nav buttons.
187
- const next = shiftWindowToMonth(
188
- { year: currentMonth.year, month: currentMonth.month },
189
- { year: focusedDate.year, month: focusedDate.month },
190
- numberOfMonths,
191
- );
192
- notifyMonth(next.year, next.month);
193
- return;
194
- }
195
- navigateToMonth(focusedDate.year, focusedDate.month);
196
- }, [
197
- focusedDate,
198
- navigateToMonth,
199
- isMonthControlled,
200
- isMonthVisible,
201
- notifyMonth,
202
- currentMonth.year,
203
- currentMonth.month,
204
- numberOfMonths,
205
- ]);
206
-
207
- // --- Month navigation callbacks ---
208
- const goNextMonth = useCallback(() => {
209
- if (isMonthControlled) {
210
- const { year, month, firstDay } = computeAdjacentMonth(
211
- { year: monthProp!.year, month: monthProp!.month },
212
- "next",
213
- T,
214
- );
215
- // Notify the parent directly; the follow-on focus move is suppressed in
216
- // the focus-sync effect (keyed on the value) so onMonthChange fires once.
217
- const nextFocus = focusedDateForMonth(
218
- focusedDate,
219
- { year, month },
220
- firstDay,
221
- );
222
- skipFocusSyncForRef.current = nextFocus;
223
- notifyMonth(year, month);
224
- setFocusedDate(nextFocus);
225
- return;
226
- }
227
- setInternalMonth((m) => {
228
- const { year, month, firstDay } = computeAdjacentMonth(m, "next", T);
229
- setFocusedDate((prev) =>
230
- focusedDateForMonth(prev, { year, month }, firstDay),
231
- );
232
- return { year, month };
233
- });
234
- }, [T, isMonthControlled, monthProp, notifyMonth, focusedDate]);
235
-
236
- const goPrevMonth = useCallback(() => {
237
- if (isMonthControlled) {
238
- const { year, month, firstDay } = computeAdjacentMonth(
239
- { year: monthProp!.year, month: monthProp!.month },
240
- "prev",
241
- T,
242
- );
243
- const nextFocus = focusedDateForMonth(
244
- focusedDate,
245
- { year, month },
246
- firstDay,
247
- );
248
- skipFocusSyncForRef.current = nextFocus;
249
- notifyMonth(year, month);
250
- setFocusedDate(nextFocus);
251
- return;
252
- }
253
- setInternalMonth((m) => {
254
- const { year, month, firstDay } = computeAdjacentMonth(m, "prev", T);
255
- setFocusedDate((prev) =>
256
- focusedDateForMonth(prev, { year, month }, firstDay),
257
- );
258
- return { year, month };
259
- });
260
- }, [T, isMonthControlled, monthProp, notifyMonth, focusedDate]);
261
-
262
118
  // --- Grid computation ---
263
119
  const allMonths = useMemo<MonthData[]>(() => {
264
120
  const opts = { weekStartDay, fixedWeeks };
@@ -340,32 +196,6 @@ function MonthViewRoot(props: MonthViewRootProps) {
340
196
  ],
341
197
  );
342
198
 
343
- // --- viewingYearMonth (for the render-prop rootState) ---
344
- // ISO — the locale calendar only affects display, not this value.
345
- const viewingYearMonth = useMemo(
346
- () =>
347
- T.PlainYearMonth.from({
348
- year: currentMonth.year,
349
- month: currentMonth.month,
350
- }),
351
- [currentMonth, T],
352
- );
353
-
354
- // --- Fire onMonthChange when uncontrolled navigation moves the view (not on
355
- // mount). In controlled mode the parent owns `month`, so notification flows
356
- // from button clicks (goNext/goPrevMonth) and the focus-sync effect instead;
357
- // firing here would just echo the parent's own update back to it. ---
358
- const mountedRef = useRef(false);
359
-
360
- useEffect(() => {
361
- if (!mountedRef.current) {
362
- mountedRef.current = true;
363
- return;
364
- }
365
- if (isMonthControlled) return;
366
- onMonthChangeRef.current?.(viewingYearMonth);
367
- }, [viewingYearMonth, isMonthControlled]);
368
-
369
199
  // --- rootState (for render functions) ---
370
200
  const rootState = useMemo<RootState>(
371
201
  () => ({
@@ -383,6 +213,7 @@ function MonthViewRoot(props: MonthViewRootProps) {
383
213
  fixedWeeks,
384
214
  outsideDays,
385
215
  outOfRangeBehavior,
216
+ windowPolicy,
386
217
  goNextMonth,
387
218
  goPrevMonth,
388
219
  setGridLabelId,
@@ -393,6 +224,7 @@ function MonthViewRoot(props: MonthViewRootProps) {
393
224
  fixedWeeks,
394
225
  outsideDays,
395
226
  outOfRangeBehavior,
227
+ windowPolicy,
396
228
  goNextMonth,
397
229
  goPrevMonth,
398
230
  setGridLabelId,
@@ -0,0 +1,207 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { describe, expect, it } from "vitest";
3
+ import { createMonthWindowPolicy } from "./month-window";
4
+ import type { MonthWindowPolicyInput } from "./month-window";
5
+
6
+ const T = Temporal;
7
+
8
+ const policy = (overrides: Partial<MonthWindowPolicyInput> = {}) =>
9
+ createMonthWindowPolicy({
10
+ numberOfMonths: 1,
11
+ behavior: "stop",
12
+ min: undefined,
13
+ max: undefined,
14
+ T,
15
+ ...overrides,
16
+ });
17
+
18
+ const date = (iso: string) => T.PlainDate.from(iso);
19
+
20
+ describe("MonthWindowPolicy.step", () => {
21
+ it.each([
22
+ {
23
+ description: "next across a year boundary",
24
+ start: { year: 2026, month: 12 },
25
+ direction: "next" as const,
26
+ expected: {
27
+ start: { year: 2027, month: 1 },
28
+ entering: { year: 2027, month: 1 },
29
+ firstDay: "2027-01-01",
30
+ blocked: false,
31
+ },
32
+ },
33
+ {
34
+ description: "prev across a year boundary",
35
+ start: { year: 2026, month: 1 },
36
+ direction: "prev" as const,
37
+ expected: {
38
+ start: { year: 2025, month: 12 },
39
+ entering: { year: 2025, month: 12 },
40
+ firstDay: "2025-12-01",
41
+ blocked: false,
42
+ },
43
+ },
44
+ {
45
+ description:
46
+ "next with two months visible enters the month after the window",
47
+ start: { year: 2026, month: 3 },
48
+ direction: "next" as const,
49
+ numberOfMonths: 2,
50
+ expected: {
51
+ start: { year: 2026, month: 4 },
52
+ entering: { year: 2026, month: 5 },
53
+ firstDay: "2026-04-01",
54
+ blocked: false,
55
+ },
56
+ },
57
+ ])("$description", ({ start, direction, numberOfMonths, expected }) => {
58
+ const step = policy({ numberOfMonths: numberOfMonths ?? 1 }).step(
59
+ start,
60
+ direction,
61
+ );
62
+ expect({ ...step, firstDay: step.firstDay.toString() }).toEqual(expected);
63
+ });
64
+
65
+ it.each([
66
+ {
67
+ description: "stop: prev into the min month is allowed (min mid-month)",
68
+ direction: "prev" as const,
69
+ start: { year: 2026, month: 4 },
70
+ min: "2026-03-20",
71
+ expected: false,
72
+ },
73
+ {
74
+ description: "stop: prev before the min month is blocked",
75
+ direction: "prev" as const,
76
+ start: { year: 2026, month: 3 },
77
+ min: "2026-03-20",
78
+ expected: true,
79
+ },
80
+ {
81
+ description: "stop: next into the max month is allowed",
82
+ direction: "next" as const,
83
+ start: { year: 2026, month: 2 },
84
+ max: "2026-03-05",
85
+ expected: false,
86
+ },
87
+ {
88
+ description: "stop: next past the max month is blocked",
89
+ direction: "next" as const,
90
+ start: { year: 2026, month: 3 },
91
+ max: "2026-03-05",
92
+ expected: true,
93
+ },
94
+ {
95
+ description: "stop: the window's last month is what must stay within max",
96
+ direction: "next" as const,
97
+ start: { year: 2026, month: 2 },
98
+ numberOfMonths: 2,
99
+ max: "2026-03-31",
100
+ expected: true,
101
+ },
102
+ {
103
+ description: "stop: min only never blocks next",
104
+ direction: "next" as const,
105
+ start: { year: 2030, month: 1 },
106
+ min: "2026-03-20",
107
+ expected: false,
108
+ },
109
+ {
110
+ description: "stop: max only never blocks prev",
111
+ direction: "prev" as const,
112
+ start: { year: 2020, month: 1 },
113
+ max: "2026-03-20",
114
+ expected: false,
115
+ },
116
+ {
117
+ description: "unbounded: never blocks past min",
118
+ direction: "prev" as const,
119
+ behavior: "unbounded" as const,
120
+ start: { year: 2026, month: 3 },
121
+ min: "2026-03-20",
122
+ expected: false,
123
+ },
124
+ {
125
+ description: "unbounded: never blocks past max",
126
+ direction: "next" as const,
127
+ behavior: "unbounded" as const,
128
+ start: { year: 2026, month: 3 },
129
+ max: "2026-03-05",
130
+ expected: false,
131
+ },
132
+ ])(
133
+ "$description",
134
+ ({ direction, start, min, max, numberOfMonths, behavior, expected }) => {
135
+ const blocked = policy({
136
+ numberOfMonths: numberOfMonths ?? 1,
137
+ behavior: behavior ?? "stop",
138
+ min: min ? date(min) : undefined,
139
+ max: max ? date(max) : undefined,
140
+ }).step(start, direction).blocked;
141
+ expect(blocked).toBe(expected);
142
+ },
143
+ );
144
+ });
145
+
146
+ describe("MonthWindowPolicy.isVisible", () => {
147
+ it.each([
148
+ {
149
+ description: "the first month",
150
+ target: { year: 2026, month: 3 },
151
+ expected: true,
152
+ },
153
+ {
154
+ description: "the last month",
155
+ target: { year: 2026, month: 5 },
156
+ expected: true,
157
+ },
158
+ {
159
+ description: "the month before the window",
160
+ target: { year: 2026, month: 2 },
161
+ expected: false,
162
+ },
163
+ {
164
+ description: "the month after the window",
165
+ target: { year: 2026, month: 6 },
166
+ expected: false,
167
+ },
168
+ {
169
+ description: "the same month number in another year",
170
+ target: { year: 2027, month: 3 },
171
+ expected: false,
172
+ },
173
+ ])("$description", ({ target, expected }) => {
174
+ expect(
175
+ policy({ numberOfMonths: 3 }).isVisible({ year: 2026, month: 3 }, target),
176
+ ).toBe(expected);
177
+ });
178
+ });
179
+
180
+ describe("MonthWindowPolicy.reveal", () => {
181
+ it.each([
182
+ {
183
+ description: "keeps the window when the target is visible",
184
+ target: { year: 2026, month: 4 },
185
+ expected: { year: 2026, month: 3 },
186
+ },
187
+ {
188
+ description: "target before the window becomes the first month",
189
+ target: { year: 2026, month: 1 },
190
+ expected: { year: 2026, month: 1 },
191
+ },
192
+ {
193
+ description: "target after the window becomes the last month",
194
+ target: { year: 2026, month: 8 },
195
+ expected: { year: 2026, month: 7 },
196
+ },
197
+ {
198
+ description: "target after the window across a year boundary",
199
+ target: { year: 2027, month: 2 },
200
+ expected: { year: 2027, month: 1 },
201
+ },
202
+ ])("$description", ({ target, expected }) => {
203
+ expect(
204
+ policy({ numberOfMonths: 2 }).reveal({ year: 2026, month: 3 }, target),
205
+ ).toEqual(expected);
206
+ });
207
+ });