Skip to content

Commit 80562b9

Browse files
committed
edit logic on getIntegerNumber + new specs
1 parent 01d6f7c commit 80562b9

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

src/ui-scroll.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ angular.module('ui.scroll', [])
6161
}
6262

6363
function getIntegerNumber(value, defaultValue = 1) {
64-
return isNaN(value) ? defaultValue : Math.floor(value);
64+
value = value === null ? defaultValue : Math.floor(value);
65+
return isNaN(value) ? defaultValue : value;
6566
}
6667

6768
function parseNumericAttr(value, defaultValue) {

test/AdapterTestsSpec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,45 @@ describe('uiScroll', function () {
10961096
);
10971097
});
10981098

1099+
it('should round numbers for startIndex', () => {
1100+
runTest(scrollSettings,
1101+
function (viewport, scope) {
1102+
scope.adapter.reload(23.4);
1103+
expect(scope.adapter.topVisible).toBe('item23');
1104+
1105+
scope.adapter.reload(-56.9);
1106+
expect(scope.adapter.topVisible).toBe('item-57');
1107+
}
1108+
);
1109+
});
1110+
1111+
it('should correctly convert string to number and round', () => {
1112+
runTest(scrollSettings,
1113+
function (viewport, scope) {
1114+
scope.adapter.reload('1001.14');
1115+
expect(scope.adapter.topVisible).toBe('item1001');
1116+
1117+
scope.adapter.reload('0');
1118+
expect(scope.adapter.topVisible).toBe('item0');
1119+
}
1120+
);
1121+
});
1122+
1123+
it('should set startIndex to default if number is invalid', () => {
1124+
runTest(scrollSettings,
1125+
function (viewport, scope) {
1126+
scope.adapter.reload('invalid number');
1127+
expect(scope.adapter.topVisible).toBe('item1');
1128+
1129+
scope.adapter.reload({});
1130+
expect(scope.adapter.topVisible).toBe('item1');
1131+
1132+
scope.adapter.reload(null);
1133+
expect(scope.adapter.topVisible).toBe('item1');
1134+
}
1135+
);
1136+
});
1137+
10991138
});
11001139

11011140
describe('adapter bof/eof/empty', function () {

test/UserIndicesSpec.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,29 +115,6 @@ describe('uiScroll main/max indices', function() {
115115
);
116116
});
117117

118-
it('should round numbers for startIndex', () => {
119-
const startIndex = 22.4;
120-
runTest(Object.assign({}, scrollSettings, { startIndex }),
121-
(viewport, scope) => {
122-
expect(scope.adapter.topVisible).toBe('item' + Math.floor(startIndex));
123-
124-
const newStartIndex = 92.7;
125-
scope.adapter.reload(newStartIndex);
126-
expect(scope.adapter.topVisible).toBe('item' + Math.floor(newStartIndex));
127-
}
128-
);
129-
});
130-
131-
it('should validate startIndex argument on reload', () => {
132-
runTest(Object.assign({}, scrollSettings),
133-
(viewport, scope) => {
134-
const newStartIndex = 'invalid number';
135-
scope.adapter.reload(newStartIndex);
136-
expect(scope.adapter.topVisible).toBe('item1');
137-
}
138-
);
139-
});
140-
141118
});
142119

143120
describe('Reload\n', () => {

0 commit comments

Comments
 (0)