/
collectionRepeat.unit.js
465 lines (401 loc) · 15.2 KB
/
collectionRepeat.unit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
describe('collectionRepeat', function() {
var el;
beforeEach(module('ionic', function($provide) {
$provide.decorator('$$rAF', function($delegate) {
return function mockRaf(callback) { callback(); };
});
spyOn(ionic, 'animationFrameThrottle').andCallFake(function(cb) {
return function fakeThrottled() {
cb.apply(this, arguments);
};
});
spyOn(ionic, 'debounce').andCallFake(function(cb) { return cb; });
}));
var scrollView;
var repeaterScope;
var scrollCtrl;
function setup(listData, attrs, scrollViewData) {
var content = angular.element('<content>');
scrollView = angular.extend({
__content: content[0],
__clientHeight: 100,
__clientWidth: 50,
__scrollTop: 0,
__scrollLeft: 0,
__maxScrollTop: 100,
__maxScrollLeft: 50,
options: {
scrollingY: true
},
__callback: angular.noop,
resize: angular.noop,
setDimensions: angular.noop
}, scrollViewData || {});
scrollCtrl = {
scrollView: scrollView,
$element: content
};
var list = [];
if (angular.isNumber(listData)) {
for (var i = 0; i < listData; i++) list.push(i);
} else if (angular.isArray(listData)) {
list = listData;
}
var element;
inject(function($compile, $rootScope) {
repeaterScope = $rootScope.$new();
attrs = attrs || '';
if (!/item-height/.test(attrs)) attrs += ' item-height="25px"';
if (!/item-render-buffer/.test(attrs)) attrs += ' item-render-buffer="0"';
element = angular.element(
'<div collection-repeat="item in list" '+(attrs)+'>{{item}}</div>'
);
content.append(element);
content.data('$$ionicScrollController', scrollCtrl);
$rootScope.list = list;
$compile(element)(repeaterScope);
$rootScope.$apply();
});
return element;
}
afterEach(function() {
repeaterScope && repeaterScope.$destroy();
repeaterScope = null;
});
function scrollTo(n) {
if (scrollView.options.scrollingY) {
scrollView.__scrollTop = n;
scrollView.__maxScrollTop = scrollView.options.getContentHeight() - scrollView.__clientHeight;
} else {
scrollView.__scrollLeft = n;
scrollView.__maxScrollLeft = scrollView.options.getContentWidth() - scrollView.__clientWidth;
}
scrollView.__callback();
}
function getItems() {
return [].slice.call(scrollView.__content.querySelectorAll('[collection-repeat]'))
.map(function(node) {
return angular.element(node).data('$$collectionRepeatItem');
})
// make sure we didn't get anything that doesn't actually have the data
.filter(function(item) {
return !!item;
});
}
function activeItems() {
var items = getItems().filter(function(item) {
return item.isShown;
});
//1. Group items by their primary position (row),
//2. Sort those groups by secondary position (column),
//3. Concat them all together.
var itemsByPos = {};
items.forEach(function(item) {
(itemsByPos[item.primaryPos] || (itemsByPos[item.primaryPos] = [])).push(item);
});
var result = [];
Object.keys(itemsByPos)
.sort(function(pos1, pos2) {
return (+pos1) > (+pos2) ? 1 : -1;
})
.forEach(function(primaryPos) {
var sortedRow = itemsByPos[primaryPos].sort(function(a,b) {
return a.secondaryPos > b.secondaryPos ? 1 : -1;
});
result = result.concat(sortedRow);
});
return result;
}
function activeItemContents() {
return activeItems().map(function(item) {
return item.node.innerHTML.trim();
});
}
function activeItemIds() {
return activeItems().map(function(item) {
return item.id;
});
}
function activeItemDimensions() {
return activeItems().map(function(item) {
return scrollView.options.scrollingX ? (
'x:' + item.primaryPos + ',y:' + item.secondaryPos +
',w:' + item.primarySize + ',h:' + item.secondarySize
) : (
'x:' + item.secondaryPos + ',y:' + item.primaryPos +
',w:' + item.secondarySize + ',h:' + item.primarySize
);
});
}
it('should error with direction="xy" parent', function() {
expect(function() {
setup(10, '', {
options: { scrollingX: true, scrollingY: true }
});
}).toThrow();
});
it('should error without proper collection-repeat expression', inject(function($compile, $rootScope) {
expect(function() {
$compile('<ion-content>' +
'<div collection-repeat="bad"></div>' +
'</ion-content>')($rootScope);
}).toThrow();
}));
it('should destroy and restore normal scrollView behavior', inject(function($compile, $rootScope) {
var scope = $rootScope.$new();
var content = $compile('<ion-content>')(scope);
var scrollView = content.data('$$ionicScrollController').scrollView;
var originalCallback = scrollView.__callback;
var originalGetContentHeight = scrollView.options.getContentHeight;
var repeater = angular.element(
'<div collection-repeat="item in items" item-height="5" item-width="5"></div>'
);
content.append(repeater);
$compile(repeater)(content.scope());
$rootScope.$apply();
content.triggerHandler('scroll.init');
$rootScope.$apply();
expect(scrollView.__callback).not.toBe(originalCallback);
expect(scrollView.options.getContentHeight).not.toBe(originalGetContentHeight);
scope.$destroy();
expect(scrollView.__callback).toBe(originalCallback);
expect(scrollView.options.getContentHeight).toBe(originalGetContentHeight);
}));
describe('automatic dimensions', function() {
it('should use computed width/height', inject(function($window) {
spyOn($window, 'getComputedStyle').andReturn({
height: '50px',
width: '50px'
});
setup(5, 'item-height="" item-width=""', {
__clientHeight: 75,
__clientWidth: 100
});
expect(activeItems().length).toBe(4);
expect(activeItemDimensions()).toEqual([
'x:0,y:0,w:50,h:50',
'x:50,y:0,w:50,h:50',
'x:0,y:50,w:50,h:50',
'x:50,y:50,w:50,h:50'
]);
}));
it('should error if computed height is 0', inject(function($window) {
spyOn($window, 'getComputedStyle').andReturn({
height: '0px',
width: '100px'
});
expect(function() {
setup(5, 'item-height="" item-width=""');
}).toThrow();
}));
it('should error if computed width is 0', inject(function($window) {
spyOn($window, 'getComputedStyle').andReturn({
height: '100px',
width: '0px'
});
expect(function() {
setup(5, 'item-height="" item-width=""');
}).toThrow();
}));
it('should refresh layout on scrollCtrl.resize', inject(function($timeout, $window) {
var el = setup(10, 'item-height="20px"', {
__clientHeight: 50,
__clientWidth: 1
});
expect(activeItems().length).toEqual(3);
scrollView.__clientHeight = 30;
scrollCtrl.$element.triggerHandler('scroll.resize');
expect(activeItems().length).toEqual(2);
}));
});
describe('horizontal static list', function() {
function setupHorizontal() {
setup(10, 'item-height="100%" item-width="30"', {
options: {
scrollingX: true,
scrollingY: false
},
__clientWidth: 80,
__clientHeight: 25
});
}
it('should show initial screen of items', function() {
setupHorizontal();
expect(activeItems().length).toBe(3);
expect(activeItemContents()).toEqual(['0','1','2']);
});
it('should switch out as you scroll', function() {
setupHorizontal();
expect(activeItems().length).toBe(3);
expect(activeItemContents()).toEqual(['0','1','2']);
expect(activeItemIds()).toEqual(['item0','item1','item2']);
// Item 0 gets sent down to the bottom after scrolling past it
scrollTo(31);
expect(activeItems().length).toBe(3);
expect(activeItemContents()).toEqual(['1','2','3']);
expect(activeItemIds()).toEqual(['item1','item2','item0']);
// Item 1 gets sent down
scrollTo(61);
expect(activeItems().length).toBe(3);
expect(activeItemContents()).toEqual(['2','3','4']);
expect(activeItemIds()).toEqual(['item2','item0','item1']);
});
it('should start with the same items when resizing', inject(function($window) {
setupHorizontal();
scrollTo(31);
scrollTo(61);
expect(activeItems().length).toBe(3);
expect(activeItemContents()).toEqual(['2','3','4']);
expect(activeItemIds()).toEqual(['item2','item0','item1']);
scrollView.__clientWidth = 50;
scrollView.__clientHeight = 40;
angular.element($window).triggerHandler('resize');
expect(activeItems().length).toBe(2);
expect(activeItemContents()).toEqual(['2','3']);
expect(activeItemIds()).toEqual(['item2','item0']);
}));
});
describe('vertical static list', function() {
beforeEach(function() {
setup(10);
});
it('should show initial screen of items', function() {
expect(activeItems().length).toBe(5);
expect(activeItemContents()).toEqual(['0','1','2','3','4']);
});
it('should switch out as you scroll', function() {
expect(activeItems().length).toBe(5);
expect(activeItemContents()).toEqual(['0','1','2','3','4']);
expect(activeItemIds()).toEqual(['item0','item1','item2','item3','item4']);
// Item 0 gets sent down to the bottom after scrolling past it
scrollTo(26);
expect(activeItems().length).toBe(5);
expect(activeItemContents()).toEqual(['1','2','3','4','5']);
expect(activeItemIds()).toEqual(['item1','item2','item3','item4','item0']);
// Item 1 gets sent down
scrollTo(51);
expect(activeItems().length).toBe(5);
expect(activeItemContents()).toEqual(['2','3','4','5','6']);
expect(activeItemIds()).toEqual(['item2','item3','item4','item0','item1']);
// scroll to bottom incrementally
// items are traded our until it's the first case again
scrollTo(76);
scrollTo(101);
scrollTo(126);
expect(activeItems().length).toBe(5);
expect(activeItemContents()).toEqual(['5','6','7','8','9']);
expect(activeItemIds()).toEqual(['item0','item1','item2','item3','item4']);
});
it('should start with the same items when resizing', inject(function($window) {
scrollTo(26);
scrollTo(51);
expect(activeItems().length).toBe(5);
expect(activeItemContents()).toEqual(['2','3','4','5','6']);
expect(activeItemIds()).toEqual(['item2','item3','item4','item0','item1']);
scrollView.__clientWidth = 200;
scrollView.__clientHeight = 40;
angular.element($window).triggerHandler('resize');
expect(activeItems().length).toBe(2);
expect(activeItemContents()).toEqual(['2','3']);
expect(activeItemIds()).toEqual(['item2','item3']);
}));
});
describe('vertical static grid', function() {
beforeEach(function() {
setup(10, 'item-width="33%" item-height="25"', {
__clientWidth: 120,
__clientHeight: 30
});
});
it('should show initial screen of items', function() {
expect(activeItems().length).toBe(3 * 2);
expect(activeItemContents()).toEqual(['0','1','2','3','4','5']);
});
it('should switch out as you scroll', function() {
expect(activeItems().length).toBe(6);
expect(activeItemContents()).toEqual(['0','1','2','3','4','5']);
expect(activeItemIds().sort()).toEqual(['item0','item1','item2','item3','item4','item5']);
scrollTo(26);
expect(activeItems().length).toBe(6);
expect(activeItemContents()).toEqual(['3','4','5','6','7','8']);
expect(activeItemIds().sort()).toEqual(['item0','item1','item2','item3','item4','item5']);
scrollTo(51);
expect(activeItems().length).toBe(4);
expect(activeItemContents()).toEqual(['6','7','8','9']);
expect(activeItemIds().sort()).toEqual(['item0','item1','item2','item5']);
});
it('should start with the same items when resizing', inject(function($window) {
scrollTo(26);
expect(activeItems().length).toBe(6);
expect(activeItemContents()).toEqual(['3','4','5','6','7','8']);
expect(activeItemIds().sort()).toEqual(['item0','item1','item2','item3','item4','item5']);
scrollView.__clientWidth = 200;
scrollView.__clientHeight = 20;
angular.element($window).triggerHandler('resize');
expect(activeItems().length).toBe(3);
expect(activeItemContents()).toEqual(['3','4','5']);
expect(activeItemIds().sort()).toEqual(['item3','item4','item5']);
}));
});
describe('vertical dynamic grid', function() {
beforeEach(function() {
// Odd rows 25 height, even rows 50 height
setup(10, 'item-width="(16 * (1+($index % 5))) + \'%\'" ' +
'item-height="($index % 2) ? 25 : 50"', {
__clientWidth: 100,
__clientHeight: 60
});
});
it('should show initial screen of items', function() {
// row 0, index 0: 50 height, 3 items (widths 16%, 32%, 48%)
// row 1, index 3: 25 height, 2 items (widths 64%)
expect(activeItems().length).toBe(4);
expect(activeItemContents()).toEqual(['0','1','2','3']);
var dim = activeItemDimensions();
//Row 0
expect(dim[0]).toBe('x:0,y:0,w:16,h:50');
expect(dim[1]).toBe('x:16,y:0,w:32,h:25');
expect(dim[2]).toBe('x:48,y:0,w:48,h:50');
// Row 1
expect(dim[3]).toBe('x:0,y:50,w:64,h:25');
});
it('should switch out as you scroll', function() {
// Scroll past row 0, height 50
scrollTo(51);
// row 1, index 3: 25 height, 1 item (width 64%)
// row 2, index 4: 50 height, 2 items (width 80%, 16%)
expect(activeItems().length).toBe(3);
expect(activeItemContents()).toEqual(['3','4','5']);
expect(activeItemIds().sort()).toEqual(['item1','item2','item3']);
var dim = activeItemDimensions();
//Row 1
expect(dim[0]).toBe('x:0,y:50,w:64,h:25');
//Row 2
expect(dim[1]).toBe('x:0,y:75,w:80,h:50');
expect(dim[2]).toBe('x:80,y:75,w:16,h:25');
// row 3, index 6: 50 height, 2 items (width 32%, 48%)
//Scroll past row 2 and row 3 to the end
scrollTo(176);
expect(activeItems().length).toBe(2);
expect(activeItemContents()).toEqual(['8','9']);
dim = activeItemDimensions();
expect(dim[0]).toBe('x:0,y:175,w:64,h:50');
expect(dim[1]).toBe('x:0,y:225,w:80,h:25');
});
it('should start with the same items when resizing', inject(function($window) {
// Scroll past row 0, height 50
scrollTo(51);
// row 1, index 3: 25 height, 1 item (width 64%)
// row 2, index 4: 50 height, 2 items (width 80%, 16%)
expect(activeItems().length).toBe(3);
expect(activeItemContents()).toEqual(['3','4','5']);
expect(activeItemIds().sort()).toEqual(['item1','item2','item3']);
scrollView.__clientWidth = 50;
scrollView.__clientHeight = 50;
angular.element($window).triggerHandler('resize');
expect(activeItems().length).toBe(3);
expect(activeItemContents()).toEqual(['3','4','5']);
expect(activeItemIds().sort()).toEqual(['item1','item2','item3']);
}));
});
});