@@ -5,146 +5,227 @@ describe('uiScroll Paddings cache', function () {
5
5
beforeEach ( module ( 'ui.scroll' ) ) ;
6
6
beforeEach ( module ( 'ui.scroll.test.datasources' ) ) ;
7
7
8
- describe ( 'applyUpdates out of buffer\n' , function ( ) {
9
- var itemsCount = 30 ;
10
- var itemHeight = 100 ;
11
- var viewportHeight = 500 ;
12
-
13
- var scrollSettings = {
14
- datasource : 'myResponsiveDatasource' ,
15
- adapter : 'adapter' ,
16
- itemHeight : itemHeight ,
17
- viewportHeight : viewportHeight
18
- } ;
19
-
20
- function getBottomPaddingHeight ( viewport ) {
21
- var viewportChildren = viewport . children ( ) ;
22
- var bottomPadding = viewportChildren [ viewportChildren . length - 1 ] ;
23
- return parseInt ( angular . element ( bottomPadding ) . css ( 'height' ) , 10 ) ;
8
+ var itemsCount = 30 ;
9
+ var itemHeight = 100 ;
10
+ var viewportHeight = 500 ;
11
+ var MAX = 3 ; // maximum scrolling interations to reach out the EOF/BOF
12
+
13
+ var scrollSettings = {
14
+ datasource : 'myResponsiveDatasource' ,
15
+ adapter : 'adapter' ,
16
+ itemHeight : itemHeight ,
17
+ viewportHeight : viewportHeight
18
+ } ;
19
+
20
+ function getBottomPaddingHeight ( viewport ) {
21
+ var viewportChildren = viewport . children ( ) ;
22
+ var bottomPadding = viewportChildren [ viewportChildren . length - 1 ] ;
23
+ return parseInt ( angular . element ( bottomPadding ) . css ( 'height' ) , 10 ) ;
24
+ }
25
+
26
+ function getTopPaddingHeight ( viewport ) {
27
+ var viewportChildren = viewport . children ( ) ;
28
+ var topPadding = viewportChildren [ 0 ] ;
29
+ return parseInt ( angular . element ( topPadding ) . css ( 'height' ) , 10 ) ;
30
+ }
31
+
32
+ function scrollBottom ( viewport , count = 1 ) {
33
+ for ( var i = 0 ; i < count ; i ++ ) {
34
+ viewport . scrollTop ( 99999 ) ;
35
+ viewport . trigger ( 'scroll' ) ;
24
36
}
37
+ }
25
38
26
- function getTopPaddingHeight ( viewport ) {
27
- var viewportChildren = viewport . children ( ) ;
28
- var topPadding = viewportChildren [ 0 ] ;
29
- return parseInt ( angular . element ( topPadding ) . css ( 'height' ) , 10 ) ;
39
+ function scrollTop ( viewport , count = 1 ) {
40
+ for ( var i = 0 ; i < count ; i ++ ) {
41
+ viewport . scrollTop ( 0 ) ;
42
+ viewport . trigger ( 'scroll' ) ;
30
43
}
31
-
32
- function scrollBottom ( viewport , count = 1 ) {
33
- for ( var i = 0 ; i < count ; i ++ ) {
34
- viewport . scrollTop ( 99999 ) ;
35
- viewport . trigger ( 'scroll' ) ;
44
+ }
45
+
46
+ function removeItem ( datasource , index ) {
47
+ if ( index >= datasource . min && index <= datasource . max ) {
48
+ var indexRemoved = datasource . data . indexOf ( datasource . data [ index - datasource . min ] ) ;
49
+ datasource . data . splice ( indexRemoved , 1 ) ;
50
+ if ( index === datasource . min ) {
51
+ datasource . min ++ ;
52
+ }
53
+ else {
54
+ datasource . max -- ;
36
55
}
37
56
}
57
+ }
38
58
39
- function scrollTop ( viewport , count = 1 ) {
40
- for ( var i = 0 ; i < count ; i ++ ) {
41
- viewport . scrollTop ( 0 ) ;
42
- viewport . trigger ( 'scroll' ) ;
43
- }
59
+ function checkRow ( viewport , row , content ) {
60
+ var children = viewport . children ( ) ;
61
+ if ( row < 0 ) { // from the end
62
+ row = children . length - 2 + row ;
44
63
}
64
+ var rowElement = children [ row ] ;
65
+ expect ( rowElement . innerHTML ) . toBe ( content ) ;
66
+ }
67
+
68
+ it ( 'should set up properly' , function ( ) {
69
+ var datasource ;
70
+ inject ( function ( myResponsiveDatasource ) {
71
+ datasource = myResponsiveDatasource ;
72
+ } ) ;
73
+ runTest ( scrollSettings ,
74
+ function ( ) {
75
+ expect ( datasource . min ) . toBe ( 1 ) ;
76
+ expect ( datasource . max ) . toBe ( itemsCount ) ;
77
+ }
78
+ ) ;
79
+ } ) ;
80
+
81
+ describe ( 'removing outside the buffer via indexed-based applyUpdates\n' , function ( ) {
45
82
46
- it ( 'should delete last row when out of buffer ' , function ( ) {
47
- var removeLastItem ;
83
+ it ( 'should delete last row' , function ( ) {
84
+ var datasource ;
48
85
inject ( function ( myResponsiveDatasource ) {
49
- var datasource = myResponsiveDatasource ;
50
- removeLastItem = function ( ) {
51
- datasource . data . slice ( - 1 , 1 ) ;
52
- datasource . max -- ;
53
- } ;
86
+ datasource = myResponsiveDatasource ;
54
87
} ) ;
55
88
runTest ( scrollSettings ,
56
89
function ( viewport , scope ) {
57
90
58
- scrollBottom ( viewport , 3 ) ;
91
+ scrollBottom ( viewport , MAX ) ;
59
92
scrollTop ( viewport ) ;
60
93
61
94
var initialBottomHeight = getBottomPaddingHeight ( viewport ) ;
62
- removeLastItem ( ) ;
95
+ removeItem ( datasource , datasource . max ) ;
63
96
scope . adapter . applyUpdates ( itemsCount , [ ] ) ;
64
97
expect ( getBottomPaddingHeight ( viewport ) ) . toBe ( initialBottomHeight - itemHeight ) ;
65
98
66
- scrollBottom ( viewport , 3 ) ;
67
- expect ( viewport . scrollTop ( ) ) . toBe ( itemsCount * itemHeight - viewportHeight - itemHeight ) ;
99
+ scrollBottom ( viewport , MAX ) ;
100
+ expect ( viewport . scrollTop ( ) ) . toBe ( itemsCount * itemHeight - viewportHeight - itemHeight ) ;
101
+ checkRow ( viewport , - 1 , ( itemsCount - 2 ) + ': item' + ( itemsCount - 2 ) ) ;
68
102
}
69
103
) ;
70
104
} ) ;
71
105
72
- it ( 'should delete last row and then the next after last, when out of buffer ' , function ( ) {
73
- var removeLastItem ;
106
+ it ( 'should delete last row and then the next after last' , function ( ) {
107
+ var datasource ;
74
108
inject ( function ( myResponsiveDatasource ) {
75
- var datasource = myResponsiveDatasource ;
76
- removeLastItem = function ( ) {
77
- datasource . data . slice ( - 1 , 1 ) ;
78
- datasource . max -- ;
79
- } ;
109
+ datasource = myResponsiveDatasource ;
80
110
} ) ;
81
111
runTest ( scrollSettings ,
82
112
function ( viewport , scope ) {
83
113
84
- scrollBottom ( viewport , 3 ) ;
114
+ scrollBottom ( viewport , MAX ) ;
85
115
scrollTop ( viewport ) ;
86
116
87
117
var initialBottomHeight = getBottomPaddingHeight ( viewport ) ;
88
- removeLastItem ( ) ;
118
+ removeItem ( datasource , datasource . max ) ;
89
119
scope . adapter . applyUpdates ( itemsCount , [ ] ) ;
90
- removeLastItem ( ) ;
120
+ removeItem ( datasource , datasource . max ) ;
91
121
scope . adapter . applyUpdates ( itemsCount - 1 , [ ] ) ;
92
122
expect ( getBottomPaddingHeight ( viewport ) ) . toBe ( initialBottomHeight - itemHeight * 2 ) ;
93
123
94
- scrollBottom ( viewport , 3 ) ;
124
+ scrollBottom ( viewport , MAX ) ;
95
125
expect ( viewport . scrollTop ( ) ) . toBe ( itemsCount * itemHeight - viewportHeight - itemHeight * 2 ) ;
126
+ checkRow ( viewport , - 1 , ( itemsCount - 3 ) + ': item' + ( itemsCount - 3 ) ) ;
96
127
}
97
128
) ;
98
129
} ) ;
99
130
100
- it ( 'should delete first row when out of buffer ' , function ( ) {
101
- var removeFirstItem ;
131
+ it ( 'should delete first row' , function ( ) {
132
+ var datasource ;
102
133
inject ( function ( myResponsiveDatasource ) {
103
- var datasource = myResponsiveDatasource ;
104
- removeFirstItem = function ( ) {
105
- datasource . data . shift ( ) ;
106
- datasource . min ++ ;
107
- } ;
134
+ datasource = myResponsiveDatasource ;
108
135
} ) ;
109
136
runTest ( scrollSettings ,
110
137
function ( viewport , scope ) {
111
138
112
- scrollBottom ( viewport , 3 ) ;
139
+ scrollBottom ( viewport , MAX ) ;
113
140
114
141
var initialTopHeight = getTopPaddingHeight ( viewport ) ;
115
- removeFirstItem ( ) ;
142
+ removeItem ( datasource , datasource . min ) ;
116
143
scope . adapter . applyUpdates ( 1 , [ ] ) ;
117
144
expect ( getTopPaddingHeight ( viewport ) ) . toBe ( initialTopHeight - itemHeight ) ;
118
145
119
146
scrollTop ( viewport ) ;
120
147
expect ( getTopPaddingHeight ( viewport ) ) . toBe ( 0 ) ;
148
+ checkRow ( viewport , 1 , '2: item2' ) ;
121
149
}
122
150
) ;
123
151
} ) ;
124
152
125
- it ( 'should delete first row and then the next after first, when out of buffer ' , function ( ) {
126
- var removeFirstItem ;
153
+ it ( 'should delete first row and then the next after first' , function ( ) {
154
+ var datasource ;
127
155
inject ( function ( myResponsiveDatasource ) {
128
- var datasource = myResponsiveDatasource ;
129
- removeFirstItem = function ( ) {
130
- datasource . data . shift ( ) ;
131
- datasource . min ++ ;
132
- } ;
156
+ datasource = myResponsiveDatasource ;
133
157
} ) ;
134
158
runTest ( scrollSettings ,
135
159
function ( viewport , scope ) {
136
160
137
- scrollBottom ( viewport , 3 ) ;
161
+ scrollBottom ( viewport , MAX ) ;
138
162
139
163
var initialTopHeight = getTopPaddingHeight ( viewport ) ;
140
- removeFirstItem ( ) ;
164
+ removeItem ( datasource , datasource . min ) ;
141
165
scope . adapter . applyUpdates ( 1 , [ ] ) ;
142
- removeFirstItem ( ) ;
166
+ removeItem ( datasource , datasource . min ) ;
143
167
scope . adapter . applyUpdates ( 2 , [ ] ) ;
144
168
expect ( getTopPaddingHeight ( viewport ) ) . toBe ( initialTopHeight - itemHeight * 2 ) ;
145
169
146
170
scrollTop ( viewport ) ;
147
171
expect ( getTopPaddingHeight ( viewport ) ) . toBe ( 0 ) ;
172
+ checkRow ( viewport , 1 , '3: item3' ) ;
173
+ }
174
+ ) ;
175
+ } ) ;
176
+
177
+ } ) ;
178
+
179
+ describe ( 'removing inside the buffer\n' , function ( ) {
180
+
181
+ it ( 'should delete second row via index-based applyUpdates' , function ( ) {
182
+ var datasource ;
183
+ inject ( function ( myResponsiveDatasource ) {
184
+ datasource = myResponsiveDatasource ;
185
+ } ) ;
186
+ runTest ( scrollSettings ,
187
+ function ( viewport , scope ) {
188
+
189
+ removeItem ( datasource , datasource . min + 1 ) ;
190
+ scope . adapter . applyUpdates ( 2 , [ ] ) ;
191
+
192
+ checkRow ( viewport , 1 , '1: item1' ) ;
193
+ checkRow ( viewport , 2 , '2: item3' ) ;
194
+
195
+ scrollBottom ( viewport , MAX ) ;
196
+ scrollTop ( viewport ) ;
197
+
198
+ expect ( getTopPaddingHeight ( viewport ) ) . toBe ( 0 ) ;
199
+ checkRow ( viewport , 1 , '1: item1' ) ;
200
+ checkRow ( viewport , 2 , '2: item3' ) ;
201
+ }
202
+ ) ;
203
+ } ) ;
204
+
205
+ it ( 'should delete second row via function-based applyUpdates' , function ( ) {
206
+ var datasource ;
207
+ inject ( function ( myResponsiveDatasource ) {
208
+ datasource = myResponsiveDatasource ;
209
+ } ) ;
210
+ runTest ( scrollSettings ,
211
+ function ( viewport , scope ) {
212
+
213
+ removeItem ( datasource , datasource . min + 1 ) ;
214
+ scope . adapter . applyUpdates ( function ( item ) {
215
+ if ( item === 'item2' ) {
216
+ return [ ] ;
217
+ }
218
+ } ) ;
219
+
220
+ checkRow ( viewport , 1 , '1: item1' ) ;
221
+ checkRow ( viewport , 2 , '2: item3' ) ;
222
+
223
+ scrollBottom ( viewport , MAX ) ;
224
+ scrollTop ( viewport ) ;
225
+
226
+ expect ( getTopPaddingHeight ( viewport ) ) . toBe ( 0 ) ;
227
+ checkRow ( viewport , 1 , '1: item1' ) ;
228
+ checkRow ( viewport , 2 , '2: item3' ) ;
148
229
}
149
230
) ;
150
231
} ) ;
0 commit comments