import { fireEvent, screen, within } from "@testing-library/react"; function topPopover() { const dialogs = screen.getAllByRole("dialog"); return dialogs[dialogs.length - 1]; } /** Shows `iso`'s month in the popover's first calendar month and clicks the day. */ function clickDay(pop: HTMLElement, iso: string) { const [month, year] = within(pop).getAllByRole("combobox"); const [y, m] = iso.split("-").map(Number); fireEvent.change(year, { target: { value: String(y) } }); fireEvent.change(month, { target: { value: String(m - 1) } }); const day = pop.querySelector(`[data-day="${iso}"] button`); // A disabled day ignores the click, so fail loudly instead of letting the test pass vacuously. if (!day || day.disabled) throw new Error(`pickDate: ${iso} is not an enabled day in the calendar`); fireEvent.click(day); } /** Picks `iso` (yyyy-MM-dd) in the calendar of the date box labelled `label`. */ export function pickDate(label: string | RegExp | HTMLElement, iso: string) { fireEvent.click(label instanceof HTMLElement ? label : screen.getByLabelText(label)); clickDay(topPopover(), iso); } /** Picks the range `from`–`to` in the calendar of the range box labelled `fromLabel`. */ export function pickRange(fromLabel: string | RegExp, from: string, to: string) { fireEvent.click(screen.getByLabelText(fromLabel)); const pop = topPopover(); clickDay(pop, from); clickDay(pop, to); }