Skip to content

Commit cebc88f

Browse files
committed
extract BufferCleanupSpec
1 parent 9f431e0 commit cebc88f

File tree

2 files changed

+113
-99
lines changed

2 files changed

+113
-99
lines changed

test/AdapterTestsSpec.js

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,105 +1607,6 @@ describe('uiScroll', function () {
16071607
}
16081608
);
16091609
});
1610-
1611-
});
1612-
1613-
describe('buffer cleanup', function () {
1614-
var getSettings = function () {
1615-
return {
1616-
datasource: 'myEdgeDatasource',
1617-
adapter: 'adapter',
1618-
viewportHeight: 60,
1619-
itemHeight: 20,
1620-
padding: 0.3,
1621-
startIndex: 3,
1622-
bufferSize: 3
1623-
};
1624-
};
1625-
1626-
injectDatasource('myEdgeDatasource');
1627-
1628-
var cleanBuffer = function (scope, applyUpdateOptions) {
1629-
var get = datasource.get;
1630-
var removedItems = [];
1631-
// sync the datasource
1632-
datasource.get = function (index, count, success) {
1633-
var removedIndex = removedItems.indexOf('item' + index);
1634-
if (removedIndex !== -1) {
1635-
// todo consider mutable-top case
1636-
index += removedItems.length - removedIndex;
1637-
}
1638-
get(index, count, success);
1639-
};
1640-
// clean up the buffer
1641-
scope.adapter.applyUpdates(function (item) {
1642-
removedItems.push(item);
1643-
return [];
1644-
}, applyUpdateOptions);
1645-
};
1646-
1647-
it('should be consistent on forward direction when eof with immutabeTop', function () {
1648-
runTest(getSettings(),
1649-
function (viewport, scope) {
1650-
expect(scope.adapter.isBOF()).toBe(false);
1651-
expect(scope.adapter.isEOF()).toBe(true);
1652-
1653-
// remove items 0..6 items form -5..6 datasource
1654-
cleanBuffer(scope, { immutableTop: true });
1655-
1656-
// result [-5..-1]
1657-
expect(scope.adapter.isBOF()).toBe(true);
1658-
expect(scope.adapter.isEOF()).toBe(true);
1659-
expect(scope.adapter.bufferFirst).toBe('item-5');
1660-
expect(scope.adapter.bufferLast).toBe('item-1');
1661-
expect(scope.adapter.bufferLength).toBe(5);
1662-
}
1663-
);
1664-
});
1665-
1666-
it('should be consistent on forward direction when not eof with immutabeTop', function () {
1667-
var scrollSettings = getSettings();
1668-
scrollSettings.startIndex = -1;
1669-
scrollSettings.viewportHeight = 40;
1670-
runTest(scrollSettings,
1671-
function (viewport, scope) {
1672-
expect(scope.adapter.isBOF()).toBe(false);
1673-
expect(scope.adapter.isEOF()).toBe(false);
1674-
1675-
// remove items -4..1 items form -5..6 datasource
1676-
cleanBuffer(scope, { immutableTop: true });
1677-
1678-
// result [-5, 2, 3, 4]
1679-
expect(scope.adapter.isBOF()).toBe(true);
1680-
expect(scope.adapter.isEOF()).toBe(false);
1681-
expect(scope.adapter.bufferFirst).toBe('item-5');
1682-
expect(scope.adapter.bufferLast).toBe('item4');
1683-
expect(scope.adapter.bufferLength).toBe(4);
1684-
}
1685-
);
1686-
});
1687-
1688-
it('should be consistent on backward direction when bof without immutableTop', function () {
1689-
var scrollSettings = getSettings();
1690-
scrollSettings.startIndex = -3;
1691-
scrollSettings.padding = 0.5;
1692-
runTest(scrollSettings,
1693-
function (viewport, scope) {
1694-
expect(scope.adapter.isBOF()).toBe(true);
1695-
expect(scope.adapter.isEOF()).toBe(false);
1696-
1697-
// remove items -5..1 items form -5..6 datasource
1698-
cleanBuffer(scope);
1699-
1700-
expect(scope.adapter.isBOF()).toBe(true);
1701-
expect(scope.adapter.isEOF()).toBe(true);
1702-
expect(scope.adapter.bufferFirst).toBe('item2');
1703-
expect(scope.adapter.bufferLast).toBe('item6');
1704-
expect(scope.adapter.bufferLength).toBe(5);
1705-
}
1706-
);
1707-
});
1708-
17091610
});
17101611

