Skip to content

Commit 87bc3d1

Browse files
refact(chapter-9): refactor pagination tests
1 parent 1c9bc29 commit 87bc3d1

File tree

1 file changed

+56
-59
lines changed

1 file changed

+56
-59
lines changed
Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
describe('pagination directive', function () {
2-
var $rootScope, element;
2+
var $scope, element, lis;
33
beforeEach(module('pagination-directive'));
4-
beforeEach(inject(function(_$compile_, _$rootScope_) {
5-
$compile = _$compile_;
6-
$rootScope = _$rootScope_;
7-
$rootScope.numPages = 5;
8-
$rootScope.currentPage = 3;
9-
element = $compile('<pagination num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
10-
$rootScope.$digest();
4+
beforeEach(inject(function($compile, $rootScope) {
5+
$scope = $rootScope;
6+
$scope.numPages = 5;
7+
$scope.currentPage = 3;
8+
element = $compile('<pagination num-pages="numPages" current-page="currentPage" on-select-page="selectPageHandler(page)"></pagination>')($scope);
9+
$scope.$digest();
10+
lis = function() { return element.find('li'); };
1111
}));
1212

1313
it('has a "pagination" css class', function() {
@@ -16,102 +16,99 @@ describe('pagination directive', function () {
1616

1717
it('contains one ul and num-pages + 2 li elements', function() {
1818
expect(element.find('ul').length).toBe(1);
19-
expect(element.find('li').length).toBe(7);
20-
expect(element.find('li').eq(0).text()).toBe('Previous');
21-
expect(element.find('li').eq(-1).text()).toBe('Next');
19+
expect(lis().length).toBe(7);
20+
expect(lis().eq(0).text()).toBe('Previous');
21+
expect(lis().eq(-1).text()).toBe('Next');
2222
});
2323

2424
it('has the number of the page as text in each page item', function() {
25-
var lis = element.find('li');
26-
for(var i=1; i<=$rootScope.numPages;i++) {
27-
expect(lis.eq(i).text()).toEqual(''+i);
25+
for(var i=1; i<=$scope.numPages;i++) {
26+
expect(lis().eq(i).text()).toEqual(''+i);
2827
}
2928
});
3029

3130
it('sets the current-page to be active', function() {
32-
var currentPageItem = element.find('li').eq($rootScope.currentPage);
31+
var currentPageItem = lis().eq($scope.currentPage);
3332
expect(currentPageItem.hasClass('active')).toBe(true);
3433
});
3534

3635
it('disables the "previous" link if current-page is 1', function() {
37-
$rootScope.currentPage = 1;
38-
$rootScope.$digest();
39-
var previousPageItem = element.find('li').eq(0);
36+
$scope.currentPage = 1;
37+
$scope.$digest();
38+
var previousPageItem = lis().eq(0);
4039
expect(previousPageItem.hasClass('disabled')).toBe(true);
4140
});
4241

4342
it('disables the "next" link if current-page is num-pages', function() {
44-
$rootScope.currentPage = 5;
45-
$rootScope.$digest();
46-
var nextPageItem = element.find('li').eq(-1);
43+
$scope.currentPage = 5;
44+
$scope.$digest();
45+
var nextPageItem = lis().eq(-1);
4746
expect(nextPageItem.hasClass('disabled')).toBe(true);
4847
});
4948

5049
it('changes currentPage if a page link is clicked', function() {
51-
var page2 = element.find('li').eq(2).find('a');
50+
var page2 = lis().eq(2).find('a');
5251
page2.click();
53-
$rootScope.$digest();
54-
expect($rootScope.currentPage).toBe(2);
52+
$scope.$digest();
53+
expect($scope.currentPage).toBe(2);
5554
});
5655

5756
it('changes currentPage if the "previous" link is clicked', function() {
58-
var previous = element.find('li').eq(0).find('a').eq(0);
57+
var previous = lis().eq(0).find('a').eq(0);
5958
previous.click();
60-
$rootScope.$digest();
61-
expect($rootScope.currentPage).toBe(2);
59+
$scope.$digest();
60+
expect($scope.currentPage).toBe(2);
6261
});
6362

6463
it('changes currentPage if the "next" link is clicked', function() {
65-
var next = element.find('li').eq(-1).find('a').eq(0);
64+
var next = lis().eq(-1).find('a').eq(0);
6665
next.click();
67-
$rootScope.$digest();
68-
expect($rootScope.currentPage).toBe(4);
66+
$scope.$digest();
67+
expect($scope.currentPage).toBe(4);
6968
});
7069

7170
it('does not change the current page on "previous" click if already at first page', function() {
72-
var previous = element.find('li').eq(0).find('a').eq(0);
73-
$rootScope.currentPage = 1;
74-
$rootScope.$digest();
71+
var previous = lis().eq(0).find('a').eq(0);
72+
$scope.currentPage = 1;
73+
$scope.$digest();
7574
previous.click();
76-
$rootScope.$digest();
77-
expect($rootScope.currentPage).toBe(1);
75+
$scope.$digest();
76+
expect($scope.currentPage).toBe(1);
7877
});
7978

8079
it('does not change the current page on "next" click if already at last page', function() {
81-
var next = element.find('li').eq(-1).find('a').eq(0);
82-
$rootScope.currentPage = 5;
83-
$rootScope.$digest();
80+
var next = lis().eq(-1).find('a').eq(0);
81+
$scope.currentPage = 5;
82+
$scope.$digest();
8483
next.click();
85-
$rootScope.$digest();
86-
expect($rootScope.currentPage).toBe(5);
84+
$scope.$digest();
85+
expect($scope.currentPage).toBe(5);
8786
});
8887

8988
it('executes the onSelectPage expression when the current page changes', function() {
90-
$rootScope.selectPageHandler = jasmine.createSpy('selectPageHandler');
91-
element = $compile('<pagination num-pages="numPages" current-page="currentPage" on-select-page="selectPageHandler(page)"></pagination>')($rootScope);
92-
$rootScope.$digest();
93-
var page2 = element.find('li').eq(2).find('a').eq(0);
89+
$scope.selectPageHandler = jasmine.createSpy('selectPageHandler');
90+
$scope.$digest();
91+
var page2 = lis().eq(2).find('a').eq(0);
9492
page2.click();
95-
$rootScope.$digest();
96-
expect($rootScope.selectPageHandler).toHaveBeenCalledWith(2);
93+
$scope.$digest();
94+
expect($scope.selectPageHandler).toHaveBeenCalledWith(2);
9795
});
9896

9997
it('changes the number of items when numPages changes', function() {
100-
$rootScope.numPages = 8;
101-
$rootScope.$digest();
102-
expect(element.find('li').length).toBe(10);
103-
expect(element.find('li').eq(0).text()).toBe('Previous');
104-
expect(element.find('li').eq(-1).text()).toBe('Next');
98+
$scope.numPages = 8;
99+
$scope.$digest();
100+
expect(lis().length).toBe(10);
101+
expect(lis().eq(0).text()).toBe('Previous');
102+
expect(lis().eq(-1).text()).toBe('Next');
105103
});
106104

107105
it('sets the current page to the last page if the numPages is changed to less than the current page', function() {
108-
$rootScope.selectPageHandler = jasmine.createSpy('selectPageHandler');
109-
element = $compile('<pagination num-pages="numPages" current-page="currentPage" on-select-page="selectPageHandler(page)"></pagination>')($rootScope);
110-
$rootScope.$digest();
111-
$rootScope.numPages = 2;
112-
$rootScope.$digest();
113-
expect(element.find('li').length).toBe(4);
114-
expect($rootScope.currentPage).toBe(2);
115-
expect($rootScope.selectPageHandler).toHaveBeenCalledWith(2);
106+
$scope.selectPageHandler = jasmine.createSpy('selectPageHandler');
107+
$scope.$digest();
108+
$scope.numPages = 2;
109+
$scope.$digest();
110+
expect(lis().length).toBe(4);
111+
expect($scope.currentPage).toBe(2);
112+
expect($scope.selectPageHandler).toHaveBeenCalledWith(2);
116113
});
117114
});

0 commit comments

Comments
 (0)