UNPKG

2.14 kBTypeScriptView Raw
1import React from "react";
2
3import { addDays } from "date-fns";
4
5import { dateButton, gridcell } from "@/test/elements";
6import { render, screen } from "@/test/render";
7import { user } from "@/test/user";
8
9import { Range } from "./Range";
10
11const defaultMonth = new Date(2020, 5, 15);
12
13let container: HTMLElement;
14const days = [
15 defaultMonth,
16 addDays(defaultMonth, 1),
17 addDays(defaultMonth, 2),
18 addDays(defaultMonth, 3),
19 addDays(defaultMonth, 4)
20];
21
22beforeEach(() => (container = render(<Range />).container));
23
24test("should match the snapshot", () => {
25 expect(container).toMatchSnapshot();
26});
27test.each(days)("%s should be selected", (day) => {
28 expect(gridcell(day, true)).toHaveAttribute("aria-selected", "true");
29});
30
31describe("when a day in the range is clicked", () => {
32 const day = days[2];
33 beforeEach(async () => user.click(dateButton(day)));
34 test.each([days[0], days[1], day])("%s should be selected", (day) => {
35 expect(gridcell(day, true)).toHaveAttribute("aria-selected", "true");
36 });
37 test.each([days[3], days[4]])("%s should not be selected", (day) => {
38 expect(gridcell(day, true)).not.toHaveAttribute("aria-selected");
39 });
40 describe("when the day is clicked again", () => {
41 const day = days[2];
42 beforeEach(async () => user.click(dateButton(day)));
43 test("only one day should be selected", () => {
44 expect(getAllSelected()).toHaveLength(1);
45 });
46 test("only a day in the range should be selected", () => {
47 expect(gridcell(day, true)).toHaveAttribute("aria-selected", "true");
48 });
49
50 describe("when a day in the range is clicked again", () => {
51 const day = days[2];
52 beforeEach(async () => user.click(dateButton(day)));
53 test("no day should be selected", () => {
54 expect(getAllSelected()).toHaveLength(0);
55 });
56 test("should match the snapshot", () => {
57 expect(container).toMatchSnapshot();
58 });
59 });
60 });
61});
62
63function getAllSelected() {
64 const gridcells = screen.getAllByRole("gridcell");
65
66 return Array.from(gridcells).filter(
67 (gridcell) => gridcell.getAttribute("aria-selected") === "true"
68 );
69}