File size: 3,154 Bytes
cf86710 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | import { type ByRoleOptions, screen } from "@testing-library/react";
import {
DayFlag,
labelDayButton,
labelGridcell,
labelMonthDropdown,
labelYearDropdown,
SelectionState,
} from "../src";
/** Return the application element from the screen. */
export function app() {
return screen.getByRole("application");
}
/** Return the previous button element from the screen. */
export function previousButton(
name: ByRoleOptions["name"] = "Go to the Previous Month",
) {
return screen.getByRole("button", {
name,
});
}
/** Return the next button element from the screen. */
export function nextButton(
name: ByRoleOptions["name"] = "Go to the Next Month",
) {
return screen.getByRole("button", {
name,
});
}
/**
* Return the columnheader element from the screen.
*
* @param {string} name - The name of the columnheader.
*/
export function columnheader(name?: ByRoleOptions["name"]) {
return screen.getByRole("columnheader", { name, hidden: true });
}
/**
* Return the grid element from the screen.
*
* @param {string} name - The name of the grid.
*/
export function grid(name?: ByRoleOptions["name"]) {
return screen.getByRole("grid", { name });
}
/** Return the parent element of the next button from the screen. */
export function nav() {
return screen.getByRole("navigation");
}
/**
* Return the button element with the date
*
* @param {Date} date - The date to match the button name.
*/
export function dateButton(date: Date) {
return screen.getByRole("button", {
name: new RegExp(
labelDayButton(date, {
[DayFlag.disabled]: false,
[DayFlag.hidden]: false,
[DayFlag.outside]: false,
[DayFlag.focused]: false,
[DayFlag.today]: false,
[SelectionState.range_end]: false,
[SelectionState.range_middle]: false,
[SelectionState.range_start]: false,
[SelectionState.selected]: false,
}),
"s",
),
});
}
/**
* Return the gridcell element from the screen.
*
* @param {Date} date - The date to match the gridcell name.
* @param {boolean} interactive - If the gridcell is interactive (e.g. in
* selection mode).
*/
export function gridcell(date: Date, interactive?: boolean) {
if (interactive)
return screen.getByRole("gridcell", {
name: date.getDate().toString(),
});
return screen.getByRole("gridcell", {
name: labelGridcell(date),
});
}
/**
* Return the rowheader element from the screen.
*
* @param {string} name - The name of the rowheader.
*/
export function rowheader(name?: ByRoleOptions["name"]) {
return screen.getByRole("rowheader", { name });
}
/** Return the year dropdown element from the screen. */
export function yearDropdown() {
return screen.getByRole("combobox", { name: labelYearDropdown() });
}
/** Return the month dropdown. */
export function monthDropdown() {
return screen.getByRole("combobox", { name: labelMonthDropdown() });
}
/** Return the currently focused element. */
export function activeElement() {
if (!document.activeElement)
throw new Error("Could not find any focused element");
return document.activeElement;
}
|