Skip to content

Commit b409d2c

Browse files
committed
fix(web): respect 24-hour time entry in custom snooze
1 parent 2893437 commit b409d2c

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import "~/index.css";
2+
3+
import type { TimestampFormat } from "@t3tools/contracts/settings";
4+
import { expect, it, vi } from "vite-plus/test";
5+
import { page, userEvent } from "vite-plus/test/browser";
6+
7+
import { fakeTimers, render } from "../../test/render";
8+
import { CustomSnoozeDialogHost, requestCustomSnooze } from "./CustomSnoozeDialog";
9+
10+
const settings = vi.hoisted(() => ({ timestampFormat: "24-hour" as TimestampFormat }));
11+
vi.mock("../hooks/useSettings", () => ({
12+
useClientSettings: (selector: (value: typeof settings) => unknown) => selector(settings),
13+
}));
14+
15+
function setup(timestampFormat: TimestampFormat = "24-hour") {
16+
settings.timestampFormat = timestampFormat;
17+
const timers = fakeTimers({ toFake: ["Date"] });
18+
vi.setSystemTime(new Date(2030, 0, 2, 14, 30));
19+
return timers;
20+
}
21+
22+
function TestSnooze({ onResult }: { onResult: (result: unknown) => void }) {
23+
return (
24+
<>
25+
<button onClick={() => void requestCustomSnooze().then(onResult)}>Custom snooze</button>
26+
<CustomSnoozeDialogHost />
27+
</>
28+
);
29+
}
30+
31+
it.each(["17:30", "23:59"])("accepts and retains visible 24-hour entry %s", async (time) => {
32+
using _setup = setup();
33+
const onResult = vi.fn();
34+
await using screen = await render(<TestSnooze onResult={onResult} />);
35+
await userEvent.click(screen.getByRole("button", { name: "Custom snooze" }));
36+
37+
const field = page.getByRole("textbox", { name: "Time", exact: true });
38+
await expect.element(field).toHaveValue("15:30");
39+
await userEvent.fill(field, time);
40+
await expect.element(field).toHaveValue(time);
41+
await userEvent.click(page.getByRole("button", { name: "Snooze", exact: true }));
42+
43+
const [hours, minutes] = time.split(":").map(Number);
44+
expect(onResult).toHaveBeenCalledWith({
45+
snoozedUntil: new Date(2030, 0, 2, hours, minutes).toISOString(),
46+
});
47+
});
48+
49+
it("rejects invalid and past times, then accepts midnight on a future date", async () => {
50+
using _setup = setup();
51+
const onResult = vi.fn();
52+
await using screen = await render(<TestSnooze onResult={onResult} />);
53+
await userEvent.click(screen.getByRole("button", { name: "Custom snooze" }));
54+
55+
const field = page.getByRole("textbox", { name: "Time", exact: true });
56+
for (const time of ["", "24:00", "17:60", "5:30", "5:30 PM"]) {
57+
await userEvent.fill(field, time);
58+
await userEvent.click(page.getByRole("button", { name: "Snooze", exact: true }));
59+
expect(onResult).not.toHaveBeenCalled();
60+
expect((field.element() as HTMLInputElement).checkValidity()).toBe(false);
61+
}
62+
63+
await userEvent.fill(field, "00:00");
64+
await userEvent.click(page.getByRole("button", { name: "Snooze", exact: true }));
65+
await expect
66+
.element(page.getByRole("alert"))
67+
.toHaveTextContent("Choose a valid date and time in the future.");
68+
expect(onResult).not.toHaveBeenCalled();
69+
70+
await userEvent.click(page.getByRole("button", { name: "Date", exact: true }));
71+
await userEvent.click(page.getByRole("button", { name: "Thursday, January 3rd, 2030" }));
72+
await userEvent.click(page.getByRole("button", { name: "Snooze", exact: true }));
73+
expect(onResult).toHaveBeenCalledWith({
74+
snoozedUntil: new Date(2030, 0, 3, 0, 0).toISOString(),
75+
});
76+
});
77+
78+
it.each(["12-hour", "locale"] as const)("keeps native time entry for %s", async (format) => {
79+
using _setup = setup(format);
80+
const onResult = vi.fn();
81+
await using screen = await render(<TestSnooze onResult={onResult} />);
82+
await userEvent.click(screen.getByRole("button", { name: "Custom snooze" }));
83+
const field = page.getByLabelText("Time", { exact: true });
84+
await expect.element(field).toHaveAttribute("type", "time");
85+
await userEvent.fill(field, "17:30");
86+
await userEvent.click(page.getByRole("button", { name: "Snooze", exact: true }));
87+
expect(onResult).toHaveBeenCalledWith({
88+
snoozedUntil: new Date(2030, 0, 2, 17, 30).toISOString(),
89+
});
90+
});

‎apps/web/src/components/CustomSnoozeDialog.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Button } from "./ui/button";
1010
import { CalendarIcon } from "lucide-react";
1111
import { Calendar } from "./ui/calendar";
1212
import { weekStartsOn } from "../timestampFormat";
13+
import { useClientSettings } from "../hooks/useSettings";
1314
import { Popover, PopoverTrigger, PopoverPopup } from "./ui/popover";
1415
import { Input } from "./ui/input";
1516
import { Label } from "./ui/label";
@@ -55,6 +56,7 @@ export function CustomSnoozeDialogHost() {
5556

5657
function CustomSnoozeDialog() {
5758
const id = useId();
59+
const is24HourTime = useClientSettings((settings) => settings.timestampFormat === "24-hour");
5860
const [initial] = useState(() => new Date(Date.now() + 3_600_000));
5961
const [mode, setMode] = useState<CustomSnoozeInput["mode"]>("date");
6062
const [date, setDate] = useState(initial);
@@ -153,7 +155,11 @@ function CustomSnoozeDialog() {
153155
nativeInput
154156
id={`${id}-time`}
155157
className="h-9 sm:h-8"
156-
type="time"
158+
// Native time inputs follow the browser/OS hour cycle, not our setting.
159+
type={is24HourTime ? "text" : "time"}
160+
placeholder={is24HourTime ? "HH:MM" : undefined}
161+
pattern={is24HourTime ? "([01][0-9]|2[0-3]):[0-5][0-9]" : undefined}
162+
title={is24HourTime ? "Enter a 24-hour time (HH:MM)." : undefined}
157163
required
158164
value={time}
159165
onChange={(event) => {

0 commit comments

Comments
 (0)