Skip to content

Commit 0fb976b

Browse files
committed
visibility switching spec refactoring: ES6 + timeout reducing
1 parent 238d6d6 commit 0fb976b

File tree

1 file changed

+61
-110
lines changed

1 file changed

+61
-110
lines changed

test/VisibilitySwitchingSpec.js

Lines changed: 61 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,179 +1,130 @@
11
/*global describe, beforeEach, module, it, expect, runTest */
2-
describe('uiScroll visibility. ', function() {
2+
describe('uiScroll visibility.', () => {
33
'use strict';
44

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

8-
var getScrollSettings = function() {
9-
return {
10-
datasource: 'myMultipageDatasource',
11-
viewportHeight: 200,
12-
itemHeight: 40,
13-
bufferSize: 3,
14-
adapter: 'adapter'
15-
};
8+
const scrollSettings = {
9+
datasource: 'myMultipageDatasource',
10+
viewportHeight: 200,
11+
itemHeight: 40,
12+
bufferSize: 3,
13+
adapter: 'adapter'
1614
};
1715

18-
var checkContent = function(rows, count) {
16+
const checkContent = (rows, count) => {
17+
expect(rows.length).toBe(count);
1918
for (var i = 1; i < count - 1; i++) {
20-
var row = rows[i];
21-
expect(row.tagName.toLowerCase()).toBe('div');
22-
expect(row.innerHTML).toBe(i + ': item' + i);
19+
expect(rows[i].innerHTML).toBe(i + ': item' + i);
2320
}
2421
};
2522

26-
describe('Viewport visibility changing. ', function() {
27-
var onePackItemsCount = 3 + 2;
28-
var twoPacksItemsCount = 3 * 3 + 2;
23+
const onePackItemsCount = 3 * 1 + 2;
24+
const twoPacksItemsCount = 3 * 2 + 2;
25+
const threePacksItemsCount = 3 * 3 + 2;
2926

30-
it('Should create 9 divs with data (+ 2 padding divs).', function() {
31-
runTest(getScrollSettings(),
32-
function(viewport) {
33-
expect(viewport.children().length).toBe(twoPacksItemsCount);
27+
describe('Viewport visibility changing\n', () => {
28+
29+
it('should create 9 divs with data (+ 2 padding divs)', () =>
30+
runTest(scrollSettings,
31+
(viewport) => {
3432
expect(viewport.scrollTop()).toBe(0);
35-
expect(viewport.children().css('height')).toBe('0px');
36-
expect(angular.element(viewport.children()[twoPacksItemsCount - 1]).css('height')).toBe('0px');
37-
checkContent(viewport.children(), twoPacksItemsCount);
33+
checkContent(viewport.children(), threePacksItemsCount);
3834
}
39-
);
40-
});
35+
)
36+
);
4137

42-
it('Should preserve elements after visibility switched off (display:none).', function() {
43-
runTest(getScrollSettings(),
44-
function(viewport, scope) {
38+
it('should preserve elements after visibility switched off (display:none)', () =>
39+
runTest(scrollSettings,
40+
(viewport, scope) => {
4541
viewport.css('display', 'none');
4642
scope.$apply();
4743

48-
expect(viewport.children().length).toBe(twoPacksItemsCount);
4944
expect(viewport.scrollTop()).toBe(0);
50-
expect(viewport.children().css('height')).toBe('0px');
51-
expect(angular.element(viewport.children()[twoPacksItemsCount - 1]).css('height')).toBe('0px');
52-
checkContent(viewport.children(), twoPacksItemsCount);
45+
checkContent(viewport.children(), threePacksItemsCount);
5346
}
54-
);
55-
});
56-
47+
)
48+
);
5749

58-
it('Should only load one batch with visibility switched off (display:none).', function() {
59-
runTest(getScrollSettings(),
60-
function(viewport, scope) {
50+
it('should only load one batch with visibility switched off (display:none)', () =>
51+
runTest(scrollSettings,
52+
(viewport, scope) => {
6153
viewport.css('display', 'none');
6254
scope.adapter.reload();
6355

64-
expect(viewport.children().length).toBe(onePackItemsCount);
6556
expect(viewport.scrollTop()).toBe(0);
66-
expect(viewport.children().css('height')).toBe('0px');
67-
expect(angular.element(viewport.children()[onePackItemsCount - 1]).css('height')).toBe('0px');
6857
checkContent(viewport.children(), onePackItemsCount);
6958
}
70-
);
71-
});
59+
)
60+
);
7261

73-
it('Should load full set after css-visibility switched back on.', function() {
74-
var scrollSettings = getScrollSettings();
62+
it('should load full set after css-visibility switched back on', () =>
7563
runTest(scrollSettings,
76-
function(viewport, scope, $timeout) {
64+
(viewport, scope, $timeout) => {
7765
viewport.css('display', 'none');
7866
scope.adapter.reload();
7967

8068
viewport.css('display', 'block');
8169
scope.$apply();
8270
$timeout.flush();
8371

84-
let rows = viewport.children();
85-
expect(rows.length).toBe(twoPacksItemsCount);
8672
expect(viewport.scrollTop()).toBe(0);
87-
expect(rows.css('height')).toBe('0px');
88-
expect(angular.element(rows[twoPacksItemsCount - 1]).css('height')).toBe('0px');
89-
checkContent(rows, onePackItemsCount);
73+
checkContent(viewport.children(), threePacksItemsCount);
74+
expect(scope.adapter.topVisible).toBe('item1');
9075
}
91-
);
92-
});
93-
94-
it('Should load full set after scope-visibility switched back on.', function() {
95-
var scrollSettings = getScrollSettings();
96-
scrollSettings.wrapper = {
97-
start: '<div ng-if="show">',
98-
end: '</div>'
99-
};
100-
runTest(scrollSettings,
101-
function(viewport, scope, $timeout) {
76+
)
77+
);
78+
79+
it('should load full set after scope-visibility switched back on', () =>
80+
runTest(Object.assign({}, scrollSettings, {
81+
wrapper: {
82+
start: '<div ng-if="show">',
83+
end: '</div>'
84+
}
85+
}), (viewport, scope) => {
10286
scope.show = false;
103-
scope.adapter.reload();
10487
scope.$apply();
88+
expect(viewport.children().length).toBe(0);
10589

10690
scope.show = true;
10791
scope.$apply();
108-
$timeout.flush();
109-
110-
let rows = viewport.children().children();
111-
expect(rows.length).toBe(twoPacksItemsCount);
11292
expect(viewport.scrollTop()).toBe(0);
113-
expect(rows.css('height')).toBe('0px');
114-
expect(angular.element(rows[twoPacksItemsCount - 1]).css('height')).toBe('0px');
115-
checkContent(rows, onePackItemsCount);
93+
checkContent(viewport.children().children(), threePacksItemsCount);
11694
}, {
11795
scope: {
11896
show: true
11997
}
12098
}
121-
);
122-
});
123-
124-
it('Should stay on the 1st item after the visibility is on (infinite list).', function() {
125-
var scrollSettings = getScrollSettings();
126-
scrollSettings.datasource = 'myInfiniteDatasource';
127-
scrollSettings.topVisible = 'topVisible';
128-
runTest(scrollSettings,
129-
function(viewport, scope, $timeout) {
130-
viewport.css('display', 'none');
131-
scope.adapter.reload();
132-
scope.$apply();
133-
134-
viewport.css('display', 'block');
135-
scope.$apply();
136-
$timeout.flush();
137-
138-
expect(scope.topVisible).toBe('item1');
139-
}
140-
);
141-
});
99+
)
100+
);
142101
});
143102

144-
describe('Items visibility changing. ', function() {
145-
var scrollSettings = getScrollSettings();
146-
scrollSettings.itemHeight = '0';
147-
var onePackItemsCount = 3 + 2;
148-
var twoPacksItemsCount = 3 * 2 + 2;
149-
150-
it('Should load only one batch with items height = 0', function() {
151-
runTest(scrollSettings,
152-
function(viewport) {
103+
describe('Items visibility changing\n', () => {
153104

105+
it('should load only one batch with items height = 0', () =>
106+
runTest(Object.assign({}, scrollSettings, { itemHeight: '0' }),
107+
(viewport) => {
154108
expect(viewport.children().length).toBe(onePackItemsCount);
155109
expect(viewport.scrollTop()).toBe(0);
156110
checkContent(viewport.children(), onePackItemsCount);
157111
}
158-
);
159-
});
160-
161-
it('Should continue loading after the height of some item switched to non-zero.', function() {
162-
runTest(scrollSettings,
163-
function(viewport, scope, $timeout) {
112+
)
113+
);
164114

115+
it('should load one more batch after the height of some item is set to a positive value', () =>
116+
runTest(Object.assign({}, scrollSettings, { itemHeight: '0' }),
117+
(viewport, scope, $timeout) => {
165118
angular.element(viewport.children()[onePackItemsCount - 2]).css('height', 40);
166119
expect(angular.element(viewport.children()[onePackItemsCount - 2]).css('height')).toBe('40px');
167120
scope.$apply();
168121
$timeout.flush();
169122

170-
expect(viewport.children().length).toBe(twoPacksItemsCount);
171123
expect(viewport.scrollTop()).toBe(0);
172124
checkContent(viewport.children(), twoPacksItemsCount);
173125
}
174-
);
175-
});
126+
)
127+
);
176128
});
177129

178-
179130
});

0 commit comments

Comments
 (0)