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

Sidebar.snooze.test.ts

93 lines3.9 KB

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