-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirPagination.spec.js
903 lines (725 loc) · 37.3 KB
/
dirPagination.spec.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/**
* Created by Michael on 04/05/14.
*/
describe('dirPagination directive', function() {
var $compile;
var $scope;
var $timeout;
var containingElement;
var myCollection;
beforeEach(module('angularUtils.directives.dirPagination'));
beforeEach(module('templates-main'));
beforeEach(inject(function($rootScope, _$compile_, _$timeout_) {
$compile = _$compile_;
$timeout = _$timeout_;
$scope = $rootScope.$new();
containingElement = angular.element('<div></div>');
myCollection = [];
for(var i = 1; i <= 100; i++) {
myCollection.push('item ' + i);
}
}));
function compileElement(collection, itemsPerPage, currentPage, customExpression, totalItems) {
var expression;
var totalItemsHtml;
var html;
$scope.collection = collection;
$scope.itemsPerPage = itemsPerPage;
$scope.currentPage = currentPage || 1;
$scope.totalItems = totalItems || undefined;
totalItemsHtml = (typeof totalItems !== 'undefined') ? 'total-items="totalItems"' : '';
expression = customExpression || "item in collection | itemsPerPage: itemsPerPage";
html = '<ul class="list"><li dir-paginate="'+ expression + '" current-page="currentPage" ' + totalItemsHtml + ' >{{ item }}</li></ul> ' +
'<dir-pagination-controls></dir-pagination-controls>';
containingElement.append($compile(html)($scope));
$scope.$apply();
}
function getListItems() {
return containingElement.find('ul.list li').map(function() {
return $(this).text().trim();
}).get();
}
function getPageLinksArray() {
return containingElement.find('ul.pagination li').map(function() {
return $(this).text().trim();
}).get();
}
describe('paginated list', function() {
it('should throw an exception if itemsPerPage filter not set', function() {
function compile() {
var customExpression = "item in collection";
compileElement(myCollection, 5, 1, customExpression);
}
expect(compile).toThrow("pagination directive: the 'itemsPerPage' filter must be set.");
});
it('should allow a space after itemsPerPage and before the colon', function() {
function compile() {
var customExpression = "item in collection | itemsPerPage : 10";
compileElement(myCollection, 5, 1, customExpression);
}
expect(compile).not.toThrow();
});
it('should repeat the items like ng-repeat', function() {
compileElement(myCollection);
var listItems = getListItems();
expect(listItems.length).toBe(100);
});
it('should limit the items to match itemsPerPage = 10', function() {
var listItems;
compileElement(myCollection, 10);
listItems = getListItems();
expect(listItems.length).toBe(10);
});
it('should limit the items to match itemsPerPage = 50', function() {
var listItems;
compileElement(myCollection, 50);
listItems = getListItems();
expect(listItems.length).toBe(50);
});
it('should not mutate the collection itself ', function() {
compileElement(myCollection);
expect($scope.collection.length).toBe(100);
compileElement(myCollection, 50);
expect($scope.collection.length).toBe(100);
compileElement(myCollection, 5);
expect($scope.collection.length).toBe(100);
});
it('should work correctly with other filters (filter)', function() {
$scope.filterBy = '2';
var customExpression = "item in collection | filter: filterBy | itemsPerPage: itemsPerPage";
compileElement(myCollection, 5, 1, customExpression);
var listItems = getListItems();
expect(listItems).toEqual(['item 2', 'item 12', 'item 20', 'item 21', 'item 22']);
});
it('should work correctly with other filters (orderBy)', function() {
var customExpression = "item in collection | orderBy:'toString()':true | itemsPerPage: itemsPerPage";
compileElement(myCollection, 5, 1, customExpression);
var listItems = getListItems();
expect(listItems).toEqual(['item 99', 'item 98', 'item 97', 'item 96', 'item 95']);
});
it('should allow parentheses around the itemsPerPage filter', function() {
var customExpression = "item in filtered = (collection | filter: '1' | itemsPerPage: itemsPerPage)";
compileElement(myCollection, 5, 1, customExpression);
var listItems = getListItems();
expect(listItems).toEqual(['item 1', 'item 10', 'item 11', 'item 12', 'item 13']);
expect($scope.filtered.length).toEqual(5);
});
it('should work inside a transcluded directive (ng-if)', function() {
$scope.collection = myCollection;
var html = '<div ng-if="true">' +
'<ul class="list"><li dir-paginate="item in collection | itemsPerPage: 5">{{ item }}</li></ul> ' +
'<dir-pagination-controls></dir-pagination-controls>' +
'</div>';
containingElement.append($compile(html)($scope));
$scope.$apply();
var listItems = getListItems();
expect(listItems).toEqual(['item 1', 'item 2', 'item 3', 'item 4', 'item 5']);
});
it('should display the second page when compiled with currentPage = 2', function() {
var listItems;
compileElement(myCollection, 3, 2);
listItems = getListItems();
expect(listItems).toEqual(['item 4', 'item 5', 'item 6']);
});
it('should display the next page when the currentPage changes', function() {
var listItems;
compileElement(myCollection, 3);
listItems = getListItems();
expect(listItems).toEqual(['item 1', 'item 2', 'item 3']);
$scope.$apply(function() {
$scope.currentPage = 2;
});
listItems = getListItems();
expect(listItems).toEqual(['item 4', 'item 5', 'item 6']);
$scope.$apply(function() {
$scope.currentPage = 3;
});
listItems = getListItems();
expect(listItems).toEqual(['item 7', 'item 8', 'item 9']);
});
it('should work if itemsPerPage is a literal value', function() {
var customExpression = "item in collection | itemsPerPage: 2";
compileElement(myCollection, null, 1, customExpression);
var listItems = getListItems();
expect(listItems.length).toEqual(2);
expect(listItems).toEqual(['item 1', 'item 2']);
});
});
describe('if currentPage attribute is not set', function() {
beforeEach(function() {
$scope.collection = myCollection;
html = '<ul class="list"><li dir-paginate="item in collection | itemsPerPage: 3">{{ item }}</li></ul> ' +
'<dir-pagination-controls></dir-pagination-controls>';
containingElement.append($compile(html)($scope));
$scope.$apply();
});
it('should compile', function() {
var listItems = getListItems();
expect(listItems).toEqual(['item 1', 'item 2', 'item 3']);
});
it('should page correctly', function() {
var pagination = containingElement.find('ul.pagination');
pagination.children().eq(3).find('a').triggerHandler('click');
$scope.$apply();
expect($scope.__default__currentPage).toBe(3);
var listItems = getListItems();
expect(listItems).toEqual(['item 7', 'item 8', 'item 9']);
});
});
describe('pagination controls', function() {
beforeEach(function(){
spyOn(console, 'warn');
});
it('should throw a warning if the dir-paginate directive has not been set up', function() {
function compile() {
var html = '<dir-pagination-controls></dir-pagination-controls>';
containingElement.append($compile(html)($scope));
$scope.$apply();
}
compile();
expect(console.warn).toHaveBeenCalledWith('Pagination directive: the pagination controls cannot be used without the corresponding pagination directive, which was not found at link time.');
});
it('should not display pagination if all rows fit on one page', function() {
compileElement(myCollection, 9999);
var paginationLinks = getPageLinksArray();
expect(paginationLinks.length).toBe(0);
});
it('should paginate by default if all items do not fit on page', function() {
compileElement(myCollection, 40);
var paginationLinks = getPageLinksArray();
expect(paginationLinks).toEqual(['‹','1', '2', '3', '›']);
});
it('should update the currentPage property of $scope when links clicked', function() {
compileElement(myCollection, 40);
var pagination = containingElement.find('ul.pagination');
pagination.children().eq(3).find('a').triggerHandler('click');
$scope.$apply();
expect($scope.currentPage).toBe(3);
pagination.children().eq(2).find('a').triggerHandler('click');
$scope.$apply();
expect($scope.currentPage).toBe(2);
pagination.children().eq(1).find('a').triggerHandler('click');
$scope.$apply();
expect($scope.currentPage).toBe(1);
});
it('should set the pagination.current value to 5 when compiled with currentPage = 5', function() {
compileElement(myCollection, 3, 5);
var activePageItem = containingElement.find('li.active').eq(0);
var activePage = activePageItem.text().trim();
expect(activePage).toEqual('5');
});
it('should show the correct pagination links at start of sequence', function() {
compileElement(myCollection, 1);
var pageLinks = getPageLinksArray();
expect(pageLinks).toEqual(['‹','1', '2', '3', '4', '5', '6', '7', '...', '100', '›']);
});
it('should show the correct pagination links in middle sequence', function() {
compileElement(myCollection, 1);
$scope.$apply(function() {
$scope.currentPage = 50;
});
var pageLinks = getPageLinksArray();
expect(pageLinks).toEqual(['‹','1', '...', '48', '49', '50', '51', '52', '...', '100', '›']);
});
it('should show the correct pagination links at end of sequence', function() {
compileElement(myCollection, 1);
$scope.$apply(function() {
$scope.currentPage = 99;
});
var pageLinks = getPageLinksArray();
expect(pageLinks).toEqual(['‹','1', '...', '94', '95', '96', '97', '98', '99', '100', '›']);
});
it('should show the correct pagination links after item removed from cllection', function() {
compileElement(myCollection, 1);
$scope.$apply(function() {
$scope.currentPage = 98;
});
$scope.$apply(function() {
$scope.collection.pop();
});
var pageLinks = getPageLinksArray();
expect(pageLinks).toEqual(['‹','1', '...', '93', '94', '95', '96', '97', '98', '99', '›']);
});
it('should calculate pages based off collection after all filters are applied', function() {
$scope.filterBy = '2';
var customExpression = "item in collection | filter: filterBy | itemsPerPage: itemsPerPage";
compileElement(myCollection, 5, 1, customExpression);
var pageLinks = getPageLinksArray();
expect(pageLinks.length).toEqual(6);
});
it('should update the active page to reflect the value of the current-page property', function() {
compileElement(myCollection, 10, 3);
var activeLink = containingElement.find('ul.pagination li.active');
expect(activeLink.html()).toContain(3);
$scope.$apply(function() {
$scope.currentPage = 1;
});
activeLink = containingElement.find('ul.pagination li.active');
expect(activeLink.html()).toContain(1);
});
/**
* Issue raised here https://github.com/michaelbromley/angularUtils/issues/78
* Where the dir-paginate directive is inside an ngSwitch block (which is initially hidden), so the linking function is not immediately executed.
* The dir-pagination-controls directive is *outside* the switch block, so it gets both compiled *and* linked on page load.
*/
it('should allow paginate directive to be defined in a deferred-linking situation without error', function() {
function compile() {
var html;
$scope.collection = myCollection;
$scope.showList = false;
html = '<div ng-if="showList">' +
'<ul class="list"><li dir-paginate="item in collection | itemsPerPage: 10">{{ item }}</li></ul> ' +
'</div>' +
'<dir-pagination-controls></dir-pagination-controls>';
containingElement.append($compile(html)($scope));
$scope.$apply();
}
expect(compile).not.toThrow();
expect(getListItems().length).toEqual(0);
$scope.$apply(function() {
$scope.showList = true;
});
expect(getListItems().length).toEqual(10);
});
describe('optional attributes', function() {
function compileWithAttributes(attributes) {
$scope.collection = myCollection;
$scope.currentPage = 1;
var html = '<ul class="list"><li dir-paginate="item in collection | itemsPerPage: 10" current-page="currentPage">{{ item }}</li></ul> ' +
'<dir-pagination-controls ' + attributes + ' ></dir-pagination-controls>';
containingElement.append($compile(html)($scope));
$scope.$apply();
}
it('should accept a max-size attribute to limit the length of the control', function() {
compileWithAttributes(' max-size="5" ');
var pageLinks = getPageLinksArray();
expect(pageLinks).toEqual(['‹','1', '2', '3', '...', '10', '›']);
});
it('should impose a minimum max-size of 5', function() {
compileWithAttributes(' max-size="2" ');
var pageLinks = getPageLinksArray();
expect(pageLinks).toEqual(['‹','1', '2', '3', '...', '10', '›']);
});
it('should go to the last page when clicking the end arrow', function() {
compileWithAttributes(' boundary-links="true" ');
var pagination = containingElement.find('ul.pagination');
pagination.children().eq(10).find('a').triggerHandler('click');
$scope.$apply();
expect($scope.currentPage).toBe(10);
});
it('should go to the first page when clicking the end arrow', function() {
compileWithAttributes(' boundary-links="true" ');
var pagination = containingElement.find('ul.pagination');
$scope.$apply(function() {
$scope.currentPage = 5;
});
expect($scope.currentPage).toBe(5);
pagination.children().eq(0).find('a').triggerHandler('click');
$scope.$apply();
expect($scope.currentPage).toBe(1);
});
it('should page forward', function() {
compileWithAttributes(' ');
var pagination = containingElement.find('ul.pagination');
pagination.children().eq(10).find('a').triggerHandler('click');
$scope.$apply();
expect($scope.currentPage).toBe(2);
});
describe('on-page-change callback', function() {
beforeEach(function() {
$scope.myCallback = function(currentPage) {
return "The current page is " + currentPage;
};
spyOn($scope, 'myCallback');
});
it('should call the callback once when page link clicked', function() {
compileWithAttributes(' on-page-change="myCallback(newPageNumber)" ');
var pagination = containingElement.find('ul.pagination');
expect($scope.myCallback.calls.count()).toEqual(0);
pagination.children().eq(2).find('a').triggerHandler('click');
$scope.$apply();
expect($scope.myCallback).toHaveBeenCalled();
expect($scope.myCallback.calls.count()).toEqual(1);
});
it('should not call the callback on loading first page, even with controls appearing above the pagination', function() {
function compileWithControlsFirst(attributes) {
$scope.currentPage = 1;
var html = '<dir-pagination-controls ' + attributes + ' ></dir-pagination-controls>' +
'<table>' +
'<tr dir-paginate="item in collection | itemsPerPage: 10"><td>{{ item }}</td></tr>' +
'</table>';
containingElement.append($compile(html)($scope));
$scope.$apply();
}
compileWithControlsFirst(' on-page-change="myCallback(newPageNumber)" ');
$scope.$apply(function() {
$scope.collection = myCollection;
});
var pagination = containingElement.find('ul.pagination');
expect($scope.myCallback.calls.count()).toEqual(0);
pagination.children().eq(2).find('a').triggerHandler('click');
$scope.$apply();
expect($scope.myCallback).toHaveBeenCalled();
expect($scope.myCallback.calls.count()).toEqual(1);
});
it('should pass the current page number to the callback', function() {
compileWithAttributes(' on-page-change="myCallback(newPageNumber)" ');
var pagination = containingElement.find('ul.pagination');
pagination.children().eq(2).find('a').triggerHandler('click');
$scope.$apply();
expect($scope.myCallback).toHaveBeenCalledWith(2);
});
});
describe('total-items attribute', function() {
it('should give correct pagination at 200', function() {
compileElement(myCollection, 100, 1, false, 200);
var pageLinks = getPageLinksArray();
expect(pageLinks).toEqual(['‹','1', '2', '›']);
});
it('should give correct pagination at 500', function() {
compileElement(myCollection, 100, 1, false, 500);
var pageLinks = getPageLinksArray();
expect(pageLinks).toEqual(['‹','1', '2','3','4','5', '›']);
});
it('should correctly display the second page of results', function() {
compileElement(myCollection, 100, 2, false, 500);
listItems = getListItems();
expect(listItems.length).toEqual(100);
});
});
describe('auto-hide attribute', function () {
function compileWithAttributesAndItemsPerPage(attributes, itemsPerPage) {
$scope.collection = myCollection;
$scope.currentPage = 1;
var html = '<ul class="list"><li dir-paginate="item in collection | itemsPerPage: ' +
itemsPerPage + '" current-page="currentPage">{{ item }}</li></ul> ' +
'<dir-pagination-controls ' + attributes + ' ></dir-pagination-controls>';
containingElement.append($compile(html)($scope));
$scope.$apply();
}
it('when not set, should not generate pagination controls, with not enough items to paginate over', function () {
compileWithAttributesAndItemsPerPage('', 100);
var pagination = containingElement.find('ul.pagination');
expect(pagination.length).toEqual(0);
});
it('when not set, should generate pagination controls, with enough items to paginate over', function () {
compileWithAttributesAndItemsPerPage('', 99);
var pagination = containingElement.find('ul.pagination');
expect(pagination.length).toEqual(1);
});
it('when set to false, should generate pagination controls, with not enough items to paginate over', function () {
compileWithAttributesAndItemsPerPage('auto-hide="false"', 100);
var pagination = containingElement.find('ul.pagination');
expect(pagination.length).toEqual(1);
var pageLinks = containingElement.find('ul.pagination li.disabled');
expect(pageLinks.length).toEqual(3);
});
it('when set to false, should generate pagination controls, with enough items to paginate over', function () {
compileWithAttributesAndItemsPerPage('auto-hide="false"', 99);
var pagination = containingElement.find('ul.pagination');
expect(pagination.length).toEqual(1);
});
it('when set to true, should generate pagination controls, with enough items to paginate over', function () {
compileWithAttributesAndItemsPerPage('auto-hide="true"', 100);
var pagination = containingElement.find('ul.pagination');
expect(pagination.length).toEqual(0);
});
it('when set to true, should generate pagination controls, with not enough items to paginate over', function () {
compileWithAttributesAndItemsPerPage('auto-hide="true"', 99);
var pagination = containingElement.find('ul.pagination');
expect(pagination.length).toEqual(1);
});
});
});
});
describe('multiple pagination instances per page', function() {
var collection1, collection2, currentPage1, currentPage2;
beforeEach(function() {
collection1 = [];
collection2 = [];
for (var i = 0; i < 20; i++) {
collection1.push('c1:' + i);
collection2.push('c2:' + i);
}
spyOn(console, 'warn');
});
/**
* Compile function for multiple pagination directives on a single page
* @param collection
* @param itemsPerPage
* @param currentPage
* @param paginationId
* @param customExpression
*/
function compileMultipleInstance(collection, itemsPerPage, currentPage, paginationId, customExpression) {
var expression;
var html;
if ($scope.collection === undefined) {
$scope.collection = {};
}
if ($scope.itemsPerPage === undefined) {
$scope.itemsPerPage = {};
}
if ($scope.currentPage === undefined) {
$scope.currentPage = {};
}
$scope.collection[paginationId] = collection;
$scope.itemsPerPage[paginationId] = itemsPerPage;
$scope.currentPage[paginationId] = currentPage || 1;
expression = customExpression || "item in collection." + paginationId + " | itemsPerPage: itemsPerPage." + paginationId + ": '" + paginationId + "'";
html = '<ul class="list"><li dir-paginate="'+ expression + '" current-page="currentPage.' + paginationId + '" pagination-id="' + paginationId + '" >{{ item }}</li></ul> ' +
'<dir-pagination-controls pagination-id="' + paginationId + '"></dir-pagination-controls>';
containingElement.append($compile(html)($scope));
$scope.$apply();
}
function clickPaginationLink(paginationId, index) {
var pagination = containingElement.find('dir-pagination-controls[pagination-id="' + paginationId + '"]');
pagination.find('li').eq(index).find('a').triggerHandler('click');
$scope.$apply();
}
function getMultiPageLinksArray(paginationId) {
return containingElement.find('dir-pagination-controls[pagination-id="' + paginationId + '"] li').map(function() {
return $(this).text().trim();
}).get();
}
function getMultiListItems(paginationId) {
return containingElement.find('li[pagination-id="' + paginationId + '"]').map(function() {
return $(this).text().trim();
}).get();
}
it('should allow pagination-id to control a specific collection', function() {
compileMultipleInstance(collection1, 5, 1, "c1" );
compileMultipleInstance(collection2, 5, 1, "c2" );
clickPaginationLink("c1", 2);
clickPaginationLink("c2", 4);
expect($scope.currentPage.c1).toEqual(2);
expect($scope.currentPage.c2).toEqual(4);
});
it('should allow independent changing of items per page', function() {
compileMultipleInstance(collection1, 5, 1, "c1" );
compileMultipleInstance(collection2, 5, 1, "c2" );
expect(getMultiPageLinksArray("c1").length).toBe(6);
expect(getMultiPageLinksArray("c2").length).toBe(6);
$scope.$apply(function() {
$scope.itemsPerPage.c1 = 10;
});
expect(getMultiPageLinksArray("c1").length).toBe(4);
expect(getMultiPageLinksArray("c2").length).toBe(6);
$scope.$apply(function() {
$scope.itemsPerPage.c2 = 7;
});
expect(getMultiPageLinksArray("c1").length).toBe(4);
expect(getMultiPageLinksArray("c2").length).toBe(5);
});
it('should allow independent filtering', function() {
compileMultipleInstance(collection1, 5, 1, "c1", "item in collection.c1 | filter: filter1 | itemsPerPage: itemsPerPage.c1: 'c1'");
compileMultipleInstance(collection2, 5, 1, "c2", "item in collection.c2 | filter: filter2 | itemsPerPage: itemsPerPage.c2: 'c2'" );
$scope.$apply(function() {
$scope.filter1 = "7";
$scope.filter2 = "8";
});
expect(getMultiListItems("c1")).toEqual(['c1:7', 'c1:17']);
expect(getMultiListItems("c2")).toEqual(['c2:8', 'c2:18']);
});
it('should allow independent setting of current-page externally', function() {
compileMultipleInstance(collection1, 2, 1, "c1" );
compileMultipleInstance(collection2, 2, 1, "c2" );
$scope.$apply(function() {
$scope.currentPage.c1 = 2;
$scope.currentPage.c2 = 4;
});
expect(getMultiListItems("c1")).toEqual(['c1:2', 'c1:3']);
expect(getMultiListItems("c2")).toEqual(['c2:6', 'c2:7']);
});
it('should print a warning if a non-existant paginationId is set in the pagination-controls', function() {
$scope.collection = [1,2,3,4,5];
function compile() {
var html = '<ul class="list"><li dir-paginate="item in collection | itemsPerPage: 3 : \'id1\'" pagination-id="id1" >{{ item }}</li></ul> ' +
'<dir-pagination-controls pagination-id="id2"></dir-pagination-controls>';
containingElement.append($compile(html)($scope));
$scope.$apply();
}
compile();
expect(console.warn).toHaveBeenCalledWith('Pagination directive: the pagination controls (id: id2) cannot be used without the corresponding pagination directive, which was not found at link time.');
});
it('should throw an exception if a non-existant paginationId is set in the itemsPerPage filter', function() {
$scope.collection = [1,2,3,4,5];
function compile() {
var html = '<ul class="list"><li dir-paginate="item in collection | itemsPerPage: 3 : \'id2\'" pagination-id="id1" >{{ item }}</li></ul> ' +
'<dir-pagination-controls pagination-id="id1"></dir-pagination-controls>';
containingElement.append($compile(html)($scope));
$scope.$apply();
}
expect(compile).toThrow("pagination directive: the itemsPerPage id argument (id: id2) does not match a registered pagination-id.");
});
});
describe('pagination controls template API', function() {
function compile() {
var html = '<ul class="list"><li dir-paginate="item in collection | itemsPerPage: 3" current-page="currentPage">{{ item }}</li></ul> ' +
'<dir-pagination-controls template-url="directives/pagination/testTemplate.tpl.html"></dir-pagination-controls>';
$scope.collection = [1,2,3,4,5,6,7];
$scope.currentPage = 1;
containingElement.append($compile(html)($scope));
$scope.$apply();
}
it('should provide correct values for current page', function() {
compile();
expect(containingElement.find('#tt-pagination-current').html()).toEqual('1');
$scope.$apply(function() {
$scope.currentPage = 2;
});
expect(containingElement.find('#tt-pagination-current').html()).toEqual('2');
});
it('should provide correct value for last page', function() {
compile();
expect(containingElement.find('#tt-pagination-last').html()).toEqual('3');
});
it('should provide correct value for range.lower', function() {
compile();
expect(containingElement.find('#tt-range-lower').html()).toEqual('1');
$scope.$apply(function() {
$scope.currentPage = 2;
});
expect(containingElement.find('#tt-range-lower').html()).toEqual('4');
$scope.$apply(function() {
$scope.currentPage = 3;
});
expect(containingElement.find('#tt-range-lower').html()).toEqual('7');
});
it('should provide correct value for range.upper', function() {
compile();
expect(containingElement.find('#tt-range-upper').html()).toEqual('3');
$scope.$apply(function() {
$scope.currentPage = 2;
});
expect(containingElement.find('#tt-range-upper').html()).toEqual('6');
$scope.$apply(function() {
$scope.currentPage = 3;
});
expect(containingElement.find('#tt-range-upper').html()).toEqual('7');
});
it('should provide correct value for range.total', function() {
compile();
expect(containingElement.find('#tt-range-total').html()).toEqual('7');
$scope.$apply(function() {
$scope.currentPage = 2;
});
expect(containingElement.find('#tt-range-total').html()).toEqual('7');
});
});
describe('multi element functionality', function() {
function compileMultiElement(collection, itemsPerPage, currentPage) {
var html;
$scope.collection = collection;
$scope.itemsPerPage = itemsPerPage || 10;
$scope.currentPage = currentPage || 1;
html = '<div>' +
'<div dir-paginate-start="item in collection | itemsPerPage: itemsPerPage" current-page="currentPage">header</div>' +
'<p>{{ item }}</p>' +
'<div dir-paginate-end>footer</div>' +
'</div> ';
containingElement.append($compile(html)($scope));
$scope.$apply();
}
it('should compile with multi element syntax', function() {
function compile() {
compileMultiElement([]);
}
expect(compile).not.toThrow();
});
it('should display the list correctly', function() {
compileMultiElement(myCollection, 3);
expect(containingElement.find('p').length).toEqual(3);
});
it('should page correctly', function() {
compileMultiElement(myCollection, 3);
expect(containingElement.find('p').eq(0).html()).toEqual('item 1');
$scope.$apply(function() {
$scope.currentPage = 2;
});
expect(containingElement.find('p').eq(0).html()).toEqual('item 4');
});
it('should work with data-dir-paginate-start syntax', function() {
function compile() {
var html = '<div>' +
'<h1 data-dir-paginate-start="item in collection | itemsPerPage: 3">{{ item }}</h1>' +
'<p data-dir-paginate-end>stuff</p>' +
'</div> ';
$scope.collection = myCollection;
containingElement.append($compile(html)($scope));
$scope.$apply();
}
expect(compile).not.toThrow();
expect(containingElement.find('h1').length).toEqual(3);
expect(containingElement.find('p').length).toEqual(3);
});
/**
* See https://github.com/michaelbromley/angularUtils/issues/92
*/
it('should correctly compile an inner ng-repeat', function() {
function compile() {
var html = '<div>' +
'<h1 dir-paginate-start="item in collection | itemsPerPage: 3" current-page="currentPage">{{ item }}</h1>' +
'<div dir-paginate-end> yo <ul class="options"><li ng-repeat="option in options">{{ option }} : {{ item }}</li></ul></div>' +
'</div> ';
$scope.options = ['option1', 'option2', 'option3'];
$scope.collection = myCollection;
$scope.currentPage = 1;
containingElement.append($compile(html)($scope));
$scope.$apply();
}
compile();
var options = containingElement.find('.options').eq(0).find('li');
expect(options.length).toEqual(3);
expect(options.eq(0).text()).toEqual('option1 : item 1');
});
});
/**
* THis suite tests out the ability to handle dynamically-generated pagination ids. The main use case is when
* there are multiple dir-pagination instances being generated by an ng-repeat, and the pagination id is
* only known at run-time.
*/
describe('dynamic pagination ids', function() {
function compile() {
var html = '<div ng-repeat="list in lists"><ul class="list">' +
'<li dir-paginate="item in list.collection | itemsPerPage: 3" pagination-id="list.id">{{ item }}</li>' +
'</ul>' +
'<dir-pagination-controls pagination-id="list.id"></dir-pagination-controls>' +
'</div>';
$scope.lists = [
{
id: 'list1',
collection: [1, 2, 3, 4, 5]
},
{
id: 'list2',
collection: ['a', 'b', 'c', 'd', 'e']
}
];
containingElement.append($compile(html)($scope));
$scope.$apply();
}
function getListItems($list) {
return $list.find('li').map(function() {
return $(this).text().trim();
}).get();
}
it('should not throw an exception', function() {
expect(compile).not.toThrow();
});
it('should allow independent pagination', function() {
compile();
var $list1 = containingElement.find('ul.list').eq(0);
var $list2 = containingElement.find('ul.list').eq(1);
expect(getListItems($list1)).toEqual([ '1', '2', '3' ]);
expect(getListItems($list2)).toEqual([ 'a', 'b', 'c' ]);
// click the "page 2" link on the first set of pagination
var pagination1 = $list1.parent().find('ul.pagination');
pagination1.children().eq(2).find('a').triggerHandler('click');
$scope.$apply();
// ensure only the first set of pagination changes
expect(getListItems($list1)).toEqual([ '4', '5' ]);
expect(getListItems($list2)).toEqual([ 'a', 'b', 'c' ]);
});
});
});