Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Sidebar.snooze.test.ts

91 lines3.9 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { resolveSnoozePresets, snoozeWakeDescription } from "./Sidebar.snooze";
4​
5function localDate(year: number, month: number, day: number, hour: number, minute = 0): Date {
6 return new Date(year, month - 1, day, hour, minute, 0, 0);
7}
8​
9describe("resolveSnoozePresets", () => {
10 it("offers one hour, three hours, evening, tomorrow, and next week in the morning", () => {
11 const presets = resolveSnoozePresets(localDate(2026, 4, 8, 10), "locale");
12 expect(presets.map((preset) => preset.id)).toEqual([
13 "hour",
14 "three-hours",
15 "evening",
16 "tomorrow",
17 "next-week",
18 ]);
19 const threeHours = presets.find((preset) => preset.id === "three-hours");
20 expect(new Date(threeHours!.snoozedUntil).getHours()).toBe(13);
21 const evening = presets.find((preset) => preset.id === "evening");
22 expect(new Date(evening!.snoozedUntil).getHours()).toBe(18);
23 const tomorrow = presets.find((preset) => preset.id === "tomorrow");
24 const tomorrowDate = new Date(tomorrow!.snoozedUntil);
25 expect(tomorrowDate.getDate()).toBe(9);
26 expect(tomorrowDate.getHours()).toBe(9);
27 const nextWeek = presets.find((preset) => preset.id === "next-week");
28 const nextWeekDate = new Date(nextWeek!.snoozedUntil);
29 expect(nextWeekDate.getDay()).toBe(1);
30 expect(nextWeekDate.getDate()).toBe(13);
31 });
32​
33 it("whenLabel complements the label instead of repeating it", () => {
34 const presets = resolveSnoozePresets(localDate(2026, 4, 8, 10), "locale");
35 for (const preset of presets) {
36 expect(preset.whenLabel.toLowerCase()).not.toContain("tomorrow");
37 }
38 const tomorrow = presets.find((preset) => preset.id === "tomorrow");
39 expect(tomorrow!.whenLabel).toMatch(/9/);
40 const nextWeek = presets.find((preset) => preset.id === "next-week");
41 expect(nextWeek!.whenLabel).toMatch(/Mon/);
42 });
43​
44 it("drops the evening preset once evening is near or past", () => {
45 expect(
46 resolveSnoozePresets(localDate(2026, 4, 8, 17, 30), "locale").map((preset) => preset.id),
47 ).toEqual(["hour", "three-hours", "tomorrow", "next-week"]);
48 expect(
49 resolveSnoozePresets(localDate(2026, 4, 8, 21), "locale").map((preset) => preset.id),
50 ).toEqual(["hour", "three-hours", "tomorrow", "next-week"]);
51 });
52​
53 it("puts next week a full week out when today is Monday", () => {
54 const presets = resolveSnoozePresets(localDate(2026, 4, 6, 10), "locale");
55 const nextWeek = new Date(presets.find((preset) => preset.id === "next-week")!.snoozedUntil);
56 expect(nextWeek.getDay()).toBe(1);
57 expect(nextWeek.getDate()).toBe(13);
58 });
59 it("formats preset times with the selected clock preference", () => {
60 const twelveHour = resolveSnoozePresets(localDate(2026, 4, 8, 10), "12-hour");
61 const twentyFourHour = resolveSnoozePresets(localDate(2026, 4, 8, 10), "24-hour");
62​
63 expect(twelveHour.find((preset) => preset.id === "evening")!.whenLabel).toMatch(/PM/i);
64 expect(twentyFourHour.find((preset) => preset.id === "evening")!.whenLabel).toBe("18:00");
65 });
66});
67​
68describe("snoozeWakeDescription", () => {
69 const now = localDate(2026, 4, 8, 10);
70​
71 it("uses bare time today, 'tomorrow' next day, weekday within the week", () => {
72 expect(
73 snoozeWakeDescription(localDate(2026, 4, 8, 18).toISOString(), now, "locale"),
74 ).not.toContain("tomorrow");
75 expect(snoozeWakeDescription(localDate(2026, 4, 9, 9).toISOString(), now, "locale")).toContain(
76 "tomorrow",
77 );
78 expect(snoozeWakeDescription(localDate(2026, 4, 13, 9).toISOString(), now, "locale")).toMatch(
79 /Mon/,
80 );
81 });
82​
83 it("formats wake descriptions with the selected clock preference", () => {
84 expect(snoozeWakeDescription(localDate(2026, 4, 8, 18).toISOString(), now, "12-hour")).toMatch(
85 /PM/i,
86 );
87 expect(snoozeWakeDescription(localDate(2026, 4, 8, 18).toISOString(), now, "24-hour")).toBe(
88 "18:00",
89 );
90 });
91});