-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathListView.test.tsx
243 lines (211 loc) · 7.37 KB
/
ListView.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
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
import {
expect,
describe,
it,
jest,
beforeAll,
afterAll,
afterEach,
} from '@jest/globals';
import { Button } from '@keystar/ui/button';
import {
act,
fireEvent,
firePress,
renderWithProvider,
within,
} from '#test-utils';
import { ListView, Item } from '..';
// eslint-disable-next-line jest/no-disabled-tests
describe.skip('list-view/ListView', () => {
let offsetWidth: jest.SpiedGetter<number>,
offsetHeight: jest.SpiedGetter<number>,
scrollHeight: jest.SpiedGetter<number>;
let items = [
{ key: 'foo', label: 'Foo' },
{ key: 'bar', label: 'Bar' },
{ key: 'baz', label: 'Baz' },
];
let manyItems: { id: number; label: string }[] = [];
for (let i = 1; i <= 100; i++) {
manyItems.push({ id: i, label: 'Foo ' + i });
}
beforeAll(function () {
offsetWidth = jest
.spyOn(window.HTMLElement.prototype, 'clientWidth', 'get')
.mockImplementation(() => 1000);
offsetHeight = jest
.spyOn(window.HTMLElement.prototype, 'clientHeight', 'get')
.mockImplementation(() => 1000);
scrollHeight = jest
.spyOn(window.HTMLElement.prototype, 'scrollHeight', 'get')
.mockImplementation(() => 40);
jest.useFakeTimers();
});
afterEach(function () {
fireEvent.keyDown(document.activeElement!, { key: 'Escape' });
fireEvent.keyUp(document.activeElement!, { key: 'Escape' });
jest.clearAllMocks();
});
afterAll(function () {
offsetWidth.mockReset();
offsetHeight.mockReset();
scrollHeight.mockReset();
});
let render = (children: any) => {
let tree = renderWithProvider(children);
// Allow for Virtualizer layout to update
act(() => {
jest.runAllTimers();
});
return tree;
};
let renderList = (props = {}) => {
return render(
<ListView items={items} aria-label="List" {...props}>
{item => <Item textValue={item.label}>{item.label}</Item>}
</ListView>
);
};
let renderListWithFocusables = (props = {}) => {
return render(
<ListView items={items} aria-label="List" {...props}>
{item => (
<Item textValue={item.label}>
{item.label}
<Button>button1 {item.label}</Button>
<Button>button2 {item.label}</Button>
</Item>
)}
</ListView>
);
};
let getRow = (tree: any, text: string) => {
// Find by text, then go up to the element with the row role.
let el = tree.getByText(text);
while (el && !/row/.test(el.getAttribute('role'))) {
el = el.parentElement;
}
return el;
};
it('renders a static listview', function () {
let { getByRole, getAllByRole } = render(
<ListView aria-label="List" data-testid="test">
<Item>Foo</Item>
<Item>Bar</Item>
<Item>Baz</Item>
</ListView>
);
let grid = getByRole('grid');
expect(grid).toBeVisible();
expect(grid).toHaveAttribute('aria-label', 'List');
expect(grid).toHaveAttribute('data-testid', 'test');
expect(grid).toHaveAttribute('aria-rowcount', '3');
expect(grid).toHaveAttribute('aria-colcount', '1');
let rows = getAllByRole('row');
expect(rows).toHaveLength(3);
expect(rows[0]).toHaveAttribute('aria-rowindex', '1');
expect(rows[1]).toHaveAttribute('aria-rowindex', '2');
expect(rows[2]).toHaveAttribute('aria-rowindex', '3');
let gridCells = within(rows[0]).getAllByRole('gridcell');
expect(gridCells).toHaveLength(1);
expect(gridCells[0]).toHaveTextContent('Foo');
expect(gridCells[0]).toHaveAttribute('aria-colindex', '1');
});
it('renders a dynamic listview', function () {
let items = [
{ key: 'foo', label: 'Foo' },
{ key: 'bar', label: 'Bar' },
{ key: 'baz', label: 'Baz' },
];
let { getByRole, getAllByRole } = render(
<ListView items={items} aria-label="List">
{item => <Item textValue={item.key}>{item.label}</Item>}
</ListView>
);
let grid = getByRole('grid');
expect(grid).toBeVisible();
expect(grid).toHaveAttribute('aria-label', 'List');
expect(grid).toHaveAttribute('aria-rowcount', '3');
expect(grid).toHaveAttribute('aria-colcount', '1');
let rows = getAllByRole('row');
expect(rows).toHaveLength(3);
expect(rows[0]).toHaveAttribute('aria-rowindex', '1');
expect(rows[1]).toHaveAttribute('aria-rowindex', '2');
expect(rows[2]).toHaveAttribute('aria-rowindex', '3');
let gridCells = within(rows[0]).getAllByRole('gridcell');
expect(gridCells).toHaveLength(1);
expect(gridCells[0]).toHaveTextContent('Foo');
expect(gridCells[0]).toHaveAttribute('aria-colindex', '1');
});
it('renders a falsy ids', function () {
let items = [
{ id: 0, label: 'Foo' },
{ id: 1, label: 'Bar' },
];
let { getByRole, getAllByRole } = render(
<ListView items={items} aria-label="List">
{item => <Item textValue={item.label}>{item.label}</Item>}
</ListView>
);
let grid = getByRole('grid');
expect(grid).toBeVisible();
let rows = getAllByRole('row');
expect(rows).toHaveLength(2);
let gridCells = within(rows[0]).getAllByRole('gridcell');
expect(gridCells).toHaveLength(1);
expect(gridCells[0]).toHaveTextContent('Foo');
});
it('should retain focus on the pressed child', function () {
let tree = renderListWithFocusables();
let button = within(getRow(tree, 'Foo')).getAllByRole('button')[1];
firePress(button);
expect(document.activeElement).toBe(button);
});
it('should focus the row if the cell is pressed', function () {
let tree = renderList({ selectionMode: 'single' });
let cell = within(getRow(tree, 'Bar')).getByRole('gridcell');
firePress(cell);
act(() => {
jest.runAllTimers();
});
expect(document.activeElement).toBe(getRow(tree, 'Bar'));
});
it('should have an aria-label on the row for the row text content', function () {
let tree = renderList();
expect(getRow(tree, 'Foo')).toHaveAttribute('aria-label', 'Foo');
expect(getRow(tree, 'Bar')).toHaveAttribute('aria-label', 'Bar');
expect(getRow(tree, 'Baz')).toHaveAttribute('aria-label', 'Baz');
});
it('should label the checkboxes with the row label', function () {
let tree = renderList({ selectionMode: 'single' });
let rows = tree.getAllByRole('row');
for (let row of rows) {
let checkbox = within(row).getByRole('checkbox');
expect(checkbox).toHaveAttribute(
'aria-labelledby',
`${checkbox.id} ${row.id}`
);
}
});
it('should disable nested elements when row is disabled', function () {
let tree = renderListWithFocusables({
disabledKeys: ['foo'],
selectionMode: 'multiple',
});
let row = getRow(tree, 'Foo');
expect(row).toHaveAttribute('aria-disabled', 'true');
expect(row).not.toHaveAttribute('aria-selected');
expect(within(row).getByRole('checkbox')).toHaveAttribute('disabled');
let buttons = within(row).getAllByRole('button');
expect(buttons[0]).toHaveAttribute('disabled');
expect(buttons[1]).toHaveAttribute('disabled');
row = getRow(tree, 'Bar');
expect(row).not.toHaveAttribute('aria-disabled', 'true');
expect(row).toHaveAttribute('aria-selected', 'false');
expect(within(row).getByRole('checkbox')).not.toHaveAttribute('disabled');
buttons = within(row).getAllByRole('button');
expect(buttons[0]).not.toHaveAttribute('disabled');
expect(buttons[1]).not.toHaveAttribute('disabled');
});
});