17111612
});

test/BufferCleanupSpec.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
describe('uiScroll', function () {
2+
'use strict';
3+
4+
let datasource;
5+
beforeEach(module('ui.scroll'));
6+
beforeEach(module('ui.scroll.test.datasources'));
7+
8+
const injectDatasource = (datasourceToken) =>
9+
beforeEach(
10+
inject([datasourceToken, function (_datasource) {
11+
datasource = _datasource;
12+
}])
13+
);
14+
15+
describe('buffer cleanup', function () {
16+
var getSettings = function () {
17+
return {
18+
datasource: 'myEdgeDatasource',
19+
adapter: 'adapter',
20+
viewportHeight: 60,
21+
itemHeight: 20,
22+
padding: 0.3,
23+
startIndex: 3,
24+
bufferSize: 3
25+
};
26+
};
27+
28+
injectDatasource('myEdgeDatasource');
29+
30+
var cleanBuffer = function (scope, applyUpdateOptions) {
31+
var get = datasource.get;
32+
var removedItems = [];
33+
// sync the datasource
34+
datasource.get = function (index, count, success) {
35+
var removedIndex = removedItems.indexOf('item' + index);
36+
if (removedIndex !== -1) {
37+
// todo consider mutable-top case
38+
index += removedItems.length;// - removedIndex;
39+
}
40+
get(index, count, success);
41+
};
42+
// clean up the buffer
43+
scope.adapter.applyUpdates(function (item) {
44+
removedItems.push(item);
45+
return [];
46+
}, applyUpdateOptions);
47+
};
48+
49+
it('should be consistent on forward direction when eof with immutabeTop', function () {
50+
runTest(getSettings(),
51+
function (viewport, scope) {
52+
expect(scope.adapter.isBOF()).toBe(false);
53+
expect(scope.adapter.isEOF()).toBe(true);
54+
55+
// remove items 0..6 items form -5..6 datasource
56+
cleanBuffer(scope, { immutableTop: true });
57+
58+
// result [-5..-1]
59+
expect(scope.adapter.isBOF()).toBe(true);
60+
expect(scope.adapter.isEOF()).toBe(true);
61+
expect(scope.adapter.bufferFirst).toBe('item-5');
62+
expect(scope.adapter.bufferLast).toBe('item-1');
63+
expect(scope.adapter.bufferLength).toBe(5);
64+
}
65+
);
66+
});
67+
68+
it('should be consistent on forward direction when not eof with immutabeTop', function () {
69+
var scrollSettings = getSettings();
70+
scrollSettings.startIndex = -1;
71+
scrollSettings.viewportHeight = 40;
72+
runTest(scrollSettings,
73+
function (viewport, scope) {
74+
expect(scope.adapter.isBOF()).toBe(false);
75+
expect(scope.adapter.isEOF()).toBe(false);
76+
77+
// remove items -4..1 items form -5..6 datasource
78+
cleanBuffer(scope, { immutableTop: true });
79+
80+
// result [-5, 2, 3, 4]
81+
expect(scope.adapter.isBOF()).toBe(true);
82+
expect(scope.adapter.isEOF()).toBe(false);
83+
expect(scope.adapter.bufferFirst).toBe('item-5');
84+
expect(scope.adapter.bufferLast).toBe('item4');
85+
expect(scope.adapter.bufferLength).toBe(4);
86+
}
87+
);
88+
});
89+
90+
it('should be consistent on backward direction when bof without immutableTop', function () {
91+
var scrollSettings = getSettings();
92+
scrollSettings.startIndex = -3;
93+
scrollSettings.padding = 0.5;
94+
runTest(scrollSettings,
95+
function (viewport, scope) {
96+
expect(scope.adapter.isBOF()).toBe(true);
97+
expect(scope.adapter.isEOF()).toBe(false);
98+
99+
// remove items -5..1 items form -5..6 datasource
100+
cleanBuffer(scope);
101+
102+
// result [2..6]
103+
expect(scope.adapter.isBOF()).toBe(true);
104+
expect(scope.adapter.isEOF()).toBe(true);
105+
expect(scope.adapter.bufferFirst).toBe('item2');
106+
expect(scope.adapter.bufferLast).toBe('item6');
107+
expect(scope.adapter.bufferLength).toBe(5);
108+
}
109+
);
110+
});
111+
});
112+
113+
});

0 commit comments

Comments
 (0)