forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.test.js
More file actions
566 lines (515 loc) · 20.5 KB
/
Grid.test.js
File metadata and controls
566 lines (515 loc) · 20.5 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import React from 'react'
import { findDOMNode } from 'react-dom'
import { render } from '../TestUtils'
import { Simulate } from 'react-addons-test-utils'
import Grid from './Grid'
const NUM_ROWS = 100
const NUM_COLUMNS = 50
describe('Grid', () => {
function getMarkup ({
className,
columnsCount = NUM_COLUMNS,
columnWidth = 50,
height = 100,
noContentRenderer,
onSectionRendered,
onScroll,
overscanColumnsCount = 0,
overscanRowsCount = 0,
renderCell,
rowHeight = 20,
rowsCount = NUM_ROWS,
scrollLeft = undefined,
scrollToColumn,
scrollToRow,
scrollTop = undefined,
width = 200
} = {}) {
function defaultRenderCell ({ columnIndex, rowIndex }) {
return (
<div className='gridItem'>
{`row:${rowIndex}, column:${columnIndex}`}
</div>
)
}
return (
<Grid
className={className}
columnsCount={columnsCount}
columnWidth={columnWidth}
height={height}
noContentRenderer={noContentRenderer}
onSectionRendered={onSectionRendered}
onScroll={onScroll}
overscanColumnsCount={overscanColumnsCount}
overscanRowsCount={overscanRowsCount}
renderCell={renderCell || defaultRenderCell}
rowHeight={rowHeight}
rowsCount={rowsCount}
scrollLeft={scrollLeft}
scrollToColumn={scrollToColumn}
scrollToRow={scrollToRow}
scrollTop={scrollTop}
width={width}
/>
)
}
describe('number of rendered children', () => {
it('should render enough children to fill the available area', () => {
const rendered = findDOMNode(render(getMarkup()))
expect(rendered.querySelectorAll('.gridItem').length).toEqual(20) // 5 rows x 4 columns
})
it('should not render more rows than available if the area is not filled', () => {
const rendered = findDOMNode(render(getMarkup({ rowsCount: 2 })))
expect(rendered.querySelectorAll('.gridItem').length).toEqual(8) // 2 rows x 4 columns
})
it('should not render more columns than available if the area is not filled', () => {
const rendered = findDOMNode(render(getMarkup({ columnsCount: 2 })))
expect(rendered.querySelectorAll('.gridItem').length).toEqual(10) // 5 rows x 2 columns
})
// Small performance tweak added in 5.5.6
it('should not render/parent cells that are null or false', () => {
function renderCell ({ columnIndex, rowIndex }) {
if (columnIndex === 0) {
return null
} else if (rowIndex === 0) {
return false
} else {
return `row:${rowIndex}, column:${columnIndex}`
}
}
const rendered = findDOMNode(render(getMarkup({
columnsCount: 3,
overscanColumnsCount: 0,
overscanRowsCount: 0,
rowsCount: 3,
renderCell
})))
expect(rendered.querySelectorAll('.Grid__cell').length).toEqual(4) // [1,1], [1,2], [2,1], and [2,2]
expect(rendered.textContent).not.toContain('column:0')
expect(rendered.textContent).not.toContain('row:0')
})
})
describe('shows and hides scrollbars based on rendered content', () => {
it('should set overflowX:hidden on scroll-container if columns fit within the available width', () => {
const rendered = findDOMNode(render(getMarkup({ columnsCount: 4 })))
expect(rendered.style.overflowX).toEqual('hidden')
})
it('should leave overflowX:auto on scroll-container if columns require more than the available width', () => {
const rendered = findDOMNode(render(getMarkup({ columnsCount: 25 })))
expect(rendered.style.overflowX).not.toEqual('hidden')
})
it('should set overflowY:hidden on scroll-container if rows fit within the available height', () => {
const rendered = findDOMNode(render(getMarkup({ rowsCount: 5 })))
expect(rendered.style.overflowY).toEqual('hidden')
})
it('should leave overflowY:auto on scroll-container if rows require more than the available height', () => {
const rendered = findDOMNode(render(getMarkup({ rowsCount: 25 })))
expect(rendered.style.overflowY).not.toEqual('hidden')
})
})
/** Tests scrolling via initial props */
describe(':scrollToColumn and :scrollToRow', () => {
it('should scroll to the left', () => {
const grid = render(getMarkup({ scrollToColumn: 0 }))
expect(grid.state.scrollLeft).toEqual(0)
})
it('should scroll over to the middle', () => {
const grid = render(getMarkup({ scrollToColumn: 24 }))
// 100 columns * 50 item width = 5,000 total item width
// 4 columns can be visible at a time and :scrollLeft is initially 0,
// So the minimum amount of scrolling leaves the 25th item at the right (just scrolled into view).
expect(grid.state.scrollLeft).toEqual(1050)
})
it('should scroll to the far right', () => {
const grid = render(getMarkup({ scrollToColumn: 49 }))
// 100 columns * 50 item width = 5,000 total item width
// Target offset for the last item then is 5,000 - 200
expect(grid.state.scrollLeft).toEqual(2300)
})
it('should scroll to the top', () => {
const grid = render(getMarkup({ scrollToRow: 0 }))
expect(grid.state.scrollTop).toEqual(0)
})
it('should scroll down to the middle', () => {
const grid = render(getMarkup({ scrollToRow: 49 }))
// 100 rows * 20 item height = 2,000 total item height
// 5 rows can be visible at a time and :scrollTop is initially 0,
// So the minimum amount of scrolling leaves the 50th item at the bottom (just scrolled into view).
expect(grid.state.scrollTop).toEqual(900)
})
it('should scroll to the bottom', () => {
const grid = render(getMarkup({ scrollToRow: 99 }))
// 100 rows * 20 item height = 2,000 total item height
// Target offset for the last item then is 2,000 - 100
expect(grid.state.scrollTop).toEqual(1900)
})
it('should scroll to a row and column just added', () => {
let grid = render(getMarkup())
expect(grid.state.scrollLeft).toEqual(0)
expect(grid.state.scrollTop).toEqual(0)
grid = render(getMarkup({
columnsCount: NUM_COLUMNS + 1,
rowsCount: NUM_ROWS + 1,
scrollToColumn: NUM_COLUMNS,
scrollToRow: NUM_ROWS
}))
expect(grid.state.scrollLeft).toEqual(2350)
expect(grid.state.scrollTop).toEqual(1920)
})
it('should scroll back to a newly-added cell without a change in prop', () => {
let grid = render(getMarkup({
columnsCount: NUM_COLUMNS,
rowsCount: NUM_ROWS,
scrollToColumn: NUM_COLUMNS,
scrollToRow: NUM_ROWS
}))
grid = render(getMarkup({
columnsCount: NUM_COLUMNS + 1,
rowsCount: NUM_ROWS + 1,
scrollToColumn: NUM_COLUMNS,
scrollToRow: NUM_ROWS
}))
expect(grid.state.scrollLeft).toEqual(2350)
expect(grid.state.scrollTop).toEqual(1920)
})
})
describe('property updates', () => {
it('should update :scrollToColumn position when :columnWidth changes', () => {
let grid = findDOMNode(render(getMarkup({ scrollToColumn: 25 })))
expect(grid.textContent).toContain('column:25')
// Making columns taller pushes name off/beyond the scrolled area
grid = findDOMNode(render(getMarkup({ scrollToColumn: 25, columnWidth: 20 })))
expect(grid.textContent).toContain('column:25')
})
it('should update :scrollToRow position when :rowHeight changes', () => {
let grid = findDOMNode(render(getMarkup({ scrollToRow: 50 })))
expect(grid.textContent).toContain('row:50')
// Making rows taller pushes name off/beyond the scrolled area
grid = findDOMNode(render(getMarkup({ scrollToRow: 50, rowHeight: 20 })))
expect(grid.textContent).toContain('row:50')
})
it('should update :scrollToColumn position when :height changes', () => {
let grid = findDOMNode(render(getMarkup({ scrollToColumn: 25 })))
expect(grid.textContent).toContain('column:25')
// Making the grid narrower leaves only room for 1 item
grid = findDOMNode(render(getMarkup({ scrollToColumn: 25, width: 50 })))
expect(grid.textContent).toContain('column:25')
})
it('should update :scrollToRow position when :height changes', () => {
let grid = findDOMNode(render(getMarkup({ scrollToRow: 50 })))
expect(grid.textContent).toContain('row:50')
// Making the grid shorter leaves only room for 1 item
grid = findDOMNode(render(getMarkup({ scrollToRow: 50, height: 20 })))
expect(grid.textContent).toContain('row:50')
})
it('should update :scrollToColumn position when :scrollToColumn changes', () => {
let grid = findDOMNode(render(getMarkup()))
expect(grid.textContent).not.toContain('column:25')
grid = findDOMNode(render(getMarkup({ scrollToColumn: 25 })))
expect(grid.textContent).toContain('column:25')
})
it('should update :scrollToRow position when :scrollToRow changes', () => {
let grid = findDOMNode(render(getMarkup()))
expect(grid.textContent).not.toContain('row:50')
grid = findDOMNode(render(getMarkup({ scrollToRow: 50 })))
expect(grid.textContent).toContain('row:50')
})
it('should update scroll position if size shrinks smaller than the current scroll', () => {
let grid = findDOMNode(render(getMarkup({ scrollToColumn: 250 })))
grid = findDOMNode(render(getMarkup()))
grid = findDOMNode(render(getMarkup({ scrollToColumn: 250, columnsCount: 10 })))
expect(grid.textContent).toContain('column:9')
})
it('should update scroll position if size shrinks smaller than the current scroll', () => {
let grid = findDOMNode(render(getMarkup({ scrollToRow: 500 })))
grid = findDOMNode(render(getMarkup()))
grid = findDOMNode(render(getMarkup({ scrollToRow: 500, rowsCount: 10 })))
expect(grid.textContent).toContain('row:9')
})
})
describe('noContentRenderer', () => {
it('should call :noContentRenderer if :columnsCount is 0', () => {
let list = findDOMNode(render(getMarkup({
noContentRenderer: () => <div>No data</div>,
columnsCount: 0
})))
expect(list.textContent).toEqual('No data')
})
it('should call :noContentRenderer if :rowsCount is 0', () => {
let list = findDOMNode(render(getMarkup({
noContentRenderer: () => <div>No data</div>,
rowsCount: 0
})))
expect(list.textContent).toEqual('No data')
})
it('should render an empty body if :columnsCount is 0 and there is no :noContentRenderer', () => {
let list = findDOMNode(render(getMarkup({
columnsCount: 0
})))
expect(list.textContent).toEqual('')
})
it('should render an empty body if :rowsCount is 0 and there is no :noContentRenderer', () => {
let list = findDOMNode(render(getMarkup({
rowsCount: 0
})))
expect(list.textContent).toEqual('')
})
})
describe('onSectionRendered', () => {
it('should call :onSectionRendered if at least one cell is rendered', () => {
let columnStartIndex, columnStopIndex, rowStartIndex, rowStopIndex
render(getMarkup({
onSectionRendered: params => ({ columnStartIndex, columnStopIndex, rowStartIndex, rowStopIndex } = params)
}))
expect(columnStartIndex).toEqual(0)
expect(columnStopIndex).toEqual(3)
expect(rowStartIndex).toEqual(0)
expect(rowStopIndex).toEqual(4)
})
it('should not call :onSectionRendered unless the column or row start or stop indices have changed', () => {
let numCalls = 0
let columnStartIndex, columnStopIndex, rowStartIndex, rowStopIndex
const onSectionRendered = params => {
columnStartIndex = params.columnStartIndex
columnStopIndex = params.columnStopIndex
rowStartIndex = params.rowStartIndex
rowStopIndex = params.rowStopIndex
numCalls++
}
render(getMarkup({ onSectionRendered }))
expect(numCalls).toEqual(1)
expect(columnStartIndex).toEqual(0)
expect(columnStopIndex).toEqual(3)
expect(rowStartIndex).toEqual(0)
expect(rowStopIndex).toEqual(4)
render(getMarkup({ onSectionRendered }))
expect(numCalls).toEqual(1)
expect(columnStartIndex).toEqual(0)
expect(columnStopIndex).toEqual(3)
expect(rowStartIndex).toEqual(0)
expect(rowStopIndex).toEqual(4)
})
it('should call :onSectionRendered if the row or column start or stop indices have changed', () => {
let numCalls = 0
let columnStartIndex, columnStopIndex, rowStartIndex, rowStopIndex
const onSectionRendered = params => {
columnStartIndex = params.columnStartIndex
columnStopIndex = params.columnStopIndex
rowStartIndex = params.rowStartIndex
rowStopIndex = params.rowStopIndex
numCalls++
}
render(getMarkup({ onSectionRendered }))
expect(columnStartIndex).toEqual(0)
expect(columnStopIndex).toEqual(3)
expect(rowStartIndex).toEqual(0)
expect(rowStopIndex).toEqual(4)
render(getMarkup({
height: 50,
onSectionRendered
}))
expect(numCalls).toEqual(2)
expect(columnStartIndex).toEqual(0)
expect(columnStopIndex).toEqual(3)
expect(rowStartIndex).toEqual(0)
expect(rowStopIndex).toEqual(2)
render(getMarkup({
height: 50,
onSectionRendered,
width: 100
}))
expect(numCalls).toEqual(3)
expect(columnStartIndex).toEqual(0)
expect(columnStopIndex).toEqual(1)
expect(rowStartIndex).toEqual(0)
expect(rowStopIndex).toEqual(2)
})
it('should not call :onSectionRendered if no cells are rendered', () => {
let numCalls = 0
render(getMarkup({
height: 0,
onSectionRendered: params => numCalls++
}))
expect(numCalls).toEqual(0)
})
})
describe(':scrollLeft and :scrollTop properties', () => {
it('should render correctly when an initial :scrollLeft and :scrollTop properties are specified', () => {
let columnStartIndex, columnStopIndex, rowStartIndex, rowStopIndex
render(getMarkup({
onSectionRendered: params => ({ columnStartIndex, columnStopIndex, rowStartIndex, rowStopIndex } = params),
scrollLeft: 250,
scrollTop: 100
}))
expect(rowStartIndex).toEqual(5)
expect(rowStopIndex).toEqual(9)
expect(columnStartIndex).toEqual(5)
expect(columnStopIndex).toEqual(8)
})
it('should render correctly when :scrollLeft and :scrollTop properties are updated', () => {
let columnStartIndex, columnStopIndex, rowStartIndex, rowStopIndex
render(getMarkup({
onSectionRendered: params => ({ columnStartIndex, columnStopIndex, rowStartIndex, rowStopIndex } = params)
}))
expect(rowStartIndex).toEqual(0)
expect(rowStopIndex).toEqual(4)
expect(columnStartIndex).toEqual(0)
expect(columnStopIndex).toEqual(3)
render(getMarkup({
onSectionRendered: params => ({ columnStartIndex, columnStopIndex, rowStartIndex, rowStopIndex } = params),
scrollLeft: 250,
scrollTop: 100
}))
expect(rowStartIndex).toEqual(5)
expect(rowStopIndex).toEqual(9)
expect(columnStartIndex).toEqual(5)
expect(columnStopIndex).toEqual(8)
})
})
describe('styles and classeNames', () => {
it('should use the expected global CSS classNames', () => {
const rendered = findDOMNode(render(getMarkup()))
expect(rendered.className).toEqual('Grid')
})
it('should use a custom :className if specified', () => {
const rendered = findDOMNode(render(getMarkup({ className: 'foo' })))
expect(rendered.className).toContain('foo')
})
})
describe('onScroll', () => {
function helper ({ grid, scrollLeft, scrollTop }) {
const target = { scrollLeft, scrollTop }
grid.refs.scrollingContainer = target // HACK to work around _onScroll target check
Simulate.scroll(findDOMNode(grid), { target })
}
it('should trigger callback when component is mounted', () => {
const onScrollCalls = []
render(getMarkup({
onScroll: params => onScrollCalls.push(params),
scrollLeft: 50,
scrollTop: 100
}))
expect(onScrollCalls).toEqual([{
clientHeight: 100,
clientWidth: 200,
scrollHeight: 2000,
scrollLeft: 50,
scrollTop: 100,
scrollWidth: 2500
}])
})
it('should trigger callback when component scrolls horizontally', () => {
const onScrollCalls = []
const grid = render(getMarkup({
onScroll: params => onScrollCalls.push(params)
}))
helper({
grid,
scrollLeft: 100,
scrollTop: 0
})
expect(onScrollCalls.length).toEqual(2)
expect(onScrollCalls[1]).toEqual({
clientHeight: 100,
clientWidth: 200,
scrollHeight: 2000,
scrollLeft: 100,
scrollTop: 0,
scrollWidth: 2500
})
})
it('should trigger callback when component scrolls vertically', () => {
const onScrollCalls = []
const grid = render(getMarkup({
onScroll: params => onScrollCalls.push(params)
}))
helper({
grid,
scrollLeft: 0,
scrollTop: 100
})
expect(onScrollCalls.length).toEqual(2)
expect(onScrollCalls[1]).toEqual({
clientHeight: 100,
clientWidth: 200,
scrollHeight: 2000,
scrollLeft: 0,
scrollTop: 100,
scrollWidth: 2500
})
})
})
describe('overscanRowsCount', () => {
function createHelper () {
let columnOverscanStartIndex, columnOverscanStopIndex, columnStartIndex, columnStopIndex, rowOverscanStartIndex, rowOverscanStopIndex, rowStartIndex, rowStopIndex
function onSectionRendered (params) {
columnOverscanStartIndex = params.columnOverscanStartIndex
columnOverscanStopIndex = params.columnOverscanStopIndex
columnStartIndex = params.columnStartIndex
columnStopIndex = params.columnStopIndex
rowOverscanStartIndex = params.rowOverscanStartIndex
rowOverscanStopIndex = params.rowOverscanStopIndex
rowStartIndex = params.rowStartIndex
rowStopIndex = params.rowStopIndex
}
return {
columnOverscanStartIndex: () => columnOverscanStartIndex,
columnOverscanStopIndex: () => columnOverscanStopIndex,
columnStartIndex: () => columnStartIndex,
columnStopIndex: () => columnStopIndex,
onSectionRendered,
rowOverscanStartIndex: () => rowOverscanStartIndex,
rowOverscanStopIndex: () => rowOverscanStopIndex,
rowStartIndex: () => rowStartIndex,
rowStopIndex: () => rowStopIndex
}
}
it('should not overscan if disabled', () => {
const helper = createHelper()
render(getMarkup({
onSectionRendered: helper.onSectionRendered
}))
expect(helper.columnOverscanStartIndex()).toEqual(helper.columnStartIndex())
expect(helper.columnOverscanStopIndex()).toEqual(helper.columnStopIndex())
expect(helper.rowOverscanStartIndex()).toEqual(helper.rowStartIndex())
expect(helper.rowOverscanStopIndex()).toEqual(helper.rowStopIndex())
})
it('should overscan the specified amount', () => {
const helper = createHelper()
render(getMarkup({
onSectionRendered: helper.onSectionRendered,
overscanColumnsCount: 2,
overscanRowsCount: 5,
scrollToColumn: 25,
scrollToRow: 50
}))
expect(helper.columnOverscanStartIndex()).toEqual(20)
expect(helper.columnOverscanStopIndex()).toEqual(27)
expect(helper.columnStartIndex()).toEqual(22)
expect(helper.columnStopIndex()).toEqual(25)
expect(helper.rowOverscanStartIndex()).toEqual(41)
expect(helper.rowOverscanStopIndex()).toEqual(55)
expect(helper.rowStartIndex()).toEqual(46)
expect(helper.rowStopIndex()).toEqual(50)
})
it('should not overscan beyond the bounds of the grid', () => {
const helper = createHelper()
render(getMarkup({
onSectionRendered: helper.onSectionRendered,
columnsCount: 6,
overscanColumnsCount: 10,
overscanRowsCount: 10,
rowsCount: 5
}))
expect(helper.columnOverscanStartIndex()).toEqual(0)
expect(helper.columnOverscanStopIndex()).toEqual(5)
expect(helper.columnStartIndex()).toEqual(0)
expect(helper.columnStopIndex()).toEqual(3)
expect(helper.rowOverscanStartIndex()).toEqual(0)
expect(helper.rowOverscanStopIndex()).toEqual(4)
expect(helper.rowStartIndex()).toEqual(0)
expect(helper.rowStopIndex()).toEqual(4)
})
})
})