Skip to content

Commit

Permalink
fix(accessibility): update labels, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gpbl committed Jul 14, 2024
1 parent adea702 commit 3106ed5
Show file tree
Hide file tree
Showing 17 changed files with 70 additions and 27 deletions.
6 changes: 4 additions & 2 deletions examples/DropdownMultipleMonths.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from "react";

import { labelMonthDropdown } from "react-day-picker";

import { grid } from "@/test/elements";
import { screen, render } from "@/test/render";
import { user } from "@/test/user";
Expand All @@ -19,7 +21,7 @@ describe("when choosing a month from the first dropdown", () => {
const monthName = "January";
beforeEach(async () => {
const firstDropDown = screen.getAllByRole("combobox", {
name: "Month:"
name: labelMonthDropdown()
})[0];
await user.selectOptions(firstDropDown, monthName);
});
Expand All @@ -32,7 +34,7 @@ describe("when choosing a month from the third dropdown", () => {
const newMonthName = "October";
beforeEach(async () => {
const thirdDropDown = screen.getAllByRole("combobox", {
name: "Month:"
name: labelMonthDropdown()
})[2];
await user.selectOptions(thirdDropDown, newMonthName);
});
Expand Down
12 changes: 6 additions & 6 deletions examples/__snapshots__/Range.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ exports[`should match the snapshot 1`] = `
<nav
aria-label=""
class="rdp-nav"
role="toolbar"
role="navigation"
>
<button
aria-controls="test"
aria-label="Previous Month"
aria-label="Go to the Previous Month"
class="rdp-button_previous"
type="button"
>
Expand All @@ -35,7 +35,7 @@ exports[`should match the snapshot 1`] = `
</button>
<button
aria-controls="test"
aria-label="Next Month"
aria-label="Go to the Next Month"
class="rdp-button_next"
type="button"
>
Expand Down Expand Up @@ -656,11 +656,11 @@ exports[`when a day in the range is clicked when the day is clicked again when a
<nav
aria-label=""
class="rdp-nav"
role="toolbar"
role="navigation"
>
<button
aria-controls="test"
aria-label="Previous Month"
aria-label="Go to the Previous Month"
class="rdp-button_previous"
type="button"
>
Expand All @@ -677,7 +677,7 @@ exports[`when a day in the range is clicked when the day is clicked again when a
</button>
<button
aria-controls="test"
aria-label="Next Month"
aria-label="Go to the Next Month"
class="rdp-button_next"
type="button"
>
Expand Down
6 changes: 3 additions & 3 deletions examples/__snapshots__/StylingCssModules.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ exports[`should match the snapshot 1`] = `
<nav
aria-label=""
class="rdp-nav"
role="toolbar"
role="navigation"
>
<button
aria-controls=":r0:"
aria-label="Previous Month"
aria-label="Go to the Previous Month"
class="rdp-button_previous"
type="button"
>
Expand All @@ -36,7 +36,7 @@ exports[`should match the snapshot 1`] = `
</button>
<button
aria-controls=":r0:"
aria-label="Next Month"
aria-label="Go to the Next Month"
class="rdp-button_next"
type="button"
>
Expand Down
7 changes: 5 additions & 2 deletions src/DayPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ export function DayPicker(props: DayPickerProps) {

const dataAttributes = useMemo(() => getDataAttributes(props), [props]);

const contextValue = { ...calendar, ...selection, ...modifiers };
const contextValue = useMemo(
() => ({ ...calendar, ...selection, ...modifiers }),
[calendar, modifiers, selection]
);

return (
<dayPickerContext.Provider value={contextValue}>
Expand All @@ -305,7 +308,7 @@ export function DayPicker(props: DayPickerProps) {
>
{!props.hideNavigation && (
<components.Nav
role="toolbar"
role="navigation"
className={classNames[UI.Nav]}
style={styles?.[UI.Nav]}
aria-label={labelNav()}
Expand Down
17 changes: 17 additions & 0 deletions src/labels/labelDayButton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ const dayModifiers: Modifiers = {
selected: false,
today: false
};

describe("when the day is selected", () => {
test("return the label", () => {
expect(labelDayButton(day, { ...dayModifiers, selected: true })).toEqual(
"Monday, November 21st, 2022, selected"
);
});
});

describe("when the day is today", () => {
test("return the label", () => {
expect(labelDayButton(day, { ...dayModifiers, today: true })).toEqual(
"Today, Monday, November 21st, 2022"
);
});
});

test("should return the localized label", () => {
expect(labelDayButton(day, dayModifiers, { locale: es })).toEqual(
"lunes, 21 de noviembre de 2022"
Expand Down
7 changes: 7 additions & 0 deletions src/labels/labelGrid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { labelGrid } from "./labelGrid";

const day = new Date(2022, 10, 21);

test("return the label", () => {
expect(labelGrid(day)).toEqual("November 2022");
});
7 changes: 7 additions & 0 deletions src/labels/labelGridcell.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { labelGridcell } from "./labelGridcell";

const day = new Date(2022, 10, 21);

test("return the label", () => {
expect(labelGridcell(day)).toEqual("Monday, November 21st, 2022");
});
2 changes: 1 addition & 1 deletion src/labels/labelMonthDropdown.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { labelMonthDropdown } from "./labelMonthDropdown";

test("should return the label", () => {
expect(labelMonthDropdown({})).toEqual("Month: ");
expect(labelMonthDropdown({})).toEqual("Choose the Month");
});
2 changes: 1 addition & 1 deletion src/labels/labelMonthDropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import type { LabelOptions } from "../lib/dateLib.js";
* @see http://daypicker.dev/next/docs/translation#aria-labels
*/
export function labelMonthDropdown(options?: LabelOptions) {
return "Month: ";
return "Choose the Month";
}
5 changes: 5 additions & 0 deletions src/labels/labelNav.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { labelNav } from "./labelNav";

test("should return the label", () => {
expect(labelNav()).toEqual("");
});
2 changes: 1 addition & 1 deletion src/labels/labelNext.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { labelNext } from "./labelNext";

test("should return the label", () => {
expect(labelNext(new Date(), {})).toEqual("Next Month");
expect(labelNext(new Date(), {})).toEqual("Go to the Next Month");
});
2 changes: 1 addition & 1 deletion src/labels/labelNext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export function labelNext(
month: Date | undefined,
options?: LabelOptions
) {
return "Next Month";
return "Go to the Next Month";
}
2 changes: 1 addition & 1 deletion src/labels/labelPrevious.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { labelPrevious } from "./labelPrevious";

test("should return the label", () => {
expect(labelPrevious(new Date(), {})).toEqual("Previous Month");
expect(labelPrevious(new Date(), {})).toEqual("Go to the Previous Month");
});
2 changes: 1 addition & 1 deletion src/labels/labelPrevious.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export function labelPrevious(
month: Date | undefined,
options?: LabelOptions
) {
return "Previous Month";
return "Go to the Previous Month";
}
2 changes: 1 addition & 1 deletion src/labels/labelYearDropdown.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { labelYearDropdown } from "./labelYearDropdown";

test("should return the label", () => {
expect(labelYearDropdown()).toEqual("Year: ");
expect(labelYearDropdown()).toEqual("Choose the Year");
});
2 changes: 1 addition & 1 deletion src/labels/labelYearDropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import type { LabelOptions } from "../lib/dateLib.js";
* @see http://daypicker.dev/next/docs/translation#aria-labels
*/
export function labelYearDropdown(options?: LabelOptions) {
return "Year: ";
return "Choose the Year";
}
14 changes: 8 additions & 6 deletions test/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
DayFlag,
SelectionState,
labelDayButton,
labelGridcell
labelGridcell,
labelMonthDropdown,
labelYearDropdown
} from "react-day-picker";

/** Return the application element from the screen. */
Expand All @@ -14,14 +16,14 @@ export function app() {
/** Return the previous button element from the screen. */
export function previousButton() {
return screen.getByRole("button", {
name: "Previous Month"
name: "Go to the Previous Month"
});
}

/** Return the next button element from the screen. */
export function nextButton() {
return screen.getByRole("button", {
name: "Next Month"
name: "Go to the Next Month"
});
}

Expand All @@ -45,7 +47,7 @@ export function grid(name?: ByRoleOptions["name"]) {

/** Return the parent element of the next button from the screen. */
export function nav() {
return nextButton().parentElement;
return screen.getByRole("navigation");
}

/**
Expand Down Expand Up @@ -100,12 +102,12 @@ export function rowheader(name?: ByRoleOptions["name"]) {

/** Return the year dropdown element from the screen. */
export function yearDropdown() {
return screen.getByRole("combobox", { name: "Year:" });
return screen.getByRole("combobox", { name: labelYearDropdown() });
}

/** Return the month dropdown. */
export function monthDropdown() {
return screen.getByRole("combobox", { name: "Month:" });
return screen.getByRole("combobox", { name: labelMonthDropdown() });
}

/** Return the currently focused element. */
Expand Down

0 comments on commit 3106ed5

Please sign in to comment.