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