|
| 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 | +}); |
0 commit comments