forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateScrollIndexHelper.jest.js
More file actions
171 lines (151 loc) · 4.68 KB
/
updateScrollIndexHelper.jest.js
File metadata and controls
171 lines (151 loc) · 4.68 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
import updateScrollIndexHelper from './updateScrollIndexHelper'
import CellSizeAndPositionManager from './CellSizeAndPositionManager'
// Default cell sizes and offsets for use in shared tests
export function getCellSizeAndPositionManager ({
cellCount = CELL_SIZES.length,
estimatedCellSize = 10
}) {
return new CellSizeAndPositionManager({
cellCount,
cellSizeGetter: ({ index }) => CELL_SIZES[index % CELL_SIZES.length],
estimatedCellSize
})
}
const CELL_SIZES = [
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)
]
describe('updateScrollIndexHelper', () => {
function helper ({
cellCount = undefined,
cellSizeAndPositionManager,
cellSize = 10,
previousCellsCount = undefined,
previousCellSize = 10,
previousScrollToAlignment = 'auto',
previousScrollToIndex,
previousSize = 50,
scrollOffset = 0,
scrollToAlignment = 'auto',
scrollToIndex,
size = 50
} = {}) {
cellSizeAndPositionManager = cellSizeAndPositionManager || getCellSizeAndPositionManager({ cellCount })
cellCount = cellCount === undefined
? cellSizeAndPositionManager.getCellCount()
: cellCount
previousCellsCount = previousCellsCount === undefined
? cellCount
: previousCellsCount
let updateScrollIndexCallbackCalled = false
function updateScrollIndexCallback (params) {
updateScrollIndexCallbackCalled = true
}
updateScrollIndexHelper({
cellCount,
cellSizeAndPositionManager,
cellSize,
previousCellsCount,
previousCellSize,
previousScrollToAlignment,
previousScrollToIndex,
previousSize,
scrollOffset,
scrollToAlignment,
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({
cellCount: 100,
size: 100,
previousSize: 50,
scrollToIndex: 10
})).toEqual(true)
})
it('should call :updateScrollIndexCallback if there is a :scrollToIndex and :cellSize has changed', () => {
expect(helper({
cellCount: 100,
cellSize: 15,
previousCellSize: 20,
scrollToIndex: 10
})).toEqual(true)
})
it('should call :updateScrollIndexCallback if previous :scrollToIndex has changed', () => {
expect(helper({
cellCount: 15,
previousScrollToIndex: 20,
scrollToIndex: 10
})).toEqual(true)
})
it('should call :updateScrollIndexCallback if :cellCount has been reduced past the current scroll offset', () => {
expect(helper({
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 measure rows if :size or :cellCount have been reduced but only use already measured (or estimated) total size', () => {
const cellSizeAndPositionManager = {
getCellCount: () => CELL_SIZES.length,
getTotalSize: () => 560
}
expect(helper({
cellSizeAndPositionManager,
previousSize: 100,
scrollOffset: 510,
size: 50
})).toEqual(false)
})
it('should not call :updateScrollIndexCallback if there is no :scrollToIndex but :cellCount has been increased', () => {
expect(helper({
cellCount: 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)
})
it('should call :updateScrollIndexCallback if :scrollToAlignment has changed', () => {
expect(helper({
previousScrollToAlignment: 'start',
scrollToAlignment: 'end',
scrollToIndex: 5
})).toEqual(true)
})
it('should not call :updateScrollIndexCallback if :scrollToAlignment has changed but there is no :scrollToIndex', () => {
expect(helper({
previousScrollToAlignment: 'start',
scrollToAlignment: 'end'
})).toEqual(false)
})
})