-
-
Notifications
You must be signed in to change notification settings - Fork 703
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: fixes for input fields examples (#2149)
- Loading branch information
Showing
4 changed files
with
198 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
import { userEvent } from "@testing-library/user-event"; | ||
import { format } from "date-fns"; | ||
|
||
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")); | ||
} | ||
|
||
it("renders a textbox", () => { | ||
render(<Input />); | ||
expect(textbox()).toBeInTheDocument(); | ||
}); | ||
|
||
it("updates the calendar when a date is typed in", async () => { | ||
render(<Input />); | ||
const testDate = new Date(2022, 11, 31); // Dec 31, 2022 | ||
await userEvent.type(textbox(), format(testDate, "MM/dd/yyyy")); | ||
|
||
expect( | ||
screen.getByText(`Selected: ${testDate.toDateString()}`) | ||
).toBeInTheDocument(); | ||
|
||
expect(selectedCells()).toHaveLength(1); | ||
expect(selectedCells()[0]).toHaveTextContent(`${testDate.getDate()}`); | ||
}); | ||
|
||
it("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 userEvent.type(textbox(), format(testDate, "MM/dd/yyyy")); | ||
|
||
expect( | ||
screen.getByText(`Selected: ${testDate.toDateString()}`) | ||
).toBeInTheDocument(); | ||
|
||
expect(selectedCells()).toHaveLength(1); | ||
expect(selectedCells()[0]).toHaveTextContent(`${testDate.getDate()}`); | ||
}); | ||
|
||
it("clears the selected days when an invalid date is entered", async () => { | ||
render(<Input />); | ||
await userEvent.type(textbox(), "invalid date"); | ||
expect(selectedCells()).toHaveLength(0); | ||
}); | ||
|
||
it("updates the month when a date is typed in", async () => { | ||
render(<Input />); | ||
const testDate = new Date(2022, 11, 31); // Dec 31, 2022 | ||
await userEvent.type(textbox(), format(testDate, "MM/dd/yyyy")); | ||
expect(screen.getByText(`December 2022`)).toBeInTheDocument(); | ||
}); |
Oops, something went wrong.