UNPKG

1.75 kBTypeScriptView Raw
1import React from "react";
2
3import { grid, monthDropdown, yearDropdown } from "@/test/elements";
4import { render, within } from "@/test/render";
5import { user } from "@/test/user";
6
7import { Dropdown } from "./Dropdown";
8
9const today = new Date(2015, 6, 1);
10
11beforeAll(() => jest.setSystemTime(today));
12afterAll(() => jest.useRealTimers());
13
14beforeEach(() => {
15 render(<Dropdown />);
16});
17
18test("should display the month dropdown", () => {
19 expect(monthDropdown()).toBeInTheDocument();
20});
21
22test("should display the year dropdown", () => {
23 expect(yearDropdown()).toBeInTheDocument();
24});
25
26test("should disable the months out of range", () => {
27 expect(
28 within(monthDropdown()).getByRole("option", { name: "January" })
29 ).toBeDisabled();
30});
31
32describe("when choosing a month", () => {
33 const monthName = "December";
34 beforeEach(async () => {
35 await user.selectOptions(monthDropdown(), monthName);
36 });
37 test("should display the month", () => {
38 expect(grid()).toHaveAccessibleName(`${monthName} 2015`);
39 });
40 test("should disable the years out of range", () => {
41 expect(
42 within(yearDropdown()).getByRole("option", { name: "2025" })
43 ).toBeDisabled();
44 });
45});
46
47describe("when choosing a year", () => {
48 const year = "2021";
49 beforeEach(async () => {
50 await user.selectOptions(yearDropdown(), year);
51 });
52 test("should display the year", () => {
53 expect(grid()).toHaveAccessibleName(`July ${year}`);
54 });
55});
56
57describe("when choosing a disabled month", () => {
58 const monthName = "February";
59 beforeEach(async () => {
60 await user.selectOptions(monthDropdown(), monthName);
61 });
62 test("should display the first available month", () => {
63 expect(grid()).toHaveAccessibleName(`July 2015`);
64 });
65});