forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridUtils.test.js
More file actions
518 lines (465 loc) · 14 KB
/
GridUtils.test.js
File metadata and controls
518 lines (465 loc) · 14 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
import {
computeCellMetadataAndUpdateScrollOffsetHelper,
createCallbackMemoizer,
getOverscanIndices,
getUpdatedOffsetForIndex,
getVisibleCellIndices,
initCellMetadata,
updateScrollIndexHelper
} from './GridUtils'
// Default cell sizes and offsets for use in below tests
function getCellMetadata () {
const cellSizes = [
10, // 0: 0..0 (min)
20, // 1: 0..10
15, // 2: 0..30
10, // 3: 5..45
15, // 4: 20..55
30, // 5: 50..70
20, // 6: 70..100
10, // 7: 80..110
30 // 8: 110..110 (max)
]
return initCellMetadata({
cellsCount: cellSizes.length,
size: index => cellSizes[index]
})
}
describe('computeCellMetadataAndUpdateScrollOffsetHelper', () => {
function helper ({
cellsCount = 100,
cellSize = 10,
computeMetadataCallbackProps = {},
computeMetadataOnNextUpdate = false,
nextCellsCount = 100,
nextCellSize = 10,
nextScrollToIndex,
scrollToIndex
} = {}) {
const computeMetadataCallbackCalls = []
const updateScrollOffsetForScrollToIndexCalls = []
computeCellMetadataAndUpdateScrollOffsetHelper({
cellsCount,
cellSize,
computeMetadataCallback: params => computeMetadataCallbackCalls.push(params),
computeMetadataCallbackProps,
computeMetadataOnNextUpdate,
nextCellsCount,
nextCellSize,
nextScrollToIndex,
scrollToIndex,
updateScrollOffsetForScrollToIndex: params => updateScrollOffsetForScrollToIndexCalls.push(params)
})
return {
computeMetadataCallbackCalls,
updateScrollOffsetForScrollToIndexCalls
}
}
it('should call :computeMetadataCallback if :computeMetadataOnNextUpdate is true', () => {
const { computeMetadataCallbackCalls } = helper({ computeMetadataOnNextUpdate: true })
expect(computeMetadataCallbackCalls.length).toEqual(1)
})
it('should pass the specified :computeMetadataCallbackProps to :computeMetadataCallback', () => {
const params = { foo: 'bar' }
const { computeMetadataCallbackCalls } = helper({
computeMetadataCallbackProps: params,
computeMetadataOnNextUpdate: true
})
expect(computeMetadataCallbackCalls).toEqual([params])
})
it('should call :computeMetadataCallback if :cellsCount has changed', () => {
const { computeMetadataCallbackCalls } = helper({
cellsCount: 100,
nextCellsCount: 200
})
expect(computeMetadataCallbackCalls.length).toEqual(1)
})
it('should call :computeMetadataCallback if numeric :cellSize has changed', () => {
const { computeMetadataCallbackCalls } = helper({
cellSize: 10,
nextCellSize: 20
})
expect(computeMetadataCallbackCalls.length).toEqual(1)
})
it('should not call :computeMetadataCallback if :cellSize callback has changed', () => {
const { computeMetadataCallbackCalls } = helper({
cellSize: () => {},
nextCellSize: () => {}
})
expect(computeMetadataCallbackCalls.length).toEqual(0)
})
it('should call :updateScrollOffsetForScrollToIndex if metadata has changed and :scrollToIndex has not', () => {
const { updateScrollOffsetForScrollToIndexCalls } = helper({
computeMetadataOnNextUpdate: true,
scrollToIndex: 10,
nextScrollToIndex: 10
})
expect(updateScrollOffsetForScrollToIndexCalls.length).toEqual(1)
})
it('should not call :updateScrollOffsetForScrollToIndex if :scrollToIndex is not specified', () => {
const { updateScrollOffsetForScrollToIndexCalls } = helper({
computeMetadataOnNextUpdate: true
})
expect(updateScrollOffsetForScrollToIndexCalls.length).toEqual(0)
})
it('should not call :updateScrollOffsetForScrollToIndex if :scrollToIndex has also changed', () => {
const { updateScrollOffsetForScrollToIndexCalls } = helper({
computeMetadataOnNextUpdate: true,
scrollToIndex: 10,
nextScrollToIndex: 20
})
expect(updateScrollOffsetForScrollToIndexCalls.length).toEqual(0)
})
it('should not call :computeMetadataCallback if the above conditions are not true', () => {
const { computeMetadataCallbackCalls } = helper()
expect(computeMetadataCallbackCalls.length).toEqual(0)
})
})
describe('createCallbackMemoizer', () => {
function OnRowsRendered () {
let numCalls = 0
let overscanStartIndex
let overscanStopIndex
let startIndex
let stopIndex
return {
numCalls: () => numCalls,
overscanStartIndex: () => overscanStartIndex,
overscanStopIndex: () => overscanStopIndex,
startIndex: () => startIndex,
stopIndex: () => stopIndex,
update: (params) => {
overscanStartIndex = params.overscanStartIndex
overscanStopIndex = params.overscanStopIndex
startIndex = params.startIndex
stopIndex = params.stopIndex
numCalls++
}
}
}
it('should not call onRowsRendered if startIndex or stopIndex are invalid', () => {
const util = new OnRowsRendered()
const helper = createCallbackMemoizer()
helper({
callback: util.update,
indices: {
startIndex: 0,
stopIndex: undefined
}
})
expect(util.numCalls()).toEqual(0)
helper({
callback: util.update,
indices: {
startIndex: undefined,
stopIndex: 0
}
})
expect(util.numCalls()).toEqual(0)
})
it('should call onRowsRendered if startIndex and stopIndex are valid', () => {
const util = new OnRowsRendered()
const helper = createCallbackMemoizer()
helper({
callback: util.update,
indices: {
startIndex: 0,
stopIndex: 1
}
})
expect(util.numCalls()).toEqual(1)
expect(util.startIndex()).toEqual(0)
expect(util.stopIndex()).toEqual(1)
})
it('should call onRowsRendered if startIndex and stopIndex are invalid but :requireAllKeys is false', () => {
const util = new OnRowsRendered()
const helper = createCallbackMemoizer(false)
helper({
callback: util.update,
indices: {
startIndex: undefined,
stopIndex: 1
}
})
expect(util.numCalls()).toEqual(1)
expect(util.startIndex()).toEqual(undefined)
expect(util.stopIndex()).toEqual(1)
})
it('should not call onRowsRendered if startIndex or stopIndex have not changed', () => {
const util = new OnRowsRendered()
const helper = createCallbackMemoizer()
helper({
callback: util.update,
indices: {
startIndex: 0,
stopIndex: 1
}
})
expect(util.numCalls()).toEqual(1)
expect(util.startIndex()).toEqual(0)
expect(util.stopIndex()).toEqual(1)
helper({
callback: util.update,
indices: {
startIndex: 0,
stopIndex: 1
}
})
expect(util.numCalls()).toEqual(1)
})
it('should not call onRowsRendered if startIndex or stopIndex have changed', () => {
const util = new OnRowsRendered()
const helper = createCallbackMemoizer()
helper({
callback: util.update,
indices: {
startIndex: 0,
stopIndex: 1
}
})
expect(util.numCalls()).toEqual(1)
expect(util.startIndex()).toEqual(0)
expect(util.stopIndex()).toEqual(1)
helper({
callback: util.update,
indices: {
startIndex: 1,
stopIndex: 1
}
})
expect(util.numCalls()).toEqual(2)
expect(util.startIndex()).toEqual(1)
expect(util.stopIndex()).toEqual(1)
helper({
callback: util.update,
indices: {
startIndex: 1,
stopIndex: 2
}
})
expect(util.numCalls()).toEqual(3)
expect(util.startIndex()).toEqual(1)
expect(util.stopIndex()).toEqual(2)
})
it('should call onRowsRendered if :overscanCellsCount changes', () => {
const util = new OnRowsRendered()
const helper = createCallbackMemoizer()
helper({
callback: util.update,
indices: {
overscanStartIndex: 0,
overscanStopIndex: 2,
startIndex: 0,
stopIndex: 1
}
})
expect(util.numCalls()).toEqual(1)
expect(util.startIndex()).toEqual(0)
expect(util.stopIndex()).toEqual(1)
expect(util.overscanStartIndex()).toEqual(0)
expect(util.overscanStopIndex()).toEqual(2)
helper({
callback: util.update,
indices: {
overscanStartIndex: 0,
overscanStopIndex: 3,
startIndex: 0,
stopIndex: 1
}
})
expect(util.numCalls()).toEqual(2)
expect(util.startIndex()).toEqual(0)
expect(util.stopIndex()).toEqual(1)
expect(util.overscanStartIndex()).toEqual(0)
expect(util.overscanStopIndex()).toEqual(3)
})
})
describe('getUpdatedOffsetForIndex', () => {
function testHelper (targetIndex, currentOffset, cellMetadata = getCellMetadata()) {
return getUpdatedOffsetForIndex({
cellMetadata,
containerSize: 50,
currentOffset,
targetIndex
})
}
it('should scroll to the beginning', () => {
expect(testHelper(0, 100)).toEqual(0)
})
it('should scroll forward to the middle', () => {
expect(testHelper(4, 0)).toEqual(20)
})
it('should scroll backward to the middle', () => {
expect(testHelper(2, 100)).toEqual(30)
})
it('should not scroll if an item is already visible', () => {
expect(testHelper(2, 20)).toEqual(20)
})
it('should scroll to the end', () => {
expect(testHelper(8, 0)).toEqual(110)
})
it('should not scroll too far backward', () => {
expect(testHelper(-5, 0)).toEqual(0)
})
it('should not scroll too far forward', () => {
expect(testHelper(105, 0)).toEqual(110)
})
})
describe('getVisibleCellIndices', () => {
function testHelper (currentOffset, cellMetadata = getCellMetadata()) {
return getVisibleCellIndices({
cellsCount: cellMetadata.length,
cellMetadata,
containerSize: 50,
currentOffset
})
}
it('should handle unscrolled', () => {
const { start, stop } = testHelper(0)
expect(start).toEqual(0)
expect(stop).toEqual(3)
})
it('should handle scrolled to the middle', () => {
const { start, stop } = testHelper(50)
expect(start).toEqual(3)
expect(stop).toEqual(5)
})
it('should handle scrolled to the end', () => {
const { start, stop } = testHelper(110)
expect(start).toEqual(6)
expect(stop).toEqual(8)
})
it('should handle scrolled past the end', () => {
const { start, stop } = testHelper(200)
expect(start).toEqual(6)
expect(stop).toEqual(8)
})
it('should handle scrolled past the beginning', () => {
const { start, stop } = testHelper(-50)
expect(start).toEqual(0)
expect(stop).toEqual(3)
})
})
describe('updateScrollIndexHelper', () => {
function helper ({
cellsCount = 100,
cellMetadata = getCellMetadata(),
cellSize = 10,
previousCellsCount = 100,
previousCellSize = 10,
previousScrollToIndex,
previousSize = 50,
scrollOffset = 0,
scrollToIndex,
size = 50
} = {}) {
let updateScrollIndexCallbackCalled = false
function updateScrollIndexCallback (params) {
updateScrollIndexCallbackCalled = true
}
updateScrollIndexHelper({
cellsCount,
cellMetadata,
cellSize,
previousCellsCount,
previousCellSize,
previousScrollToIndex,
previousSize,
scrollOffset,
scrollToIndex,
size,
updateScrollIndexCallback
})
return updateScrollIndexCallbackCalled
}
it('should not call :updateScrollIndexCallback if there is no :scrollToIndex and size has not changed', () => {
expect(helper()).toEqual(false)
})
it('should not call :updateScrollIndexCallback if an invalid :scrollToIndex has been specified', () => {
expect(helper({
size: 100,
previousSize: 50,
scrollToIndex: -1
})).toEqual(false)
})
it('should call :updateScrollIndexCallback if there is a :scrollToIndex and :size has changed', () => {
expect(helper({
size: 100,
previousSize: 50,
scrollToIndex: 10
})).toEqual(true)
})
it('should call :updateScrollIndexCallback if there is a :scrollToIndex and :cellSize has changed', () => {
expect(helper({
cellSize: 15,
previousCellSize: 20,
scrollToIndex: 10
})).toEqual(true)
})
it('should call :updateScrollIndexCallback if previous :scrollToIndex has changed', () => {
expect(helper({
previousScrollToIndex: 20,
scrollToIndex: 10
})).toEqual(true)
})
it('should call :updateScrollIndexCallback if :cellsCount has been reduced past the current scroll offset', () => {
expect(helper({
cellsCount: 50,
previousCellsCount: 100,
scrollOffset: 510
})).toEqual(true)
})
it('should call :updateScrollIndexCallback if there is no :scrollToIndex but :size has been reduced', () => {
expect(helper({
previousSize: 100,
scrollOffset: 510,
size: 50
})).toEqual(true)
})
it('should not call :updateScrollIndexCallback if there is no :scrollToIndex but :cellsCount has been increased', () => {
expect(helper({
cellsCount: 100,
previousCellsCount: 50
})).toEqual(false)
})
it('should not call :updateScrollIndexCallback if there is no :scrollToIndex but :size has been increased', () => {
expect(helper({
previousSize: 50,
size: 100
})).toEqual(false)
})
})
describe('getOverscanIndices', () => {
function testHelper (cellsCount, startIndex, stopIndex, overscanCellsCount) {
return getOverscanIndices({
cellsCount,
overscanCellsCount,
startIndex,
stopIndex
})
}
it('should not overscan if :overscanCellsCount is 0', () => {
expect(testHelper(100, 10, 20, 0)).toEqual({
overscanStartIndex: 10,
overscanStopIndex: 20
})
})
it('should overscan by the specified :overscanCellsCount', () => {
expect(testHelper(100, 10, 20, 10)).toEqual({
overscanStartIndex: 0,
overscanStopIndex: 30
})
})
it('should not overscan beyond the start of the list', () => {
expect(testHelper(100, 5, 15, 10)).toEqual({
overscanStartIndex: 0,
overscanStopIndex: 25
})
})
it('should not overscan beyond the end of the list', () => {
expect(testHelper(25, 10, 20, 10)).toEqual({
overscanStartIndex: 0,
overscanStopIndex: 24
})
})
})