feat: visible date range

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-06-29 21:41:27 -04:00
parent 18a96f5dab
commit 51bf195664
10 changed files with 214 additions and 68 deletions
+64
View File
@@ -0,0 +1,64 @@
import { describe, expect, it } from 'vitest'
import {
daysVisibleRange,
monthGridVisibleRange,
shiftLocalDays,
shiftLocalMonths,
} from '../../../src/utils/dateRanges'
describe('calendar visible ranges', () => {
it('uses a half-open range for a day span', () => {
const range = daysVisibleRange(new Date(2026, 5, 15, 13, 45), 7)
const start = new Date(range.startMs)
const end = new Date(range.endMs)
expect([start.getFullYear(), start.getMonth(), start.getDate(), start.getHours()])
.toEqual([2026, 5, 15, 0])
expect([end.getFullYear(), end.getMonth(), end.getDate(), end.getHours()])
.toEqual([2026, 5, 22, 0])
})
it('uses calendar-day arithmetic across a daylight-saving boundary', () => {
const range = daysVisibleRange(new Date(2026, 2, 8, 12), 1)
const start = new Date(range.startMs)
const end = new Date(range.endMs)
expect(start.getHours()).toBe(0)
expect(end.getHours()).toBe(0)
expect(end.getDate()).toBe(start.getDate() + 1)
})
it('includes every leading and trailing day rendered by a month grid', () => {
const range = monthGridVisibleRange(new Date(2026, 5, 15))
const start = new Date(range.startMs)
const end = new Date(range.endMs)
expect([start.getFullYear(), start.getMonth(), start.getDate()]).toEqual([2026, 4, 31])
expect([end.getFullYear(), end.getMonth(), end.getDate()]).toEqual([2026, 6, 5])
})
it('calculates a leap-year February grid', () => {
const range = monthGridVisibleRange(new Date(2024, 1, 29))
const start = new Date(range.startMs)
const end = new Date(range.endMs)
expect([start.getFullYear(), start.getMonth(), start.getDate()]).toEqual([2024, 0, 28])
expect([end.getFullYear(), end.getMonth(), end.getDate()]).toEqual([2024, 2, 3])
})
})
describe('calendar navigation', () => {
it('clamps month navigation instead of skipping a shorter month', () => {
const shifted = shiftLocalMonths(new Date(2026, 0, 31, 9), 1)
expect([shifted.getFullYear(), shifted.getMonth(), shifted.getDate(), shifted.getHours()])
.toEqual([2026, 1, 28, 9])
})
it('uses calendar-day navigation', () => {
const shifted = shiftLocalDays(new Date(2026, 5, 30, 9), 1)
expect([shifted.getFullYear(), shifted.getMonth(), shifted.getDate(), shifted.getHours()])
.toEqual([2026, 6, 1, 9])
})
})