Skip to content

Commit 359845d

Browse files
committed
Padding cache spec refactoring: remove first/last items
1 parent 0b6296c commit 359845d

File tree

2 files changed

+85
-21
lines changed

2 files changed

+85
-21
lines changed

test/PaddingCacheSpec.js

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ describe('uiScroll Paddings cache', function () {
66
beforeEach(module('ui.scroll.test.datasources'));
77

88
describe('applyUpdates tests\n', function () {
9+
var itemsCount = 20;
10+
var itemHeight = 100;
11+
var viewportHeight = 500;
12+
var MAX = 999999;
13+
914
var scrollSettings = {
10-
datasource: 'myMultipageDatasource',
15+
datasource: 'myResponsiveDatasource',
1116
adapter: 'adapter',
12-
itemHeight: 20,
13-
viewportHeight: 100
17+
itemHeight: itemHeight,
18+
viewportHeight: viewportHeight
1419
};
1520

1621
function getBottomPaddingHeight(viewport) {
@@ -25,40 +30,67 @@ describe('uiScroll Paddings cache', function () {
2530
return parseInt(angular.element(topPadding).css('height'), 10);
2631
}
2732

28-
var initialBottomHeight = 240;
33+
function scrollBottom(viewport, count = 1) {
34+
for (var i = 0; i < count; i++) {
35+
viewport.scrollTop(MAX);
36+
viewport.trigger('scroll');
37+
}
38+
}
39+
40+
function scrollTop(viewport, count = 1) {
41+
for (var i = 0; i < count; i++) {
42+
viewport.scrollTop(0);
43+
viewport.trigger('scroll');
44+
}
45+
}
2946

3047
it('should delete last row when out of buffer', function () {
48+
var removeLastItem;
49+
inject(function(myResponsiveDatasource) {
50+
var datasource = myResponsiveDatasource;
51+
removeLastItem = function() {
52+
datasource.data.slice(-1, 1);
53+
datasource.max--;
54+
};
55+
});
3156
runTest(scrollSettings,
3257
function (viewport, scope) {
3358

34-
viewport.scrollTop(1000);
35-
viewport.trigger('scroll');
36-
viewport.scrollTop(1000);
37-
viewport.trigger('scroll');
38-
viewport.scrollTop(0);
39-
viewport.trigger('scroll');
59+
scrollBottom(viewport, 2);
60+
scrollTop(viewport);
4061

41-
expect(getBottomPaddingHeight(viewport)).toBe(initialBottomHeight);
42-
scope.adapter.applyUpdates(20, []);
43-
expect(getBottomPaddingHeight(viewport)).toBe(initialBottomHeight - scrollSettings.itemHeight);
62+
var initialBottomHeight = getBottomPaddingHeight(viewport);
63+
removeLastItem();
64+
scope.adapter.applyUpdates(itemsCount, []);
65+
expect(getBottomPaddingHeight(viewport)).toBe(initialBottomHeight - itemHeight);
4466

67+
scrollBottom(viewport, 2);
68+
expect(viewport.scrollTop()).toBe(itemsCount * itemHeight - viewportHeight - itemHeight );
4569
}
4670
);
4771
});
4872

49-
it('should delete first when row out of buffer', function () {
73+
it('should delete first row when out of buffer', function () {
74+
var removeFirstItem;
75+
inject(function(myResponsiveDatasource) {
76+
var datasource = myResponsiveDatasource;
77+
removeFirstItem = function() {
78+
datasource.data.shift();
79+
datasource.min++;
80+
};
81+
});
5082
runTest(scrollSettings,
5183
function (viewport, scope) {
5284

53-
viewport.scrollTop(1000);
54-
viewport.trigger('scroll');
55-
viewport.scrollTop(1000);
56-
viewport.trigger('scroll');
85+
scrollBottom(viewport, 2);
5786

58-
// expect(getTopPaddingHeight(viewport)).toBe(initialBottomHeight);
59-
// scope.adapter.applyUpdates(1, []);
60-
// expect(getTopPaddingHeight(viewport)).toBe(initialBottomHeight - scrollSettings.itemHeight);
87+
var initialTopHeight = getTopPaddingHeight(viewport);
88+
removeFirstItem();
89+
scope.adapter.applyUpdates(1, []);
90+
expect(getTopPaddingHeight(viewport)).toBe(initialTopHeight - itemHeight);
6191

92+
scrollTop(viewport);
93+
expect(getTopPaddingHeight(viewport)).toBe(0);
6294
}
6395
);
6496
});

test/datasources.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,36 @@ angular.module('ui.scroll.test.datasources', [])
191191
}
192192
};
193193
}
194+
])
195+
196+
197+
.factory('myResponsiveDatasource', [
198+
'$log', '$timeout', '$rootScope', function () {
199+
var datasource = {
200+
data: [],
201+
min: 1,
202+
max: 20,
203+
init: function() {
204+
for (var i = this.min; i <= this.max; i++) {
205+
this.data.push('item' + i);
206+
}
207+
},
208+
getItem: function(index) {
209+
return this.data[index - this.min];
210+
},
211+
get: function (index, count, success) {
212+
var result = [];
213+
var start = Math.max(this.min, index);
214+
var end = Math.min(index + count - 1, this.max);
215+
if (start <= end) {
216+
for (var i = start; i <= end; i++) {
217+
result.push(this.getItem(i));
218+
}
219+
}
220+
success(result);
221+
}
222+
};
223+
datasource.init();
224+
return datasource;
225+
}
194226
]);

0 commit comments

Comments
 (0)