1 |
|
2 | import React from "react";
|
3 |
|
4 | import { format } from "date-fns";
|
5 |
|
6 | import { render, screen } from "@/test/render";
|
7 | import { user } from "@/test/user";
|
8 |
|
9 | import { Input } from "./Input";
|
10 |
|
11 | function textbox() {
|
12 | return screen.getByRole("textbox", { name: "Date:" }) as HTMLInputElement;
|
13 | }
|
14 |
|
15 | function gridcells() {
|
16 | return screen.queryAllByRole("gridcell") as HTMLTableCellElement[];
|
17 | }
|
18 |
|
19 | function selectedCells() {
|
20 | return gridcells().filter((cell) => cell.hasAttribute("aria-selected"));
|
21 | }
|
22 |
|
23 | test("renders a textbox", () => {
|
24 | render(<Input />);
|
25 | expect(textbox()).toBeInTheDocument();
|
26 | });
|
27 |
|
28 | test("updates the calendar when a date is typed in", async () => {
|
29 | render(<Input />);
|
30 | const testDate = new Date(2022, 11, 31);
|
31 | await user.type(textbox(), format(testDate, "MM/dd/yyyy"));
|
32 | expect(
|
33 | screen.getByText(`Selected: ${testDate.toDateString()}`)
|
34 | ).toBeInTheDocument();
|
35 |
|
36 | expect(selectedCells()).toHaveLength(1);
|
37 | expect(selectedCells()[0]).toHaveTextContent(`${testDate.getDate()}`);
|
38 | });
|
39 |
|
40 | test("updates the input when a day is picked from the calendar", async () => {
|
41 | render(<Input />);
|
42 | const testDate = new Date(2022, 11, 31);
|
43 | await user.type(textbox(), format(testDate, "MM/dd/yyyy"));
|
44 |
|
45 | expect(
|
46 | screen.getByText(`Selected: ${testDate.toDateString()}`)
|
47 | ).toBeInTheDocument();
|
48 |
|
49 | expect(selectedCells()).toHaveLength(1);
|
50 | expect(selectedCells()[0]).toHaveTextContent(`${testDate.getDate()}`);
|
51 | });
|
52 |
|
53 | test("clears the selected days when an invalid date is entered", async () => {
|
54 | render(<Input />);
|
55 | await user.type(textbox(), "invalid date");
|
56 | expect(selectedCells()).toHaveLength(0);
|
57 | });
|
58 |
|
59 | test("updates the month when a date is typed in", async () => {
|
60 | render(<Input />);
|
61 | const testDate = new Date(2022, 11, 31);
|
62 | await user.type(textbox(), format(testDate, "MM/dd/yyyy"));
|
63 | expect(screen.getByText(`December 2022`)).toBeInTheDocument();
|
64 | });
|
65 |
|
66 | test("reset the selected date when the field is emptied", async () => {
|
67 | render(<Input />);
|
68 | const testDate = new Date(2022, 11, 31);
|
69 | await user.type(textbox(), format(testDate, "MM/dd/yyyy"));
|
70 | await user.clear(textbox());
|
71 | expect(selectedCells()).toHaveLength(0);
|
72 | });
|