UNPKG

8.81 kBTypeScriptView Raw
1import React, { act } from "react";
2
3import {
4 addDays,
5 addMonths,
6 addWeeks,
7 addYears,
8 endOfWeek,
9 lastDayOfMonth,
10 setDate,
11 startOfWeek
12} from "date-fns";
13
14import {
15 grid,
16 nextButton,
17 dateButton,
18 activeElement,
19 previousButton
20} from "@/test/elements";
21import { render } from "@/test/render";
22import { user } from "@/test/user";
23
24import { Keyboard } from "./Keyboard";
25
26const today = new Date(2022, 5, 10);
27
28beforeAll(() => jest.setSystemTime(today));
29afterAll(() => jest.useRealTimers());
30
31describe.each(["ltr", "rtl"])("when text direction is %s", (dir: string) => {
32 beforeEach(() => {
33 render(<Keyboard mode="single" dir={dir} />);
34 });
35 describe("when clicking the previous month button", () => {
36 beforeEach(() => user.click(previousButton()));
37 test("should display the previous month", () => {
38 expect(grid("May 2022")).toBeInTheDocument();
39 });
40 });
41 describe("when clicking the next month button", () => {
42 beforeEach(() => user.click(nextButton()));
43
44 test("should display the next month", () => {
45 expect(grid("July 2022")).toBeInTheDocument();
46 });
47 });
48
49 describe("when the first day is focused", () => {
50 const day = setDate(today, 1);
51 const nextDay = addDays(day, 1);
52 const prevDay = addDays(day, -1);
53 const nextMonth = addMonths(day, 1);
54 const prevMonth = addMonths(day, -1);
55 const nextYear = addYears(day, 1);
56 const prevYear = addYears(day, -1);
57 const prevWeekDay = addWeeks(day, -1);
58 const nextWeekDay = addWeeks(day, 1);
59 const startOfWeekDay = startOfWeek(day);
60 const endOfWeekDay = endOfWeek(day);
61
62 beforeEach(() => act(() => dateButton(day).focus()));
63 test("the day button should be focused", () => {
64 expect(activeElement()).toBe(dateButton(day));
65 });
66 describe("when the Arrow Left is pressed", () => {
67 beforeEach(() => user.type(activeElement(), "{arrowleft}"));
68 if (dir === "rtl") {
69 test("should focus the next day", () => {
70 expect(dateButton(nextDay)).toHaveFocus();
71 });
72 } else {
73 test("should display the previous month", () => {
74 expect(grid("May 2022")).toBeInTheDocument();
75 });
76 test("should focus the previous day", () => {
77 expect(dateButton(prevDay)).toHaveFocus();
78 });
79 }
80 });
81 describe("when the Arrow Right is pressed", () => {
82 beforeEach(() => user.type(activeElement(), "{arrowright}"));
83 if (dir === "rtl") {
84 test("should display the previous month", () => {
85 expect(grid("May 2022")).toBeInTheDocument();
86 });
87 test("should focus the previous day", () => {
88 expect(dateButton(prevDay)).toHaveFocus();
89 });
90 } else {
91 test("should focus the next day", () => {
92 expect(dateButton(nextDay)).toHaveFocus();
93 });
94 }
95 });
96 describe("when the Arrow Up is pressed", () => {
97 beforeEach(() => user.type(activeElement(), "{arrowup}"));
98 test("should display the previous month", () => {
99 expect(grid("May 2022")).toBeInTheDocument();
100 });
101 test("should focus the day in the previous week", () => {
102 expect(dateButton(prevWeekDay)).toHaveFocus();
103 });
104 });
105 describe("when the Arrow Down is pressed", () => {
106 beforeEach(() => user.type(activeElement(), "{arrowdown}"));
107 test("should display the same month", () => {
108 expect(grid("June 2022")).toBeInTheDocument();
109 });
110 test("should focus the day in the next week", () => {
111 expect(dateButton(nextWeekDay)).toHaveFocus();
112 });
113 });
114 describe("when Page Up is pressed", () => {
115 beforeEach(() => {
116 return user.type(activeElement(), "{pageup}");
117 });
118 it("should display the previous month", () => {
119 expect(grid("May 2022")).toBeInTheDocument();
120 });
121 it("should focus the day in the previous month", () => {
122 expect(dateButton(prevMonth)).toHaveFocus();
123 });
124 });
125 describe("when Page Down is pressed", () => {
126 beforeEach(() => user.type(activeElement(), "{pagedown}"));
127 it("should display the next month", () => {
128 expect(grid("July 2022")).toBeInTheDocument();
129 });
130 it("should focus the day in the next month", () => {
131 expect(dateButton(nextMonth)).toHaveFocus();
132 });
133 });
134 describe("when Shift + Page Up is pressed", () => {
135 beforeEach(() => user.type(activeElement(), "{shift>}{pageup}"));
136 it("should display the previous year", () => {
137 expect(grid("June 2021")).toBeInTheDocument();
138 });
139 it("should focus the day in the previous year", () => {
140 expect(dateButton(prevYear)).toHaveFocus();
141 });
142 });
143 describe("when Shift + Page Down is pressed", () => {
144 beforeEach(() => {
145 return user.type(activeElement(), "{shift>}{pagedown}");
146 });
147 it("should display the next year", () => {
148 expect(grid("June 2023")).toBeInTheDocument();
149 });
150 it("should focus the day in the next yeaer", () => {
151 expect(dateButton(nextYear)).toHaveFocus();
152 });
153 });
154 describe("when Home is pressed", () => {
155 beforeEach(() => user.type(activeElement(), "{home}"));
156 it("should focus the start of the week", () => {
157 expect(dateButton(startOfWeekDay)).toHaveFocus();
158 });
159 });
160 describe("when End is pressed", () => {
161 beforeEach(() => user.type(activeElement(), "{end}"));
162 it("should focus the end of the week", () => {
163 expect(dateButton(endOfWeekDay)).toHaveFocus();
164 });
165 });
166 });
167
168 describe("when the last day is focused", () => {
169 const day = lastDayOfMonth(today);
170 const nextDay = addDays(day, 1);
171 const prevDay = addDays(day, -1);
172
173 beforeEach(() => {
174 return act(() => dateButton(day).focus());
175 });
176 describe("when the Arrow Right is pressed", () => {
177 beforeEach(() => user.type(activeElement(), "{arrowright}"));
178 if (dir === "rtl") {
179 test("should focus the previous day", () => {
180 expect(dateButton(prevDay)).toHaveFocus();
181 });
182 } else {
183 test("should display the next month", () => {
184 expect(grid("July 2022")).toBeInTheDocument();
185 });
186 test("should focus the next day", () => {
187 const nextDay = addDays(day, 1);
188 expect(dateButton(nextDay)).toHaveFocus();
189 });
190 }
191 });
192 describe("when the Arrow Left is pressed", () => {
193 beforeEach(() => user.type(activeElement(), "{arrowleft}"));
194 if (dir === "rtl") {
195 test("should display the next month", () => {
196 expect(grid("July 2022")).toBeInTheDocument();
197 });
198 test("should focus the next day", () => {
199 expect(dateButton(nextDay)).toHaveFocus();
200 });
201 } else {
202 test("should display the same month", () => {
203 expect(grid("June 2022")).toBeInTheDocument();
204 });
205 test("should focus the previous day", () => {
206 const prevDay = addDays(day, -1);
207 expect(dateButton(prevDay)).toHaveFocus();
208 });
209 }
210 });
211 describe("when the Arrow Up is pressed", () => {
212 beforeEach(() => user.type(activeElement(), "{arrowup}"));
213 test("should display the same month", () => {
214 expect(grid("June 2022")).toBeInTheDocument();
215 });
216 test("should focus the day in the previous week", () => {
217 const prevDay = addWeeks(day, -1);
218 expect(dateButton(prevDay)).toHaveFocus();
219 });
220 });
221 describe("when the Arrow Down is pressed", () => {
222 beforeEach(() => user.type(activeElement(), "{arrowdown}"));
223 test("should display the next month", () => {
224 expect(grid("July 2022")).toBeInTheDocument();
225 });
226 test("should focus the day in the next week", () => {
227 const nextDay = addWeeks(day, 1);
228 expect(dateButton(nextDay)).toHaveFocus();
229 });
230 });
231 });
232});
233
234describe("when week is set to start on a Monday", () => {
235 const day = setDate(today, 10);
236 const startOfWeekDay = startOfWeek(day, { weekStartsOn: 1 });
237 const endOfWeekDay = endOfWeek(day, { weekStartsOn: 1 });
238
239 beforeEach(() => {
240 render(<Keyboard mode="single" weekStartsOn={1} />);
241 act(() => dateButton(day).focus());
242 });
243
244 describe("when Home is pressed", () => {
245 beforeEach(() => user.type(activeElement(), "{home}"));
246 it("should focus the start of the week being Monday", () => {
247 expect(dateButton(startOfWeekDay)).toHaveFocus();
248 });
249 });
250 describe("when End is pressed", () => {
251 beforeEach(() => user.type(activeElement(), "{end}"));
252 it("should focus the end of the week being Sunday", () => {
253 expect(dateButton(endOfWeekDay)).toHaveFocus();
254 });
255 });
256});