-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Grid.test.js
1412 lines (1250 loc) · 47 KB
/
Grid.test.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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import getScrollbarSize from 'dom-helpers/util/scrollbarSize'
import React from 'react'
import { findDOMNode } from 'react-dom'
import { Simulate } from 'react-addons-test-utils'
import { render } from '../TestUtils'
import Grid, { DEFAULT_SCROLLING_RESET_TIME_INTERVAL } from './Grid'
import { SCROLL_DIRECTION_BACKWARD, SCROLL_DIRECTION_FORWARD } from './utils/getOverscanIndices'
const DEFAULT_COLUMN_WIDTH = 50
const DEFAULT_HEIGHT = 100
const DEFAULT_ROW_HEIGHT = 20
const DEFAULT_WIDTH = 200
const NUM_ROWS = 100
const NUM_COLUMNS = 50
describe('Grid', () => {
function defaultCellRenderer ({ columnIndex, key, rowIndex, style }) {
return (
<div
className='gridItem'
key={key}
style={style}
>
{`row:${rowIndex}, column:${columnIndex}`}
</div>
)
}
function simulateScroll ({
grid,
scrollLeft = 0,
scrollTop = 0
}) {
const target = { scrollLeft, scrollTop }
grid._scrollingContainer = target // HACK to work around _onScroll target check
Simulate.scroll(findDOMNode(grid), { target })
}
function getMarkup (props = {}) {
return (
<Grid
cellRenderer={defaultCellRenderer}
columnCount={NUM_COLUMNS}
columnWidth={DEFAULT_COLUMN_WIDTH}
height={DEFAULT_HEIGHT}
overscanColumnCount={0}
overscanRowCount={0}
autoHeight={false}
rowHeight={DEFAULT_ROW_HEIGHT}
rowCount={NUM_ROWS}
width={DEFAULT_WIDTH}
{...props}
/>
)
}
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({ rowCount: 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({ columnCount: 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 cellRenderer ({ columnIndex, key, rowIndex, style }) {
if (columnIndex === 0) {
return null
} else if (rowIndex === 0) {
return false
} else {
return (
<div
className='cell'
key={key}
style={style}
>
{`row:${rowIndex}, column:${columnIndex}`}
</div>
)
}
}
const rendered = findDOMNode(render(getMarkup({
columnCount: 3,
overscanColumnCount: 0,
overscanRowCount: 0,
rowCount: 3,
cellRenderer
})))
expect(rendered.querySelectorAll('.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', () => {
let scrollbarSize
beforeAll(() => {
scrollbarSize = getScrollbarSize()
})
it('should set overflowX:hidden if columns fit within the available width and y-axis has no scrollbar', () => {
const rendered = findDOMNode(render(getMarkup({
columnCount: 4,
rowCount: 5
})))
expect(rendered.style.overflowX).toEqual('hidden')
})
it('should set overflowX:hidden if columns and y-axis scrollbar fit within the available width', () => {
const rendered = findDOMNode(render(getMarkup({
columnCount: 4,
width: 200 + scrollbarSize
})))
expect(rendered.style.overflowX).toEqual('hidden')
})
it('should leave overflowX:auto if columns require more than the available width', () => {
const rendered = findDOMNode(render(getMarkup({
columnCount: 4,
width: 200 - 1,
rowCount: 5
})))
expect(rendered.style.overflowX).not.toEqual('hidden')
})
it('should leave overflowX:auto if columns and y-axis scrollbar require more than the available width', () => {
const rendered = findDOMNode(render(getMarkup({
columnCount: 4,
width: 200 + scrollbarSize - 1
})))
expect(rendered.style.overflowX).not.toEqual('hidden')
})
it('should set overflowY:hidden if rows fit within the available width and xaxis has no scrollbar', () => {
const rendered = findDOMNode(render(getMarkup({
rowCount: 5,
columnCount: 4
})))
expect(rendered.style.overflowY).toEqual('hidden')
})
it('should set overflowY:hidden if rows and x-axis scrollbar fit within the available width', () => {
const rendered = findDOMNode(render(getMarkup({
rowCount: 5,
height: 100 + scrollbarSize
})))
expect(rendered.style.overflowY).toEqual('hidden')
})
it('should leave overflowY:auto if rows require more than the available width', () => {
const rendered = findDOMNode(render(getMarkup({
rowCount: 5,
height: 100 - 1,
columnCount: 4
})))
expect(rendered.style.overflowY).not.toEqual('hidden')
})
it('should leave overflowY:auto if rows and x-axis scrollbar require more than the available width', () => {
const rendered = findDOMNode(render(getMarkup({
rowCount: 5,
height: 100 + scrollbarSize - 1
})))
expect(rendered.style.overflowY).not.toEqual('hidden')
})
it('should accept styles that overwrite calculated ones', () => {
const rendered = findDOMNode(render(getMarkup({
columnCount: 1,
height: 1,
rowCount: 1,
style: {
overflowY: 'visible',
overflowX: 'visible'
},
width: 1
})))
expect(rendered.style.overflowY).toEqual('visible')
expect(rendered.style.overflowX).toEqual('visible')
})
})
/** 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({
columnCount: NUM_COLUMNS + 1,
rowCount: 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({
columnCount: NUM_COLUMNS,
rowCount: NUM_ROWS,
scrollToColumn: NUM_COLUMNS,
scrollToRow: NUM_ROWS
}))
grid = render(getMarkup({
columnCount: NUM_COLUMNS + 1,
rowCount: NUM_ROWS + 1,
scrollToColumn: NUM_COLUMNS,
scrollToRow: NUM_ROWS
}))
expect(grid.state.scrollLeft).toEqual(2350)
expect(grid.state.scrollTop).toEqual(1920)
})
it('should scroll to the correct position for :scrollToAlignment "start"', () => {
const grid = render(getMarkup({
scrollToAlignment: 'start',
scrollToColumn: 24,
scrollToRow: 49
}))
// 100 columns * 50 item width = 5,000 total item width
// 100 rows * 20 item height = 2,000 total item height
// 4 columns and 5 rows can be visible at a time.
// The minimum amount of scrolling leaves the specified cell in the bottom/right corner (just scrolled into view).
// Since alignment is set to "start" we should scroll past this point until the cell is aligned top/left.
expect(grid.state.scrollLeft).toEqual(1200)
expect(grid.state.scrollTop).toEqual(980)
})
it('should scroll to the correct position for :scrollToAlignment "end"', () => {
render(getMarkup({
scrollToColumn: 99,
scrollToRow: 99
}))
const grid = render(getMarkup({
scrollToAlignment: 'end',
scrollToColumn: 24,
scrollToRow: 49
}))
// 100 columns * 50 item width = 5,000 total item width
// 100 rows * 20 item height = 2,000 total item height
// We first scroll past the specified cell and then back.
// The minimum amount of scrolling then should leave the specified cell in the top/left corner (just scrolled into view).
// Since alignment is set to "end" we should scroll past this point until the cell is aligned bottom/right.
expect(grid.state.scrollLeft).toEqual(1050)
expect(grid.state.scrollTop).toEqual(900)
})
it('should scroll to the correct position for :scrollToAlignment "center"', () => {
render(getMarkup({
scrollToColumn: 99,
scrollToRow: 99
}))
const grid = render(getMarkup({
scrollToAlignment: 'center',
scrollToColumn: 24,
scrollToRow: 49
}))
// 100 columns * 50 item width = 5,000 total item width
// Viewport width is 200
// Column 24 starts at 1,200, center point at 1,225, so...
expect(grid.state.scrollLeft).toEqual(1125)
// 100 rows * 20 item height = 2,000 total item height
// Viewport height is 100
// Row 49 starts at 980, center point at 990, so...
expect(grid.state.scrollTop).toEqual(940)
})
// Tests issue #218
it('should set the correct :scrollTop after row and column counts increase from 0', () => {
const expectedScrollTop = 100 * DEFAULT_ROW_HEIGHT - DEFAULT_HEIGHT + DEFAULT_ROW_HEIGHT
render(getMarkup({
columnCount: 0,
rowCount: 150,
scrollToRow: 100
}))
expect(
findDOMNode(render(getMarkup({
columnCount: 150,
rowCount: 150,
scrollToRow: 100
}))).scrollTop
).toEqual(expectedScrollTop)
})
})
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 :width 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, columnCount: 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, rowCount: 10 })))
expect(grid.textContent).toContain('row:9')
})
})
describe('noContentRenderer', () => {
it('should call :noContentRenderer if :columnCount is 0', () => {
let list = findDOMNode(render(getMarkup({
noContentRenderer: () => <div>No data</div>,
columnCount: 0
})))
expect(list.textContent).toEqual('No data')
})
it('should call :noContentRenderer if :rowCount is 0', () => {
let list = findDOMNode(render(getMarkup({
noContentRenderer: () => <div>No data</div>,
rowCount: 0
})))
expect(list.textContent).toEqual('No data')
})
// Sanity check for bvaughn/react-virtualized/pull/348
it('should render an empty body if :rowCount or :columnCount changes to 0', () => {
function noContentRenderer () {
return <div>No data</div>
}
let list = findDOMNode(render(getMarkup({
noContentRenderer
})))
expect(list.textContent).not.toEqual('No data')
list = findDOMNode(render(getMarkup({
noContentRenderer,
rowCount: 0
})))
expect(list.textContent).toEqual('No data')
list = findDOMNode(render(getMarkup({
noContentRenderer
})))
expect(list.textContent).not.toEqual('No data')
list = findDOMNode(render(getMarkup({
columnCount: 0,
noContentRenderer
})))
expect(list.textContent).toEqual('No data')
})
it('should render an empty body if :columnCount is 0 and there is no :noContentRenderer', () => {
let list = findDOMNode(render(getMarkup({
columnCount: 0
})))
expect(list.textContent).toEqual('')
})
it('should render an empty body if :rowCount is 0 and there is no :noContentRenderer', () => {
let list = findDOMNode(render(getMarkup({
rowCount: 0
})))
expect(list.textContent).toEqual('')
})
it('should render an empty body there is a :noContentRenderer but :height or :width are 0', () => {
let list = findDOMNode(render(getMarkup({
height: 0,
noContentRenderer: () => <div>No data</div>
})))
expect(list.textContent).toEqual('')
list = findDOMNode(render(getMarkup({
noContentRenderer: () => <div>No data</div>,
width: 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
findDOMNode(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, classNames, and ids', () => {
it('should use the expected global CSS classNames', () => {
const rendered = findDOMNode(render(getMarkup()))
expect(rendered.className).toEqual('ReactVirtualized__Grid')
})
it('should use a custom :className if specified', () => {
const rendered = findDOMNode(render(getMarkup({ className: 'foo' })))
expect(rendered.className).toContain('foo')
})
it('should use a custom :id if specified', () => {
const rendered = findDOMNode(render(getMarkup({ id: 'bar' })))
expect(rendered.getAttribute('id')).toEqual('bar')
})
it('should use a custom :style if specified', () => {
const style = { backgroundColor: 'red' }
const rendered = findDOMNode(render(getMarkup({ style })))
expect(rendered.style.backgroundColor).toEqual('red')
})
it('should use a custom :containerStyle if specified', () => {
const containerStyle = { backgroundColor: 'red' }
const rendered = findDOMNode(render(getMarkup({ containerStyle })))
expect(rendered.querySelector('.ReactVirtualized__Grid__innerScrollContainer').style.backgroundColor).toEqual('red')
})
})
describe('onScroll', () => {
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)
}))
simulateScroll({
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)
}))
simulateScroll({
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
})
})
it('should trigger callback with scrollLeft of 0 when total columns width is less than width', () => {
const onScrollCalls = []
const grid = render(getMarkup({
columnCount: 1,
columnWidth: 50,
onScroll: params => onScrollCalls.push(params),
scrollLeft: 0,
scrollTop: 10,
width: 200
}))
simulateScroll({
grid,
scrollLeft: 0,
scrollTop: 0
})
expect(onScrollCalls.length).toEqual(2)
expect(onScrollCalls[1]).toEqual({
clientHeight: 100,
clientWidth: 200,
scrollHeight: 2000,
scrollLeft: 0,
scrollTop: 0,
scrollWidth: 50
})
})
it('should trigger callback with scrollTop of 0 when total rows height is less than height', () => {
const onScrollCalls = []
const grid = render(getMarkup({
rowCount: 1,
rowHeight: 50,
onScroll: params => onScrollCalls.push(params),
scrollLeft: 0,
scrollTop: 10,
height: 200
}))
simulateScroll({
grid,
scrollLeft: 0,
scrollTop: 0
})
expect(onScrollCalls.length).toEqual(2)
expect(onScrollCalls[1]).toEqual({
clientHeight: 200,
clientWidth: 200,
scrollHeight: 50,
scrollLeft: 0,
scrollTop: 0,
scrollWidth: 2500
})
})
// Support use-cases like WindowScroller; enable them to stay in sync with scroll-to-cell changes.
it('should trigger when :scrollToColumn or :scrollToRow are changed via props', () => {
const onScrollCalls = []
render(getMarkup())
render(getMarkup({
onScroll: params => onScrollCalls.push(params),
scrollToColumn: 24,
scrollToRow: 49
}))
expect(onScrollCalls).toEqual([{
clientHeight: 100,
clientWidth: 200,
scrollHeight: 2000,
scrollLeft: 1050,
scrollTop: 900,
scrollWidth: 2500
}])
})
})
describe('overscanColumnCount & overscanRowCount', () => {
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,
overscanColumnCount: 2,
overscanRowCount: 5,
scrollToColumn: 25,
scrollToRow: 50
}))
expect(helper.columnOverscanStartIndex()).toEqual(22)
expect(helper.columnOverscanStopIndex()).toEqual(27)
expect(helper.columnStartIndex()).toEqual(22)
expect(helper.columnStopIndex()).toEqual(25)
expect(helper.rowOverscanStartIndex()).toEqual(46)
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,
columnCount: 6,
overscanColumnCount: 10,
overscanRowCount: 10,
rowCount: 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)
})
it('should set the correct scroll direction', () => {
// Do not pass in the initial state as props, otherwise the internal state is forbidden from
// updating itself
const grid = render(getMarkup())
// Simulate a scroll to set the initial internal state
simulateScroll({
grid,
scrollLeft: 50,
scrollTop: 50
})
expect(grid.state.scrollDirectionHorizontal).toEqual(SCROLL_DIRECTION_FORWARD)
expect(grid.state.scrollDirectionVertical).toEqual(SCROLL_DIRECTION_FORWARD)
simulateScroll({
grid,
scrollLeft: 0,
scrollTop: 0
})
expect(grid.state.scrollDirectionHorizontal).toEqual(SCROLL_DIRECTION_BACKWARD)
expect(grid.state.scrollDirectionVertical).toEqual(SCROLL_DIRECTION_BACKWARD)
simulateScroll({
grid,
scrollLeft: 100,
scrollTop: 100
})
expect(grid.state.scrollDirectionHorizontal).toEqual(SCROLL_DIRECTION_FORWARD)
expect(grid.state.scrollDirectionVertical).toEqual(SCROLL_DIRECTION_FORWARD)
})
it('should set the correct scroll direction when scroll position is updated from props', () => {
let grid = render(getMarkup({
scrollLeft: 50,
scrollTop: 50
}))
expect(grid.state.scrollDirectionHorizontal).toEqual(SCROLL_DIRECTION_FORWARD)
expect(grid.state.scrollDirectionVertical).toEqual(SCROLL_DIRECTION_FORWARD)
grid = render(getMarkup({
scrollLeft: 0,
scrollTop: 0
}))
expect(grid.state.scrollDirectionHorizontal).toEqual(SCROLL_DIRECTION_BACKWARD)
expect(grid.state.scrollDirectionVertical).toEqual(SCROLL_DIRECTION_BACKWARD)
grid = render(getMarkup({
scrollLeft: 100,
scrollTop: 100
}))
expect(grid.state.scrollDirectionHorizontal).toEqual(SCROLL_DIRECTION_FORWARD)
expect(grid.state.scrollDirectionVertical).toEqual(SCROLL_DIRECTION_FORWARD)
})
it('should not change the scroll position or direction if the axis is controlled by the parent component', () => {
// Setting the scrollTop prop here implies it is controlled by the parent component
// (e.g. WindowScroller), while scrollLeft is controlled internally by the Grid
const grid = render(getMarkup({
scrollTop: 50
}))
expect(grid.state.scrollLeft).toEqual(0)
expect(grid.state.scrollTop).toEqual(50)
expect(grid.state.scrollDirectionHorizontal).toEqual(SCROLL_DIRECTION_FORWARD)
expect(grid.state.scrollDirectionVertical).toEqual(SCROLL_DIRECTION_FORWARD)
simulateScroll({
grid,
scrollLeft: 100,
scrollTop: 100
})
expect(grid.state.scrollLeft).toEqual(100)
expect(grid.state.scrollTop).toEqual(50)
expect(grid.state.scrollDirectionHorizontal).toEqual(SCROLL_DIRECTION_FORWARD)
expect(grid.state.scrollDirectionVertical).toEqual(SCROLL_DIRECTION_FORWARD)
simulateScroll({
grid,
scrollLeft: 0,
scrollTop: 0
})
expect(grid.state.scrollLeft).toEqual(0)
expect(grid.state.scrollTop).toEqual(50)
expect(grid.state.scrollDirectionHorizontal).toEqual(SCROLL_DIRECTION_BACKWARD)
expect(grid.state.scrollDirectionVertical).toEqual(SCROLL_DIRECTION_FORWARD)
})
it('should overscan in the direction being scrolled', async (done) => {
const helper = createHelper()
let onSectionRenderedResolve
function onSectionRendered (params) {
helper.onSectionRendered(params)
if (onSectionRenderedResolve) {
onSectionRenderedResolve()
}
}
const grid = render(getMarkup({
onSectionRendered,
overscanColumnCount: 2,
overscanRowCount: 5
}))
// Wait until the onSectionRendered handler / debouncer has processed
let onSectionRenderedPromise = new Promise(resolve => {
onSectionRenderedResolve = resolve
})
simulateScroll({
grid,
scrollLeft: 200,
scrollTop: 200
})
await onSectionRenderedPromise
// It should overscan in the direction being scrolled while scroll is in progress
expect(helper.columnOverscanStartIndex()).toEqual(4)
expect(helper.columnOverscanStopIndex()).toEqual(9)
expect(helper.columnStartIndex()).toEqual(4)
expect(helper.columnStopIndex()).toEqual(7)
expect(helper.rowOverscanStartIndex()).toEqual(10)
expect(helper.rowOverscanStopIndex()).toEqual(19)
expect(helper.rowStartIndex()).toEqual(10)
expect(helper.rowStopIndex()).toEqual(14)
// Wait until the onSectionRendered handler / debouncer has processed
onSectionRenderedPromise = new Promise(resolve => {
onSectionRenderedResolve = resolve
})
simulateScroll({
grid,
scrollLeft: 100,
scrollTop: 100
})
await onSectionRenderedPromise
// It reset overscan once scrolling has finished
expect(helper.columnOverscanStartIndex()).toEqual(0)
expect(helper.columnOverscanStopIndex()).toEqual(5)
expect(helper.columnStartIndex()).toEqual(2)
expect(helper.columnStopIndex()).toEqual(5)
expect(helper.rowOverscanStartIndex()).toEqual(0)
expect(helper.rowOverscanStopIndex()).toEqual(9)
expect(helper.rowStartIndex()).toEqual(5)
expect(helper.rowStopIndex()).toEqual(9)
done()
})
})
describe('cellRangeRenderer', () => {
it('should use a custom :cellRangeRenderer if specified', () => {
let cellRangeRendererCalled = 0
let cellRangeRendererParams
const rendered = findDOMNode(render(getMarkup({