Skip to content

Commit b9bdd98

Browse files
committed
switch PaddingCache spec to ES6, add startIndex to scaffolding
1 parent 69eef67 commit b9bdd98

File tree

2 files changed

+60
-51
lines changed

2 files changed

+60
-51
lines changed

test/PaddingCacheSpec.js

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
/*global describe, beforeEach, module, it, expect, runTest */
2-
describe('uiScroll Paddings cache', function () {
2+
describe('uiScroll Paddings cache', () => {
33
'use strict';
44

55
beforeEach(module('ui.scroll'));
66
beforeEach(module('ui.scroll.test.datasources'));
77

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
8+
const itemsCount = 30;
9+
const itemHeight = 100;
10+
const viewportHeight = 500;
11+
const MAX = 3; // maximum scrolling interations to reach out the EOF/BOF
1212

13-
var scrollSettings = {
13+
const scrollSettings = {
1414
datasource: 'myResponsiveDatasource',
1515
adapter: 'adapter',
1616
itemHeight: itemHeight,
1717
viewportHeight: viewportHeight
1818
};
1919

2020
function getBottomPaddingHeight(viewport) {
21-
var viewportChildren = viewport.children();
22-
var bottomPadding = viewportChildren[viewportChildren.length - 1];
21+
const viewportChildren = viewport.children();
22+
const bottomPadding = viewportChildren[viewportChildren.length - 1];
2323
return parseInt(angular.element(bottomPadding).css('height'), 10);
2424
}
2525

2626
function getTopPaddingHeight(viewport) {
27-
var viewportChildren = viewport.children();
28-
var topPadding = viewportChildren[0];
27+
const viewportChildren = viewport.children();
28+
const topPadding = viewportChildren[0];
2929
return parseInt(angular.element(topPadding).css('height'), 10);
3030
}
3131

@@ -45,53 +45,65 @@ describe('uiScroll Paddings cache', function () {
4545

4646
function removeItem(datasource, index) {
4747
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++;
48+
const indexRemoved = datasource.data.indexOf(datasource.data[index - datasource.min]);
49+
if(indexRemoved > -1) {
50+
datasource.data.splice(indexRemoved, 1);
51+
if(index === datasource.min) {
52+
datasource.min++;
53+
}
54+
else {
55+
datasource.max--;
56+
}
5257
}
53-
else {
54-
datasource.max--;
58+
}
59+
}
60+
61+
function insertItems(datasource, index, items = []) {
62+
if(index >= datasource.min && index <= datasource.max && items.length) {
63+
const index = datasource.data.indexOf(datasource.data[index - datasource.min]);
64+
if(index > -1) {
65+
datasource.data.splice(index, 0, items);
66+
datasource.max += items.length;
5567
}
5668
}
5769
}
5870

5971
function checkRow(viewport, row, content) {
60-
var children = viewport.children();
72+
const children = viewport.children();
6173
if(row < 0) { // from the end
6274
row = children.length - 2 + row;
6375
}
64-
var rowElement = children[row];
76+
const rowElement = children[row];
6577
expect(rowElement.innerHTML).toBe(content);
6678
}
6779

68-
it('should set up properly', function () {
69-
var datasource;
80+
it('should set up properly', () => {
81+
let datasource;
7082
inject(function(myResponsiveDatasource) {
7183
datasource = myResponsiveDatasource;
7284
});
7385
runTest(scrollSettings,
74-
function () {
86+
() => {
7587
expect(datasource.min).toBe(1);
7688
expect(datasource.max).toBe(itemsCount);
7789
}
7890
);
7991
});
8092

81-
describe('removing outside the buffer via indexed-based applyUpdates\n', function () {
93+
describe('removing outside the buffer via indexed-based applyUpdates\n', () => {
8294

83-
it('should delete last row', function () {
84-
var datasource;
95+
it('should delete last row', () => {
96+
let datasource;
8597
inject(function(myResponsiveDatasource) {
8698
datasource = myResponsiveDatasource;
8799
});
88100
runTest(scrollSettings,
89-
function (viewport, scope) {
101+
(viewport, scope) => {
90102

91103
scrollBottom(viewport, MAX);
92104
scrollTop(viewport);
93105

94-
var initialBottomHeight = getBottomPaddingHeight(viewport);
106+
const initialBottomHeight = getBottomPaddingHeight(viewport);
95107
removeItem(datasource, datasource.max);
96108
scope.adapter.applyUpdates(itemsCount, []);
97109
expect(getBottomPaddingHeight(viewport)).toBe(initialBottomHeight - itemHeight);
@@ -103,18 +115,18 @@ describe('uiScroll Paddings cache', function () {
103115
);
104116
});
105117

106-
it('should delete last row and then the next after last', function () {
107-
var datasource;
118+
it('should delete last row and then the next after last', () => {
119+
let datasource;
108120
inject(function(myResponsiveDatasource) {
109121
datasource = myResponsiveDatasource;
110122
});
111123
runTest(scrollSettings,
112-
function (viewport, scope) {
124+
(viewport, scope) => {
113125

114126
scrollBottom(viewport, MAX);
115127
scrollTop(viewport);
116128

117-
var initialBottomHeight = getBottomPaddingHeight(viewport);
129+
const initialBottomHeight = getBottomPaddingHeight(viewport);
118130
removeItem(datasource, datasource.max);
119131
scope.adapter.applyUpdates(itemsCount, []);
120132
removeItem(datasource, datasource.max);
@@ -128,17 +140,17 @@ describe('uiScroll Paddings cache', function () {
128140
);
129141
});
130142

131-
it('should delete first row', function () {
132-
var datasource;
143+
it('should delete first row', () => {
144+
let datasource;
133145
inject(function(myResponsiveDatasource) {
134146
datasource = myResponsiveDatasource;
135147
});
136148
runTest(scrollSettings,
137-
function (viewport, scope) {
149+
(viewport, scope) => {
138150

139151
scrollBottom(viewport, MAX);
140152

141-
var initialTopHeight = getTopPaddingHeight(viewport);
153+
const initialTopHeight = getTopPaddingHeight(viewport);
142154
removeItem(datasource, datasource.min);
143155
scope.adapter.applyUpdates(1, []);
144156
expect(getTopPaddingHeight(viewport)).toBe(initialTopHeight - itemHeight);
@@ -150,17 +162,17 @@ describe('uiScroll Paddings cache', function () {
150162
);
151163
});
152164

153-
it('should delete first row and then the next after first', function () {
154-
var datasource;
165+
it('should delete first row and then the next after first', () => {
166+
let datasource;
155167
inject(function(myResponsiveDatasource) {
156168
datasource = myResponsiveDatasource;
157169
});
158170
runTest(scrollSettings,
159-
function (viewport, scope) {
171+
(viewport, scope) => {
160172

161173
scrollBottom(viewport, MAX);
162174

163-
var initialTopHeight = getTopPaddingHeight(viewport);
175+
const initialTopHeight = getTopPaddingHeight(viewport);
164176
removeItem(datasource, datasource.min);
165177
scope.adapter.applyUpdates(1, []);
166178
removeItem(datasource, datasource.min);
@@ -176,15 +188,15 @@ describe('uiScroll Paddings cache', function () {
176188

177189
});
178190

179-
describe('removing inside the buffer\n', function () {
191+
describe('removing inside the buffer\n', () => {
180192

181-
it('should delete second row via index-based applyUpdates', function () {
182-
var datasource;
193+
it('should delete second row via index-based applyUpdates', () => {
194+
let datasource;
183195
inject(function(myResponsiveDatasource) {
184196
datasource = myResponsiveDatasource;
185197
});
186198
runTest(scrollSettings,
187-
function (viewport, scope) {
199+
(viewport, scope) => {
188200

189201
removeItem(datasource, datasource.min + 1);
190202
scope.adapter.applyUpdates(2, []);
@@ -202,20 +214,16 @@ describe('uiScroll Paddings cache', function () {
202214
);
203215
});
204216

205-
it('should delete second row via function-based applyUpdates', function () {
206-
var datasource;
217+
it('should delete second row via function-based applyUpdates', () => {
218+
let datasource;
207219
inject(function(myResponsiveDatasource) {
208220
datasource = myResponsiveDatasource;
209221
});
210222
runTest(scrollSettings,
211-
function (viewport, scope) {
223+
(viewport, scope) => {
212224

213225
removeItem(datasource, datasource.min + 1);
214-
scope.adapter.applyUpdates(function(item) {
215-
if(item === 'item2') {
216-
return [];
217-
}
218-
});
226+
scope.adapter.applyUpdates(item => item === 'item2' ? [] : null);
219227

220228
checkRow(viewport, 1, '1: item1');
221229
checkRow(viewport, 2, '2: item3');

test/misc/scaffolding.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ function createHtml(settings) {
88
var disabled = settings.disabled ? ' disabled="' + settings.disabled + '"' : '';
99
var adapter = settings.adapter ? ' adapter="' + settings.adapter + '"' : '';
1010
var template = settings.template ? settings.template : '{{$index}}: {{item}}';
11+
var startIndex = settings.startIndex ? ' start-index="' + settings.startIndex + '"' : '';
1112
var extra = settings.extra || '';
1213
return '<div ui-scroll-viewport' + viewportStyle + '>' +
1314
(settings.wrapper ? settings.wrapper.start : '') +
1415
'<div class="item" ui-scroll="item in ' + settings.datasource + '"' +
1516
adapter +
16-
itemStyle + bufferSize + padding + isLoading + topVisible + disabled + extra + '>' +
17+
itemStyle + bufferSize + padding + isLoading + topVisible + disabled + startIndex + extra + '>' +
1718
template +
1819
'</div>' +
1920
(settings.wrapper ? settings.wrapper.end : '') +

0 commit comments

Comments
 (0)