diff --git a/src/calendar/__tests__/Calendar.test.tsx b/src/calendar/__tests__/Calendar.test.tsx new file mode 100644 index 000000000..c75ed095c --- /dev/null +++ b/src/calendar/__tests__/Calendar.test.tsx @@ -0,0 +1,170 @@ +import * as React from "react"; +import { subWeeks, addWeeks } from "date-fns"; +import { axe, render, press } from "reakit-test-utils"; + +import { + CalendarCell, + CalendarGrid, + CalendarHeader, + CalendarButton, + useCalendarState, + CalendarWeekTitle, + CalendarCellButton, + CalendarStateInitialProps, + Calendar as CalendarWrapper, +} from "../index"; + +export const CalendarComp: React.FC = props => { + const state = useCalendarState(props); + + return ( + +
+ + previous year + + + previous month + + + + next month + + + next year + +
+ + + + + {state.weekDays.map((day, dayIndex) => { + return ( + + {day.abbr} + + ); + })} + + + + {state.daysInMonth.map((week: any[], weekIndex: React.Key) => ( + + {week.map((day: Date, dayIndex: React.Key) => ( + + + + ))} + + ))} + + +
+ ); +}; + +describe("Calendar", () => { + it("should render correctly", () => { + const { getByTestId: testId } = render( + , + ); + + expect(testId("weekDays").children).toHaveLength(7); + expect(testId("current-year")).toHaveTextContent("October 2020"); + }); + + it("should have proper calendar header keyboard navigation", () => { + const { getByTestId: testId, getByText: text } = render( + , + ); + + expect(testId("current-year")).toHaveTextContent("October 2020"); + press.Tab(); + press.Enter(); + expect(text("previous year")).toHaveFocus(); + expect(testId("current-year")).toHaveTextContent("October 2019"); + press.Tab(); + press.Enter(); + expect(text("previous month")).toHaveFocus(); + expect(testId("current-year")).toHaveTextContent("September 2019"); + press.Tab(); + press.Enter(); + expect(text("next month")).toHaveFocus(); + expect(testId("current-year")).toHaveTextContent("October 2019"); + press.Tab(); + press.Enter(); + expect(text("next year")).toHaveFocus(); + expect(testId("current-year")).toHaveTextContent("October 2020"); + }); + + it("should proper grid navigation", () => { + const { getByTestId: testId, getByLabelText: label } = render( + , + ); + + expect(testId("current-year")).toHaveTextContent("October 2020"); + press.Tab(); + press.Tab(); + press.Tab(); + press.Tab(); + press.Tab(); + + expect(label("Wednesday, October 7, 2020 selected")).toHaveFocus(); + + // Let's navigate to 30 + press.ArrowDown(); + press.ArrowDown(); + press.ArrowRight(); + press.ArrowRight(); + press.ArrowDown(); + + expect(label("Friday, October 30, 2020")).toHaveFocus(); + + // Let's go to next month + press.ArrowDown(); + expect(label("Friday, November 6, 2020")).toHaveFocus(); + expect(testId("current-year")).toHaveTextContent("November 2020"); + }); +}); + +test("should have min/max values", async () => { + const { getByLabelText: label } = render( + , + ); + + press.Tab(); + press.Tab(); + press.Tab(); + press.Tab(); + press.Tab(); + expect(label("Saturday, November 7, 2020 selected")).toHaveFocus(); + + // try to go outside the min max value + press.ArrowUp(); + press.ArrowUp(); + press.ArrowUp(); + press.ArrowUp(); + expect(label("Saturday, October 31, 2020")).toHaveFocus(); + + press.ArrowDown(); + press.ArrowDown(); + press.ArrowDown(); + expect(label("Saturday, November 14, 2020")).toHaveFocus(); +}); + +test("Calendar renders with no a11y violations", async () => { + const { container } = render(); + const results = await axe(container); + + expect(results).toHaveNoViolations(); +}); diff --git a/src/calendar/__tests__/RangeCalendar.test.tsx b/src/calendar/__tests__/RangeCalendar.test.tsx new file mode 100644 index 000000000..bdf2ccdab --- /dev/null +++ b/src/calendar/__tests__/RangeCalendar.test.tsx @@ -0,0 +1,170 @@ +import * as React from "react"; +import { axe, render, press } from "reakit-test-utils"; + +import { RangeCalendarProps } from "../index.d"; +import { + Calendar, + CalendarCell, + CalendarGrid, + CalendarHeader, + CalendarButton, + CalendarWeekTitle, + CalendarCellButton, + useRangeCalendarState, +} from "../index"; + +const RangeCalendarComp: React.FC = props => { + const state = useRangeCalendarState(props); + + return ( + console.log("change")} + className="calendar-range" + > +
+ + previous year + + + previous month + + + + next month + + + next year + +
+ + + + + {state.weekDays.map((day, dayIndex) => { + return ( + + {day.abbr} + + ); + })} + + + + {state.daysInMonth.map((week, weekIndex) => ( + + {week.map((day, dayIndex) => ( + + + + ))} + + ))} + + +
+ ); +}; + +describe("RangeCalendar", () => { + it("should render correctly", () => { + const { getByTestId: testId } = render( + , + ); + + expect(testId("week-days").children).toHaveLength(7); + expect(testId("current-year")).toHaveTextContent("October 2020"); + }); + + it("should have proper initial start and end ranges", () => { + const { getByLabelText: label, baseElement } = render( + , + ); + + const start = baseElement.querySelector("[data-is-selection-start]"); + const anyMiddleDate = label("Thursday, October 15, 2020"); + const end = baseElement.querySelector("[data-is-selection-end]"); + + expect(start).toHaveTextContent("7"); + expect(anyMiddleDate.parentElement).toHaveAttribute( + "data-is-range-selection", + ); + expect(end).toHaveTextContent("30"); + }); + + it("should be able to select ranges with keyboard navigation", () => { + const { getByLabelText: label, getByTestId: testId, baseElement } = render( + , + ); + + expect(testId("current-year")).toHaveTextContent("October 2020"); + press.Tab(); + press.Tab(); + press.Tab(); + press.Tab(); + press.Tab(); + + expect(label("Wednesday, October 7, 2020 selected")).toHaveFocus(); + press.ArrowDown(); // go to down just for some variety + + press.Enter(); // start the selection, currently the start and end should be the same date + expect( + baseElement.querySelector("[data-is-selection-start]"), + ).toHaveTextContent("14"); + expect( + baseElement.querySelector("[data-is-selection-end]"), + ).toHaveTextContent("14"); + + // Now we choose the end date, let's choose 19 + press.ArrowDown(); + press.ArrowDown(); + press.ArrowDown(); + press.ArrowLeft(); + press.ArrowLeft(); + expect( + label("Monday, November 2, 2020 (click to finish selecting range)"), + ).toHaveFocus(); + // finish the selection + press.Enter(); + + // check if the selection is actually finished or not + press.ArrowRight(); + press.ArrowRight(); + expect(label("Wednesday, November 4, 2020")).toHaveFocus(); + expect( + label("Wednesday, November 4, 2020")?.parentElement, + ).not.toHaveAttribute("data-is-range-selection"); + + // Verify selection ranges + const end = baseElement.querySelector("[data-is-selection-end]"); + expect(end).toHaveTextContent("2"); + + testId("prev-month").click(); + // We need to go to previous month to see/verify the start selection + const start = baseElement.querySelector("[data-is-selection-start]"); + expect(start).toHaveTextContent("14"); + }); +}); + +test("RangeCalendar renders with no a11y violations", async () => { + const { container } = render(); + const results = await axe(container); + + expect(results).toHaveNoViolations(); +});