/** * @file dateRange.test.ts * @description The Analytics range presets: `?range=` parsing, a preset's * local bounds (week from Monday, month from the 1st), a custom range, and the query params `applyRange` sets. * @author Nguyễn Ngọc Trí Vĩ */ import { describe, expect, it } from "vitest"; import { applyRange, parsePreset, rangeDates } from "../dateRange"; describe("dateRange", () => { const today = new Date(2026, 9, 5, 12); // local 2026-10-05 it("parses presets, defaulting to all", () => { expect(parsePreset("30d")).toBe("30d"); expect(parsePreset("1y")).toBe("all"); expect(parsePreset(null)).toBe("all"); }); it("gives a preset's local bounds, today included", () => { expect(rangeDates("7d", {}, today)).toEqual({ from: "2026-09-29", to: "2026-10-05" }); expect(rangeDates("90d", {}, today).from).toBe("2026-07-08"); expect(rangeDates("all", {}, today)).toEqual({}); }); it("starts the week on Monday and the month on the 1st", () => { const wed = new Date(2026, 9, 7, 12); const sun = new Date(2026, 9, 11, 12); expect(rangeDates("today", {}, wed)).toEqual({ from: "2026-10-07", to: "2026-10-07" }); expect(rangeDates("week", {}, wed)).toEqual({ from: "2026-10-05", to: "2026-10-07" }); expect(rangeDates("week", {}, sun)).toEqual({ from: "2026-10-05", to: "2026-10-11" }); expect(rangeDates("week", {}, today)).toEqual({ from: "2026-10-05", to: "2026-10-05" }); expect(rangeDates("month", {}, wed)).toEqual({ from: "2026-10-01", to: "2026-10-07" }); expect(rangeDates("custom", { from: "2026-01-01", to: "" }, today)).toEqual({ from: "2026-01-01", to: undefined, }); }); it("sets from/to and tz_offset only when there is a bound", () => { expect(applyRange(new URLSearchParams(), {}).toString()).toBe(""); const qs = applyRange(new URLSearchParams(), { from: "2026-01-01" }); expect(qs.get("from")).toBe("2026-01-01"); expect(qs.has("to")).toBe(false); expect(qs.get("tz_offset")).toBe(String(new Date().getTimezoneOffset())); }); });