-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectBox.test.tsx
136 lines (101 loc) · 4.34 KB
/
SelectBox.test.tsx
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { render, screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, test, vi } from "vitest";
import { SelectBox } from "./SelectBox";
describe("SelectBox", () => {
test("should focus move to right cell with right key", async () => {
render(<SelectBox onClick={() => undefined} />);
const cells = screen.getAllByRole("gridcell");
expect(cells.length).toBe(4);
const firstCellBtn = within(cells[0]).getByRole("button");
expect(firstCellBtn).toHaveFocus();
const secondCellBtn = within(cells[1]).getByRole("button");
expect(secondCellBtn).not.toHaveFocus();
await userEvent.keyboard("{ArrowRight}");
expect(secondCellBtn).toHaveFocus();
expect(screen.getByRole("grid")).toHaveAttribute(
"aria-activedescendant",
"selectBoxCell_1",
);
});
test("should focus move to last cell with left key when first cell focused", async () => {
render(<SelectBox onClick={() => undefined} />);
const cells = screen.getAllByRole("gridcell");
const firstCellBtn = within(cells[0]).getByRole("button");
expect(firstCellBtn).toHaveFocus();
const lastCellBtn = within(cells[3]).getByRole("button");
expect(lastCellBtn).not.toHaveFocus();
await userEvent.keyboard("{ArrowLeft}");
expect(lastCellBtn).toHaveFocus();
expect(screen.getByRole("grid")).toHaveAttribute(
"aria-activedescendant",
"selectBoxCell_3",
);
});
test("should focus move to first cell with right key when last cell focused", async () => {
render(<SelectBox onClick={() => undefined} defaultValue={3} />);
const cells = screen.getAllByRole("gridcell");
const firstCellBtn = within(cells[0]).getByRole("button");
expect(firstCellBtn).not.toHaveFocus();
const lastCellBtn = within(cells[3]).getByRole("button");
expect(lastCellBtn).toHaveFocus();
await userEvent.keyboard("{ArrowRight}");
expect(firstCellBtn).toHaveFocus();
expect(screen.getByRole("grid")).toHaveAttribute(
"aria-activedescendant",
"selectBoxCell_0",
);
});
test("should focus move to last cell with right key when right key press 3times", async () => {
render(<SelectBox onClick={() => undefined} />);
const cells = screen.getAllByRole("gridcell");
const firstCellBtn = within(cells[0]).getByRole("button");
expect(firstCellBtn).toHaveFocus();
const lastCellBtn = within(cells[3]).getByRole("button");
expect(lastCellBtn).not.toHaveFocus();
await userEvent.keyboard("{ArrowRight>3/}");
expect(lastCellBtn).toHaveFocus();
expect(screen.getByRole("grid")).toHaveAttribute(
"aria-activedescendant",
"selectBoxCell_3",
);
});
test("should focus move to first cell with left key when left key press 3times", async () => {
render(<SelectBox onClick={() => undefined} defaultValue={3} />);
const cells = screen.getAllByRole("gridcell");
const firstCellBtn = within(cells[0]).getByRole("button");
expect(firstCellBtn).not.toHaveFocus();
const lastCellBtn = within(cells[3]).getByRole("button");
expect(lastCellBtn).toHaveFocus();
await userEvent.keyboard("{ArrowLeft>3/}");
expect(firstCellBtn).toHaveFocus();
expect(screen.getByRole("grid")).toHaveAttribute(
"aria-activedescendant",
"selectBoxCell_0",
);
});
test("should focus move to first cell with left key when left key press 3times", async () => {
render(<SelectBox onClick={() => undefined} defaultValue={3} />);
const cells = screen.getAllByRole("gridcell");
const firstCellBtn = within(cells[0]).getByRole("button");
expect(firstCellBtn).not.toHaveFocus();
const lastCellBtn = within(cells[3]).getByRole("button");
expect(lastCellBtn).toHaveFocus();
await userEvent.keyboard("{ArrowLeft>3/}");
expect(firstCellBtn).toHaveFocus();
expect(screen.getByRole("grid")).toHaveAttribute(
"aria-activedescendant",
"selectBoxCell_0",
);
});
test.each([
{ key: "{Enter}", keyName: "Enter" },
{ key: " ", keyName: "Space" },
])("should call onClick props when $keyName key press", async ({ key }) => {
const onClickMock = vi.fn();
render(<SelectBox onClick={onClickMock} />);
await userEvent.keyboard("{ArrowRight>3/}");
await userEvent.keyboard(key);
expect(onClickMock).toHaveBeenCalledWith(3);
});
});