/
index.spec.tsx
333 lines (284 loc) · 11.7 KB
/
index.spec.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import { act, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { Button } from "@twilio-paste/button";
import * as React from "react";
import { DataGridCell, DataGridHeader, DataGridHeaderSort } from "../src";
import {
ColumnSpanDataGrid,
ComposableCellsDataGrid,
PaginatedDataGrid,
PlainDataGrid,
SortableColumnsDataGrid,
} from "../stories/index.stories";
jest.useFakeTimers();
const checkTagName = (el: Element, name: string): void => expect(el.tagName).toBe(name.toUpperCase());
describe("Data Grid", () => {
describe("Semantics", () => {
// eslint-disable-next-line jest/expect-expect
it("uses table elements in the DOM", () => {
const { getByTestId } = render(<PlainDataGrid />);
const dataGrid = getByTestId("data-grid");
checkTagName(dataGrid, "table");
const thead = dataGrid.children[0];
const theadTr = thead.children[0];
const theadTrTh = theadTr.children[0];
checkTagName(thead, "thead");
checkTagName(theadTr, "tr");
checkTagName(theadTrTh, "th");
const tbody = dataGrid.children[1];
const tbodyTr = tbody.children[0];
const tbodyTrTd = tbodyTr.children[0];
checkTagName(tbody, "tbody");
checkTagName(tbodyTr, "tr");
checkTagName(tbodyTrTd, "td");
});
it("has the correct aria label and role", () => {
const { getByTestId } = render(<PlainDataGrid />);
const dataGrid = getByTestId("data-grid");
expect(dataGrid.getAttribute("aria-label")).toBeDefined();
expect(dataGrid.getAttribute("role")).toBe("grid");
});
it("should render width, textAlign, and whiteSpace styles", (): void => {
render(<PlainDataGrid />);
const renderedTh = screen.getByTestId("data-grid-header");
const renderedTd = screen.getByTestId("data-grid-cell");
expect(renderedTh).toHaveStyleRule("text-align", "left");
expect(renderedTh).toHaveStyleRule("white-space", "normal");
expect(renderedTd).toHaveStyleRule("text-align", "left");
expect(renderedTd).toHaveStyleRule("white-space", "nowrap");
});
});
describe("Column Span", () => {
it("applies colSpan attribute as expected", () => {
const { getByTestId } = render(<ColumnSpanDataGrid />);
const th = getByTestId("data-grid-header");
expect(th).toHaveAttribute("colspan", "5");
});
});
describe("Composable Cells functionality", () => {
it("has proper keyboard navigation behavior", () => {
const { getByTestId } = render(<ComposableCellsDataGrid />);
const headerCell = getByTestId("header-1");
const firstRowFirstInputCell = getByTestId("input-0-0");
const firstInputCell = firstRowFirstInputCell?.parentElement?.parentElement;
if (firstInputCell == null) {
throw new Error("cannot find firstInputCell");
}
act(() => {
headerCell.focus();
});
userEvent.keyboard("{arrowright}");
expect(document.activeElement?.innerHTML).toBe("Last Name");
userEvent.keyboard("{arrowleft}");
expect(headerCell).toHaveFocus();
userEvent.keyboard("{arrowdown}");
expect(headerCell.getAttribute("tabindex")).toBe("-1");
expect(firstInputCell).toHaveFocus();
expect(firstInputCell.getAttribute("tabindex")).toBe("0");
userEvent.keyboard("{arrowup}");
expect(firstInputCell.getAttribute("tabindex")).toBe("-1");
expect(headerCell.getAttribute("tabindex")).toBe("0");
expect(headerCell).toHaveFocus();
});
it("toggles actionable mode with [enter] and [escape] keys", () => {
const { getByTestId } = render(<ComposableCellsDataGrid />);
const wrapper = getByTestId("data-grid");
const headerCell = getByTestId("header-1");
const firstRowFirstInputCell = getByTestId("input-0-0");
const firstInputCell = firstRowFirstInputCell?.parentElement?.parentElement;
if (firstInputCell == null) {
throw new Error("cannot find firstInputCell");
}
act(() => {
headerCell.focus();
});
expect(wrapper.getAttribute("data-actionable")).toBe("false");
userEvent.keyboard("{enter}");
expect(wrapper.getAttribute("data-actionable")).toBe("true");
userEvent.keyboard("{esc}");
expect(wrapper.getAttribute("data-actionable")).toBe("false");
// TEST: should enable Actionable mode when clicking into the DataGrid
expect(wrapper.getAttribute("data-actionable")).toBe("false");
userEvent.click(headerCell);
expect(wrapper.getAttribute("data-actionable")).toBe("true");
});
it("should correctly tab through focusable elements in actionable mode", () => {
const { getByTestId } = render(<ComposableCellsDataGrid />);
const headerCell = getByTestId("header-1");
const firstRowFirstInputCell = getByTestId("input-0-0");
const firstRowSecondInputCell = getByTestId("input-0-1");
const secondRowFirstInputCell = getByTestId("input-1-0");
const firstInputCell = firstRowFirstInputCell?.parentElement?.parentElement;
if (firstInputCell == null) {
throw new Error("cannot find firstInputCell");
}
act(() => {
headerCell.focus();
});
// Down to input cell
userEvent.keyboard("{arrowdown}");
// Swap to actionable
userEvent.keyboard("{enter}");
expect(firstRowFirstInputCell).toHaveFocus();
userEvent.tab();
expect(firstRowSecondInputCell).toHaveFocus();
userEvent.tab();
userEvent.tab();
userEvent.tab();
expect(secondRowFirstInputCell).toHaveFocus();
/*
* TEST: handles entering actionable mode from a cell without focusable children correctly
* I added this particular sequence because it was a reproducable bug in my manual tests
*/
act(() => {
headerCell.focus();
});
userEvent.keyboard("{enter}");
userEvent.keyboard("{arrowdown}");
expect(firstInputCell).toHaveFocus();
userEvent.keyboard("{enter}");
userEvent.tab();
expect(firstRowSecondInputCell).toHaveFocus();
});
it("has one tab stop in navigational mode and remembers the last focus", async () => {
const { getByTestId } = render(
<div>
<Button variant="primary" data-testid="before">
Before
</Button>
<ComposableCellsDataGrid />
<Button variant="primary" data-testid="after">
After
</Button>
</div>,
);
const beforeDataGridButton = getByTestId("before");
const headerCell = getByTestId("header-1");
const firstInputCell = getByTestId("input-0-0")?.parentElement?.parentElement;
const afterDataGridButton = getByTestId("after");
if (firstInputCell == null) {
throw new Error("cannot find firstInputCell");
}
act(() => {
jest.advanceTimersByTime(300);
});
// Focus the button before the DataGrid
beforeDataGridButton.focus();
expect(beforeDataGridButton).toHaveFocus();
// Tab into the DataGrid
userEvent.tab();
expect(headerCell).toHaveFocus();
expect(headerCell.getAttribute("tabIndex")).toBe("0");
// Down
userEvent.keyboard("{arrowdown}");
expect(headerCell.getAttribute("tabindex")).toBe("-1");
expect(firstInputCell).toHaveFocus();
expect(firstInputCell.getAttribute("tabindex")).toBe("0");
// Tab out of the DataGrid
userEvent.tab();
expect(afterDataGridButton).toHaveFocus();
// Return into the DataGrid
userEvent.tab({ shift: true });
expect(firstInputCell).toHaveFocus();
expect(firstInputCell.getAttribute("tabindex")).toBe("0");
});
it("should change the focus correctly when swapping to and from actionable mode", async () => {
const { getByTestId } = render(<ComposableCellsDataGrid />);
const headerCell = getByTestId("header-1");
const firstRowFirstInputCell = getByTestId("input-0-0");
// Focus doesnt change when no focusable children
act(() => {
headerCell.focus();
});
userEvent.keyboard("{enter}");
expect(headerCell).toHaveFocus();
// Down to input firstChild
userEvent.keyboard("{esc}");
userEvent.keyboard("{arrowdown}");
userEvent.keyboard("{enter}");
expect(firstRowFirstInputCell).toHaveFocus();
// Up back to cell
userEvent.keyboard("{esc}");
expect(firstRowFirstInputCell?.parentElement?.parentElement).toHaveFocus();
});
});
describe("Paginated data grid", () => {
it("returns the first focused cell to the first cell in the datatable after pagination", async () => {
const { getByTestId } = render(<PaginatedDataGrid />);
const firstThCell = getByTestId("first-cell");
const firstTdCell = getByTestId("cell-0-0");
// I added this particular sequence because it was a reproducable bug in my manual tests
act(() => {
jest.advanceTimersByTime(300);
firstThCell.focus();
});
expect(firstThCell).toHaveFocus();
userEvent.keyboard("{arrowdown}");
expect(firstTdCell).toHaveFocus();
// Swap to page 2
userEvent.tab();
userEvent.tab();
userEvent.tab();
userEvent.keyboard("{enter}");
act(() => {
jest.advanceTimersByTime(300);
});
// Bring the focus back to the DataGrid
userEvent.tab({ shift: true });
userEvent.tab({ shift: true });
userEvent.tab({ shift: true }); // 3rd one because left arrow appears
// The first cell is focused, rather than the last cell we were on
expect(firstThCell).toHaveFocus();
});
});
describe("Sorting", () => {
it("should correctly set aria-sort on TH when sorting is enabled", () => {
const { getByTestId } = render(<SortableColumnsDataGrid />);
const header = getByTestId("header");
const headerSort = getByTestId("header-sort");
expect(header.getAttribute("aria-sort")).toBe("ascending");
act(() => {
headerSort.click();
});
expect(header.getAttribute("aria-sort")).toBe("descending");
act(() => {
headerSort.click();
});
expect(header.getAttribute("aria-sort")).toBe("ascending");
});
});
});
describe("i18n", () => {
describe("DataGridHeaderSort", () => {
it("should have default ascending label", () => {
render(<DataGridHeaderSort direction="ascending" />);
const button = screen.getByRole("button", { name: "Sort ascending" });
expect(button).toBeDefined();
});
it("should have default descending label", () => {
render(<DataGridHeaderSort direction="descending" />);
const button = screen.getByRole("button", { name: "Sort descending" });
expect(button).toBeDefined();
});
it("should have default unsorted label", () => {
render(<DataGridHeaderSort direction="none" />);
const button = screen.getByRole("button", { name: "Unsorted" });
expect(button).toBeDefined();
});
it("should use i18nAscendingLabel for the ascending label", () => {
render(<DataGridHeaderSort direction="ascending" i18nAscendingLabel="Tri croissant" />);
const button = screen.getByRole("button", { name: "Tri croissant" });
expect(button).toBeDefined();
});
it("should use i18nDescendingLabel for the descending label", () => {
render(<DataGridHeaderSort direction="descending" i18nDescendingLabel="Tri décroissant" />);
const button = screen.getByRole("button", { name: "Tri décroissant" });
expect(button).toBeDefined();
});
it("should use i18nUnsortedLabel for the unsorted label", () => {
render(<DataGridHeaderSort direction="none" i18nUnsortedLabel="Non triés" />);
const button = screen.getByRole("button", { name: "Non triés" });
expect(button).toBeDefined();
});
});
});