1
1
describe ( 'pagination directive' , function ( ) {
2
- var $rootScope , element ;
2
+ var $scope , element , lis ;
3
3
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' ) ; } ;
11
11
} ) ) ;
12
12
13
13
it ( 'has a "pagination" css class' , function ( ) {
@@ -16,102 +16,99 @@ describe('pagination directive', function () {
16
16
17
17
it ( 'contains one ul and num-pages + 2 li elements' , function ( ) {
18
18
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' ) ;
22
22
} ) ;
23
23
24
24
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 ) ;
28
27
}
29
28
} ) ;
30
29
31
30
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 ) ;
33
32
expect ( currentPageItem . hasClass ( 'active' ) ) . toBe ( true ) ;
34
33
} ) ;
35
34
36
35
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 ) ;
40
39
expect ( previousPageItem . hasClass ( 'disabled' ) ) . toBe ( true ) ;
41
40
} ) ;
42
41
43
42
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 ) ;
47
46
expect ( nextPageItem . hasClass ( 'disabled' ) ) . toBe ( true ) ;
48
47
} ) ;
49
48
50
49
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' ) ;
52
51
page2 . click ( ) ;
53
- $rootScope . $digest ( ) ;
54
- expect ( $rootScope . currentPage ) . toBe ( 2 ) ;
52
+ $scope . $digest ( ) ;
53
+ expect ( $scope . currentPage ) . toBe ( 2 ) ;
55
54
} ) ;
56
55
57
56
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 ) ;
59
58
previous . click ( ) ;
60
- $rootScope . $digest ( ) ;
61
- expect ( $rootScope . currentPage ) . toBe ( 2 ) ;
59
+ $scope . $digest ( ) ;
60
+ expect ( $scope . currentPage ) . toBe ( 2 ) ;
62
61
} ) ;
63
62
64
63
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 ) ;
66
65
next . click ( ) ;
67
- $rootScope . $digest ( ) ;
68
- expect ( $rootScope . currentPage ) . toBe ( 4 ) ;
66
+ $scope . $digest ( ) ;
67
+ expect ( $scope . currentPage ) . toBe ( 4 ) ;
69
68
} ) ;
70
69
71
70
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 ( ) ;
75
74
previous . click ( ) ;
76
- $rootScope . $digest ( ) ;
77
- expect ( $rootScope . currentPage ) . toBe ( 1 ) ;
75
+ $scope . $digest ( ) ;
76
+ expect ( $scope . currentPage ) . toBe ( 1 ) ;
78
77
} ) ;
79
78
80
79
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 ( ) ;
84
83
next . click ( ) ;
85
- $rootScope . $digest ( ) ;
86
- expect ( $rootScope . currentPage ) . toBe ( 5 ) ;
84
+ $scope . $digest ( ) ;
85
+ expect ( $scope . currentPage ) . toBe ( 5 ) ;
87
86
} ) ;
88
87
89
88
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 ) ;
94
92
page2 . click ( ) ;
95
- $rootScope . $digest ( ) ;
96
- expect ( $rootScope . selectPageHandler ) . toHaveBeenCalledWith ( 2 ) ;
93
+ $scope . $digest ( ) ;
94
+ expect ( $scope . selectPageHandler ) . toHaveBeenCalledWith ( 2 ) ;
97
95
} ) ;
98
96
99
97
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' ) ;
105
103
} ) ;
106
104
107
105
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 ) ;
116
113
} ) ;
117
114
} ) ;
0 commit comments