Skip to content

Commit 69eef67

Browse files
committed
some additional tests for Paddings cache
1 parent 7d316b2 commit 69eef67

File tree

2 files changed

+157
-71
lines changed

2 files changed

+157
-71
lines changed

demo/adapterSync/adapterSync.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ app.factory('Server', [
104104
for (var i = 0; i < length; i++) {
105105
if (this.data[i].id === itemId) {
106106
var indexRemoved = this.data[i].index;
107+
if(indexRemoved > this.firstIndex) {
108+
for (var j = i; j < length; j++) {
109+
this.data[j].index--;
110+
}
111+
}
107112
this.data.splice(i, 1);
108113
this.setIndicies();
109114
return this.returnDeferredResult(indexRemoved);

test/PaddingCacheSpec.js

Lines changed: 152 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,146 +5,227 @@ describe('uiScroll Paddings cache', function () {
55
beforeEach(module('ui.scroll'));
66
beforeEach(module('ui.scroll.test.datasources'));
77

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');
2436
}
37+
}
2538

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');
3043
}
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--;
3655
}
3756
}
57+
}
3858

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;
4463
}
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 () {
4582

46-
it('should delete last row when out of buffer', function () {
47-
var removeLastItem;
83+
it('should delete last row', function () {
84+
var datasource;
4885
inject(function(myResponsiveDatasource) {
49-
var datasource = myResponsiveDatasource;
50-
removeLastItem = function() {
51-
datasource.data.slice(-1, 1);
52-
datasource.max--;
53-
};
86+
datasource = myResponsiveDatasource;
5487
});
5588
runTest(scrollSettings,
5689
function (viewport, scope) {
5790

58-
scrollBottom(viewport, 3);
91+
scrollBottom(viewport, MAX);
5992
scrollTop(viewport);
6093

6194
var initialBottomHeight = getBottomPaddingHeight(viewport);
62-
removeLastItem();
95+
removeItem(datasource, datasource.max);
6396
scope.adapter.applyUpdates(itemsCount, []);
6497
expect(getBottomPaddingHeight(viewport)).toBe(initialBottomHeight - itemHeight);
6598

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));
68102
}
69103
);
70104
});
71105

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;
74108
inject(function(myResponsiveDatasource) {
75-
var datasource = myResponsiveDatasource;
76-
removeLastItem = function() {
77-
datasource.data.slice(-1, 1);
78-
datasource.max--;
79-
};
109+
datasource = myResponsiveDatasource;
80110
});
81111
runTest(scrollSettings,
82112
function (viewport, scope) {
83113

84-
scrollBottom(viewport, 3);
114+
scrollBottom(viewport, MAX);
85115
scrollTop(viewport);
86116

87117
var initialBottomHeight = getBottomPaddingHeight(viewport);
88-
removeLastItem();
118+
removeItem(datasource, datasource.max);
89119
scope.adapter.applyUpdates(itemsCount, []);
90-
removeLastItem();
120+
removeItem(datasource, datasource.max);
91121
scope.adapter.applyUpdates(itemsCount - 1, []);
92122
expect(getBottomPaddingHeight(viewport)).toBe(initialBottomHeight - itemHeight * 2);
93123

94-
scrollBottom(viewport, 3);
124+
scrollBottom(viewport, MAX);
95125
expect(viewport.scrollTop()).toBe(itemsCount * itemHeight - viewportHeight - itemHeight * 2);
126+
checkRow(viewport, -1, (itemsCount - 3) + ': item' + (itemsCount - 3));
96127
}
97128
);
98129
});
99130

100-
it('should delete first row when out of buffer', function () {
101-
var removeFirstItem;
131+
it('should delete first row', function () {
132+
var datasource;
102133
inject(function(myResponsiveDatasource) {
103-
var datasource = myResponsiveDatasource;
104-
removeFirstItem = function() {
105-
datasource.data.shift();
106-
datasource.min++;
107-
};
134+
datasource = myResponsiveDatasource;
108135
});
109136
runTest(scrollSettings,
110137
function (viewport, scope) {
111138

112-
scrollBottom(viewport, 3);
139+
scrollBottom(viewport, MAX);
113140

114141
var initialTopHeight = getTopPaddingHeight(viewport);
115-
removeFirstItem();
142+
removeItem(datasource, datasource.min);
116143
scope.adapter.applyUpdates(1, []);
117144
expect(getTopPaddingHeight(viewport)).toBe(initialTopHeight - itemHeight);
118145

119146
scrollTop(viewport);
120147
expect(getTopPaddingHeight(viewport)).toBe(0);
148+
checkRow(viewport, 1, '2: item2');
121149
}
122150
);
123151
});
124152

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;
127155
inject(function(myResponsiveDatasource) {
128-
var datasource = myResponsiveDatasource;
129-
removeFirstItem = function() {
130-
datasource.data.shift();
131-
datasource.min++;
132-
};
156+
datasource = myResponsiveDatasource;
133157
});
134158
runTest(scrollSettings,
135159
function (viewport, scope) {
136160

137-
scrollBottom(viewport, 3);
161+
scrollBottom(viewport, MAX);
138162

139163
var initialTopHeight = getTopPaddingHeight(viewport);
140-
removeFirstItem();
164+
removeItem(datasource, datasource.min);
141165
scope.adapter.applyUpdates(1, []);
142-
removeFirstItem();
166+
removeItem(datasource, datasource.min);
143167
scope.adapter.applyUpdates(2, []);
144168
expect(getTopPaddingHeight(viewport)).toBe(initialTopHeight - itemHeight * 2);
145169

146170
scrollTop(viewport);
147171
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');
148229
}
149230
);
150231
});

0 commit comments

Comments
 (0)