-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathArrowKeyStepper.jest.js
269 lines (241 loc) · 8 KB
/
ArrowKeyStepper.jest.js
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
import * as React from 'react';
import {findDOMNode} from 'react-dom';
import {render} from '../TestUtils';
import ArrowKeyStepper from './ArrowKeyStepper';
import {Simulate} from 'react-dom/test-utils';
function renderTextContent(scrollToColumn, scrollToRow) {
return `scrollToColumn:${scrollToColumn}, scrollToRow:${scrollToRow}`;
}
function ChildComponent({scrollToColumn, scrollToRow}) {
return <div>{renderTextContent(scrollToColumn, scrollToRow)}</div>;
}
describe('ArrowKeyStepper', () => {
function renderHelper(props = {}) {
let onSectionRenderedCallback;
const component = render(
<ArrowKeyStepper columnCount={10} mode="edges" rowCount={10} {...props}>
{({onSectionRendered, scrollToColumn, scrollToRow}) => {
onSectionRenderedCallback = onSectionRendered;
return (
<ChildComponent
scrollToColumn={scrollToColumn}
scrollToRow={scrollToRow}
/>
);
}}
</ArrowKeyStepper>,
);
const node = findDOMNode(component);
return {
component,
node,
onSectionRendered: onSectionRenderedCallback,
};
}
function assertCurrentScrollTo(node, scrollToColumn, scrollToRow) {
expect(node.textContent).toEqual(
renderTextContent(scrollToColumn, scrollToRow),
);
}
it('should use a custom :className if one is specified', () => {
const {node} = renderHelper({className: 'foo'});
expect(node.className).toEqual('foo');
});
it('should update :scrollToColumn and :scrollToRow in response to arrow keys', () => {
const {node} = renderHelper();
assertCurrentScrollTo(node, 0, 0);
Simulate.keyDown(node, {key: 'ArrowDown'});
assertCurrentScrollTo(node, 0, 1);
Simulate.keyDown(node, {key: 'ArrowRight'});
assertCurrentScrollTo(node, 1, 1);
Simulate.keyDown(node, {key: 'ArrowUp'});
assertCurrentScrollTo(node, 1, 0);
Simulate.keyDown(node, {key: 'ArrowLeft'});
assertCurrentScrollTo(node, 0, 0);
});
it('should not scroll past the row and column boundaries provided', () => {
const {node} = renderHelper({
columnCount: 2,
rowCount: 2,
});
Simulate.keyDown(node, {key: 'ArrowDown'});
Simulate.keyDown(node, {key: 'ArrowDown'});
Simulate.keyDown(node, {key: 'ArrowDown'});
assertCurrentScrollTo(node, 0, 1);
Simulate.keyDown(node, {key: 'ArrowUp'});
Simulate.keyDown(node, {key: 'ArrowUp'});
Simulate.keyDown(node, {key: 'ArrowUp'});
assertCurrentScrollTo(node, 0, 0);
Simulate.keyDown(node, {key: 'ArrowRight'});
Simulate.keyDown(node, {key: 'ArrowRight'});
Simulate.keyDown(node, {key: 'ArrowRight'});
assertCurrentScrollTo(node, 1, 0);
Simulate.keyDown(node, {key: 'ArrowLeft'});
Simulate.keyDown(node, {key: 'ArrowLeft'});
Simulate.keyDown(node, {key: 'ArrowLeft'});
assertCurrentScrollTo(node, 0, 0);
});
it('should accept initial :scrollToColumn and :scrollToRow values via props', () => {
const {node} = renderHelper({
mode: 'cells',
scrollToColumn: 2,
scrollToRow: 4,
});
assertCurrentScrollTo(node, 2, 4);
Simulate.keyDown(node, {key: 'ArrowDown'});
assertCurrentScrollTo(node, 2, 5);
Simulate.keyDown(node, {key: 'ArrowRight'});
assertCurrentScrollTo(node, 3, 5);
});
it('should accept updated :scrollToColumn and :scrollToRow values via props', () => {
const {node} = renderHelper({
mode: 'cells',
scrollToColumn: 2,
scrollToRow: 4,
});
Simulate.keyDown(node, {key: 'ArrowDown'});
assertCurrentScrollTo(node, 2, 5);
renderHelper({
mode: 'cells',
scrollToColumn: 1,
scrollToRow: 1,
});
Simulate.keyDown(node, {key: 'ArrowRight'});
assertCurrentScrollTo(node, 2, 1);
Simulate.keyDown(node, {key: 'ArrowDown'});
assertCurrentScrollTo(node, 2, 2);
});
it('should accept updated :scrollToColumn and :scrollToRow values via setScrollIndexes()', () => {
const {component, node} = renderHelper({
mode: 'cells',
scrollToColumn: 2,
scrollToRow: 4,
});
Simulate.keyDown(node, {key: 'ArrowDown'});
assertCurrentScrollTo(node, 2, 5);
component.setScrollIndexes({
scrollToColumn: 1,
scrollToRow: 1,
});
Simulate.keyDown(node, {key: 'ArrowRight'});
assertCurrentScrollTo(node, 2, 1);
Simulate.keyDown(node, {key: 'ArrowDown'});
assertCurrentScrollTo(node, 2, 2);
});
it('should not update :scrollToColumn or :scrollToRow when :disabled', () => {
const {node} = renderHelper({
disabled: true,
});
assertCurrentScrollTo(node, 0, 0);
Simulate.keyDown(node, {key: 'ArrowDown'});
assertCurrentScrollTo(node, 0, 0);
Simulate.keyDown(node, {key: 'ArrowRight'});
assertCurrentScrollTo(node, 0, 0);
});
it('should call :onScrollToChange for key down', () => {
[true, false].forEach(() => {
const onScrollToChange = jest.fn();
const {node} = renderHelper({
isControlled: true,
onScrollToChange,
});
expect(onScrollToChange.mock.calls).toHaveLength(0);
Simulate.keyDown(node, {key: 'ArrowDown'});
expect(onScrollToChange.mock.calls).toHaveLength(1);
const {scrollToColumn, scrollToRow} = onScrollToChange.mock.calls[0][0];
expect(scrollToColumn).toEqual(0);
expect(scrollToRow).toEqual(1);
});
});
it('should not call :onScrollToChange for prop update', () => {
let numCalls = 0;
const onScrollToChange = () => {
numCalls++;
};
const {node} = renderHelper({
onScrollToChange,
scrollToColumn: 0,
scrollToRow: 0,
});
renderHelper({
isControlled: true,
onScrollToChange,
node,
scrollToColumn: 0,
scrollToRow: 1,
});
expect(numCalls).toEqual(0);
});
describe('mode === "edges"', () => {
it('should update :scrollToColumn and :scrollToRow relative to the most recent :onSectionRendered event', () => {
const {node, onSectionRendered} = renderHelper();
onSectionRendered({
// Simulate a scroll
columnStartIndex: 0,
columnStopIndex: 4,
rowStartIndex: 4,
rowStopIndex: 6,
});
Simulate.keyDown(node, {key: 'ArrowDown'});
assertCurrentScrollTo(node, 0, 7);
onSectionRendered({
// Simulate a scroll
columnStartIndex: 5,
columnStopIndex: 10,
rowStartIndex: 2,
rowStopIndex: 4,
});
Simulate.keyDown(node, {key: 'ArrowUp'});
assertCurrentScrollTo(node, 0, 1);
onSectionRendered({
// Simulate a scroll
columnStartIndex: 4,
columnStopIndex: 8,
rowStartIndex: 5,
rowStopIndex: 10,
});
Simulate.keyDown(node, {key: 'ArrowRight'});
assertCurrentScrollTo(node, 9, 1);
onSectionRendered({
// Simulate a scroll
columnStartIndex: 2,
columnStopIndex: 4,
rowStartIndex: 2,
rowStopIndex: 4,
});
Simulate.keyDown(node, {key: 'ArrowLeft'});
assertCurrentScrollTo(node, 1, 1);
});
});
describe('mode === "cells"', () => {
it('should update :scrollToColumn and :scrollToRow relative to the most recent :onSectionRendered event', () => {
const {node, onSectionRendered} = renderHelper({
mode: 'cells',
scrollToColumn: 5,
scrollToRow: 5,
});
onSectionRendered({
// Simulate a scroll
columnStartIndex: 10,
columnStopIndex: 10,
rowStartIndex: 15,
rowStopIndex: 15,
});
Simulate.keyDown(node, {key: 'ArrowUp'});
assertCurrentScrollTo(node, 5, 4);
Simulate.keyDown(node, {key: 'ArrowDown'});
assertCurrentScrollTo(node, 5, 5);
onSectionRendered({
// Simulate a scroll
columnStartIndex: 10,
columnStopIndex: 10,
rowStartIndex: 15,
rowStopIndex: 15,
});
Simulate.keyDown(node, {key: 'ArrowRight'});
assertCurrentScrollTo(node, 6, 5);
Simulate.keyDown(node, {key: 'ArrowLeft'});
assertCurrentScrollTo(node, 5, 5);
});
});
});