Skip to content

Commit

Permalink
chore(test): cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
gpbl committed Jun 22, 2024
1 parent 8b3bbcd commit a778883
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 73 deletions.
4 changes: 2 additions & 2 deletions examples/Controlled.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";

import { grid } from "@/test/elements";
import { act, render, screen } from "@/test/render";
import { render, screen } from "@/test/render";
import { user } from "@/test/user";

import { Controlled } from "./Controlled";
Expand All @@ -13,7 +13,7 @@ describe('when the "Today" button is clicked', () => {
const todayButton = () => screen.getByRole("button", { name: "Go to Today" });
beforeEach(async () => {
render(<Controlled />);
await act(() => user.click(todayButton()));
await user.click(todayButton());
});
test("the button should be disabled", async () => {
expect(todayButton()).toBeDisabled();
Expand Down
6 changes: 3 additions & 3 deletions examples/CustomSingle.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";

import { gridcell } from "@/test/elements";
import { act, render, screen } from "@/test/render";
import { render, screen } from "@/test/render";
import { user } from "@/test/user";

import { CustomSingle } from "./CustomSingle";
Expand All @@ -17,7 +17,7 @@ beforeEach(() => {

describe("when a day is clicked", () => {
beforeEach(async () => {
await act(() => user.click(gridcell(today)));
await user.click(gridcell(today));
});
test("should appear as selected", () => {
expect(gridcell(today)).toHaveAttribute("aria-selected", "true");
Expand All @@ -29,7 +29,7 @@ describe("when a day is clicked", () => {
});
describe("when clicking the day again", () => {
beforeEach(async () => {
await act(() => user.click(gridcell(today)));
await user.click(gridcell(today));
});
test("should not appear as selected", () => {
expect(gridcell(today)).not.toHaveAttribute("aria-selected", "true");
Expand Down
6 changes: 3 additions & 3 deletions examples/Disabled.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const firstOfMonth = new Date(2022, 5, 1);
beforeAll(() => jest.setSystemTime(today));
afterAll(() => jest.useRealTimers());

beforeEach(async () => {
beforeEach(() => {
render(
<div role="application">
<Disabled />
Expand All @@ -30,7 +30,7 @@ test("should not display the previous button", () => {
describe("when the first day is focused", () => {
describe("when the Arrow Left is pressed", () => {
beforeEach(async () => {
await act(() => user.type(app(), "{arrowleft}"));
await user.type(app(), "{arrowleft}");
});
test("should still display the same month", () => {
expect(grid("June 2022")).toBeInTheDocument();
Expand All @@ -40,7 +40,7 @@ describe("when the first day is focused", () => {
describe("when the last day is focused", () => {
describe("when the Arrow Right is pressed", () => {
beforeEach(async () => {
await act(() => user.type(app(), "{arrowleft}"));
await user.type(app(), "{arrowleft}");
});
test("should still display the same month", () => {
expect(grid("June 2022")).toBeInTheDocument();
Expand Down
16 changes: 8 additions & 8 deletions examples/FocusRecursive.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";

import { activeElement, gridcell } from "@/test/elements";
import { act, render } from "@/test/render";
import { render } from "@/test/render";
import { user } from "@/test/user";

import { FocusRecursive } from "./FocusRecursive";
Expand All @@ -13,12 +13,12 @@ afterAll(() => jest.useRealTimers());

test("the first selected day should have focus", async () => {
render(<FocusRecursive />).container;
await act(() => user.tab());
await act(() => user.tab());
await act(() => user.tab());
await act(() => user.type(activeElement(), "{arrowdown}"));
await act(() => user.type(activeElement(), "{arrowdown}"));
await act(() => user.type(activeElement(), "{arrowdown}"));
await act(() => user.type(activeElement(), "{arrowdown}"));
await user.tab();
await user.tab();
await user.tab();
await user.type(activeElement(), "{arrowdown}");
await user.type(activeElement(), "{arrowdown}");
await user.type(activeElement(), "{arrowdown}");
await user.type(activeElement(), "{arrowdown}");
expect(gridcell(new Date(2022, 5, 22))).toHaveFocus();
});
4 changes: 2 additions & 2 deletions examples/FromToMonth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from "react";
import { differenceInMonths } from "date-fns";

import { nextButton, previousButton } from "@/test/elements";
import { act, render } from "@/test/render";
import { render } from "@/test/render";
import { user } from "@/test/user";

import { FromToMonth } from "./FromToMonth";
Expand All @@ -26,7 +26,7 @@ describe("when navigating to the last month", () => {
const nOfMonths = differenceInMonths(toDate, fromDate);
beforeEach(async () => {
for (let i = 0; i < nOfMonths; i++) {
await act(() => user.click(nextButton()));
await user.click(nextButton());
}
});

Expand Down
4 changes: 2 additions & 2 deletions examples/FromToYear.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from "react";
import { differenceInMonths } from "date-fns";

import { nextButton, previousButton } from "@/test/elements";
import { act, render } from "@/test/render";
import { render } from "@/test/render";
import { user } from "@/test/user";

import { FromToYear } from "./FromToYear";
Expand All @@ -30,7 +30,7 @@ describe("when navigating to the last month", () => {
const nOfMonths = differenceInMonths(toMonth, fromMonth);
beforeEach(async () => {
for (let i = 0; i < nOfMonths; i++) {
await act(() => user.click(nextButton()));
await user.click(nextButton());
}
});
test("the previous month button should not be disabled", () => {
Expand Down
46 changes: 23 additions & 23 deletions examples/Keyboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
renderApp(<Keyboard mode="single" dir={dir} />);
});
describe("when clicking the previous month button", () => {
beforeEach(() => act(() => user.click(previousButton())));
beforeEach(() => user.click(previousButton()));
test("should display the previous month", () => {
expect(grid("May 2022")).toBeInTheDocument();
});
});
describe("when clicking the next month button", () => {
beforeEach(() => act(() => user.click(nextButton())));
beforeEach(() => user.click(nextButton()));

test("should display the next month", () => {
expect(grid("July 2022")).toBeInTheDocument();
Expand All @@ -64,7 +64,7 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
expect(activeElement()).toBe(gridcell(day));
});
describe("when the Arrow Left is pressed", () => {
beforeEach(() => act(() => user.type(activeElement(), "{arrowleft}")));
beforeEach(() => user.type(activeElement(), "{arrowleft}"));
if (dir === "rtl") {
test("should focus the next day", () => {
expect(gridcell(nextDay)).toHaveFocus();
Expand All @@ -79,7 +79,7 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
}
});
describe("when the Arrow Right is pressed", () => {
beforeEach(() => act(() => user.type(activeElement(), "{arrowright}")));
beforeEach(() => user.type(activeElement(), "{arrowright}"));
if (dir === "rtl") {
test("should display the previous month", () => {
expect(grid("May 2022")).toBeInTheDocument();
Expand All @@ -94,7 +94,7 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
}
});
describe("when the Arrow Up is pressed", () => {
beforeEach(() => act(() => user.type(activeElement(), "{arrowup}")));
beforeEach(() => user.type(activeElement(), "{arrowup}"));
test("should display the previous month", () => {
expect(grid("May 2022")).toBeInTheDocument();
});
Expand All @@ -103,7 +103,7 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
});
});
describe("when the Arrow Down is pressed", () => {
beforeEach(() => act(() => user.type(activeElement(), "{arrowdown}")));
beforeEach(() => user.type(activeElement(), "{arrowdown}"));
test("should display the same month", () => {
expect(grid("June 2022")).toBeInTheDocument();
});
Expand All @@ -113,7 +113,7 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
});
describe("when Page Up is pressed", () => {
beforeEach(() => {
return act(() => user.type(activeElement(), "{pageup}"));
return user.type(activeElement(), "{pageup}");
});
it("should display the previous month", () => {
expect(grid("May 2022")).toBeInTheDocument();
Expand All @@ -123,7 +123,7 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
});
});
describe("when Page Down is pressed", () => {
beforeEach(() => act(() => user.type(activeElement(), "{pagedown}")));
beforeEach(() => user.type(activeElement(), "{pagedown}"));
it("should display the next month", () => {
expect(grid("July 2022")).toBeInTheDocument();
});
Expand All @@ -132,9 +132,7 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
});
});
describe("when Shift + Page Up is pressed", () => {
beforeEach(() =>
act(() => user.type(activeElement(), "{shift>}{pageup}"))
);
beforeEach(() => user.type(activeElement(), "{shift>}{pageup}"));
it("should display the previous year", () => {
expect(grid("June 2021")).toBeInTheDocument();
});
Expand All @@ -143,9 +141,9 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
});
});
describe("when Shift + Page Down is pressed", () => {
beforeEach(() =>
act(() => user.type(activeElement(), "{shift>}{pagedown}"))
);
beforeEach(() => {
return user.type(activeElement(), "{shift>}{pagedown}");
});
it("should display the next year", () => {
expect(grid("June 2023")).toBeInTheDocument();
});
Expand All @@ -154,13 +152,13 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
});
});
describe("when Home is pressed", () => {
beforeEach(() => act(() => user.type(activeElement(), "{home}")));
beforeEach(() => user.type(activeElement(), "{home}"));
it("should focus the start of the week", () => {
expect(gridcell(startOfWeekDay)).toHaveFocus();
});
});
describe("when End is pressed", () => {
beforeEach(() => act(() => user.type(activeElement(), "{end}")));
beforeEach(() => user.type(activeElement(), "{end}"));
it("should focus the end of the week", () => {
expect(gridcell(endOfWeekDay)).toHaveFocus();
});
Expand All @@ -172,9 +170,11 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
const nextDay = addDays(day, 1);
const prevDay = addDays(day, -1);

beforeEach(() => act(() => gridcell(day).focus()));
beforeEach(() => {
return act(() => gridcell(day).focus());
});
describe("when the Arrow Right is pressed", () => {
beforeEach(() => act(() => user.type(activeElement(), "{arrowright}")));
beforeEach(() => user.type(activeElement(), "{arrowright}"));
if (dir === "rtl") {
test("should focus the previous day", () => {
expect(gridcell(prevDay)).toHaveFocus();
Expand All @@ -190,7 +190,7 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
}
});
describe("when the Arrow Left is pressed", () => {
beforeEach(() => act(() => user.type(activeElement(), "{arrowleft}")));
beforeEach(() => user.type(activeElement(), "{arrowleft}"));
if (dir === "rtl") {
test("should display the next month", () => {
expect(grid("July 2022")).toBeInTheDocument();
Expand All @@ -209,7 +209,7 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
}
});
describe("when the Arrow Up is pressed", () => {
beforeEach(() => act(() => user.type(activeElement(), "{arrowup}")));
beforeEach(() => user.type(activeElement(), "{arrowup}"));
test("should display the same month", () => {
expect(grid("June 2022")).toBeInTheDocument();
});
Expand All @@ -219,7 +219,7 @@ describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
});
});
describe("when the Arrow Down is pressed", () => {
beforeEach(() => act(() => user.type(activeElement(), "{arrowdown}")));
beforeEach(() => user.type(activeElement(), "{arrowdown}"));
test("should display the next month", () => {
expect(grid("July 2022")).toBeInTheDocument();
});
Expand All @@ -242,13 +242,13 @@ describe("when week is set to start on a Monday", () => {
});

describe("when Home is pressed", () => {
beforeEach(() => act(() => user.type(activeElement(), "{home}")));
beforeEach(() => user.type(activeElement(), "{home}"));
it("should focus the start of the week being Monday", () => {
expect(gridcell(startOfWeekDay)).toHaveFocus();
});
});
describe("when End is pressed", () => {
beforeEach(() => act(() => user.type(activeElement(), "{end}")));
beforeEach(() => user.type(activeElement(), "{end}"));
it("should focus the end of the week being Sunday", () => {
expect(gridcell(endOfWeekDay)).toHaveFocus();
});
Expand Down
8 changes: 4 additions & 4 deletions examples/Range.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from "react";
import { addDays } from "date-fns";

import { gridcell } from "@/test/elements";
import { act, render, screen } from "@/test/render";
import { render, screen } from "@/test/render";
import { user } from "@/test/user";

import { Range } from "./Range";
Expand All @@ -29,7 +29,7 @@ test.each(days)("%s should be selected", (day) => {

describe("when a day in the range is clicked", () => {
const day = days[2];
beforeEach(async () => act(() => user.click(gridcell(day))));
beforeEach(async () => user.click(gridcell(day)));
test.each([days[0], days[1], day])("%s should be selected", (day) => {
expect(gridcell(day)).toHaveAttribute("aria-selected", "true");
});
Expand All @@ -38,7 +38,7 @@ describe("when a day in the range is clicked", () => {
});
describe("when the day is clicked again", () => {
const day = days[2];
beforeEach(async () => act(() => user.click(gridcell(day))));
beforeEach(async () => user.click(gridcell(day)));
test("only one day should be selected", () => {
expect(getAllSelectedDays()).toHaveLength(1);
});
Expand All @@ -48,7 +48,7 @@ describe("when a day in the range is clicked", () => {

describe("when a day in the range is clicked again", () => {
const day = days[2];
beforeEach(async () => act(() => user.click(gridcell(day))));
beforeEach(async () => user.click(gridcell(day)));
test("no day should be selected", () => {
expect(getAllSelectedDays()).toHaveLength(0);
});
Expand Down
2 changes: 1 addition & 1 deletion examples/RangeMinMax.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from "react";
import { addDays } from "date-fns";

import { gridcell } from "@/test/elements";
import { act, render, screen } from "@/test/render";
import { render, screen } from "@/test/render";
import { user } from "@/test/user";

import { RangeMinMax } from "./RangeMinMax";
Expand Down
8 changes: 4 additions & 4 deletions examples/RangeShiftKey.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";

import { gridcell } from "@/test/elements";
import { act, render } from "@/test/render";
import { render } from "@/test/render";
import { user } from "@/test/user";

import { RangeShiftKey } from "./RangeShiftKey";
Expand All @@ -16,13 +16,13 @@ beforeEach(() => render(<RangeShiftKey />).container);
describe("when displaying November 2021", () => {
describe("when clicking on the 11th", () => {
const day1 = new Date(2021, 10, 11);
beforeEach(async () => act(() => user.click(gridcell(day1))));
beforeEach(async () => user.click(gridcell(day1)));
test("the 11th day should have aria-selected true", () => {
expect(gridcell(day1)).toHaveAttribute("aria-selected", "true");
});
describe("when clicking on the 13th", () => {
const day2 = new Date(2021, 10, 13);
beforeEach(async () => act(() => user.click(gridcell(day2))));
beforeEach(async () => user.click(gridcell(day2)));

test("the 11th day should still have aria-selected true", () => {
expect(gridcell(day1)).toHaveAttribute("aria-selected", "true");
Expand All @@ -35,7 +35,7 @@ describe("when displaying November 2021", () => {
const day2 = new Date(2021, 10, 13);
beforeEach(async () => {
user.keyboard("{Shift>}");
await act(() => user.click(gridcell(day2)));
await user.click(gridcell(day2));
});
test("the 13th day should have aria-selected true", () => {
expect(gridcell(day2)).toHaveAttribute("aria-selected", "true");
Expand Down
Loading

0 comments on commit a778883

Please sign in to comment.