UNPKG

2.26 kBTypeScriptView Raw
1/* eslint-disable jest/no-disabled-tests */
2import React from "react";
3
4import { format } from "date-fns";
5
6import { render, screen } from "@/test/render";
7import { user } from "@/test/user";
8
9import { Input } from "./Input";
10
11function textbox() {
12 return screen.getByRole("textbox", { name: "Date:" }) as HTMLInputElement;
13}
14
15function gridcells() {
16 return screen.queryAllByRole("gridcell") as HTMLTableCellElement[];
17}
18
19function selectedCells() {
20 return gridcells().filter((cell) => cell.hasAttribute("aria-selected"));
21}
22
23test("renders a textbox", () => {
24 render(<Input />);
25 expect(textbox()).toBeInTheDocument();
26});
27
28test("updates the calendar when a date is typed in", async () => {
29 render(<Input />);
30 const testDate = new Date(2022, 11, 31); // Dec 31, 2022
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
40test("updates the input when a day is picked from the calendar", async () => {
41 render(<Input />);
42 const testDate = new Date(2022, 11, 31); // Dec 31, 2022
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
53test("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
59test("updates the month when a date is typed in", async () => {
60 render(<Input />);
61 const testDate = new Date(2022, 11, 31); // Dec 31, 2022
62 await user.type(textbox(), format(testDate, "MM/dd/yyyy"));
63 expect(screen.getByText(`December 2022`)).toBeInTheDocument();
64});
65
66test("reset the selected date when the field is emptied", async () => {
67 render(<Input />);
68 const testDate = new Date(2022, 11, 31); // Dec 31, 2022
69 await user.type(textbox(), format(testDate, "MM/dd/yyyy"));
70 await user.clear(textbox());
71 expect(selectedCells()).toHaveLength(0);
72});