import { fireEvent, screen } from "@testing-library/react"; /** "09:30" -> "9:30 AM", "00:00" -> "12:00 AM", "23:59" -> "11:59 PM": the DateTimePicker's option label. */ export function timeOptionLabel(hhmm: string) { const [h, m] = hhmm.split(":").map(Number); return `${h % 12 || 12}:${String(m).padStart(2, "0")} ${h < 12 ? "AM" : "PM"}`; } /** * Picks `hhmm` (HH:mm) in the time dropdown labelled `label`. * Keyboard events, not userEvent: a full pointer simulation over the 97-option list made form tests * that set two or four times run past the test timeout on a loaded machine. * The option is found by its text, not by role and name: computing the accessible name of every * option took about 1 s per pick in jsdom, against about 30 ms for a text match. */ export async function pickTime(label: string | RegExp, hhmm: string) { fireEvent.keyDown(screen.getByLabelText(label), { key: "Enter" }); const text = await screen.findByText(timeOptionLabel(hhmm), { selector: '[role="option"] *' }); fireEvent.keyDown(text.closest('[role="option"]')!, { key: "Enter" }); }