1
+ describe ( 'pager directive with default configuration' , function ( ) {
2
+ var $rootScope , element ;
3
+ beforeEach ( module ( 'ui.bootstrap.pagination' ) ) ;
4
+ beforeEach ( module ( 'template/pagination/pager.html' ) ) ;
5
+ beforeEach ( inject ( function ( _$compile_ , _$rootScope_ ) {
6
+ $compile = _$compile_ ;
7
+ $rootScope = _$rootScope_ ;
8
+ $rootScope . numPages = 5 ;
9
+ $rootScope . currentPage = 3 ;
10
+ element = $compile ( '<pager num-pages="numPages" current-page="currentPage"></pager>' ) ( $rootScope ) ;
11
+ $rootScope . $digest ( ) ;
12
+ } ) ) ;
13
+
14
+ it ( 'has a "pager" css class' , function ( ) {
15
+ expect ( element . hasClass ( 'pager' ) ) . toBe ( true ) ;
16
+ } ) ;
17
+
18
+ it ( 'contains 2 li elements' , function ( ) {
19
+ expect ( element . find ( 'li' ) . length ) . toBe ( 2 ) ;
20
+ expect ( element . find ( 'li' ) . eq ( 0 ) . text ( ) ) . toBe ( '« Previous' ) ;
21
+ expect ( element . find ( 'li' ) . eq ( - 1 ) . text ( ) ) . toBe ( 'Next »' ) ;
22
+ } ) ;
23
+
24
+ it ( 'aligns previous & next page' , function ( ) {
25
+ expect ( element . find ( 'li' ) . eq ( 0 ) . hasClass ( 'previous' ) ) . toBe ( true ) ;
26
+ expect ( element . find ( 'li' ) . eq ( 0 ) . hasClass ( 'next' ) ) . toBe ( false ) ;
27
+
28
+ expect ( element . find ( 'li' ) . eq ( - 1 ) . hasClass ( 'previous' ) ) . toBe ( false ) ;
29
+ expect ( element . find ( 'li' ) . eq ( - 1 ) . hasClass ( 'next' ) ) . toBe ( true ) ;
30
+ } ) ;
31
+
32
+ it ( 'disables the "previous" link if current-page is 1' , function ( ) {
33
+ $rootScope . currentPage = 1 ;
34
+ $rootScope . $digest ( ) ;
35
+ expect ( element . find ( 'li' ) . eq ( 0 ) . hasClass ( 'disabled' ) ) . toBe ( true ) ;
36
+ } ) ;
37
+
38
+ it ( 'disables the "next" link if current-page is num-pages' , function ( ) {
39
+ $rootScope . currentPage = 5 ;
40
+ $rootScope . $digest ( ) ;
41
+ expect ( element . find ( 'li' ) . eq ( - 1 ) . hasClass ( 'disabled' ) ) . toBe ( true ) ;
42
+ } ) ;
43
+
44
+ it ( 'changes currentPage if the "previous" link is clicked' , function ( ) {
45
+ var previous = element . find ( 'li' ) . eq ( 0 ) . find ( 'a' ) . eq ( 0 ) ;
46
+ previous . click ( ) ;
47
+ $rootScope . $digest ( ) ;
48
+ expect ( $rootScope . currentPage ) . toBe ( 2 ) ;
49
+ } ) ;
50
+
51
+ it ( 'changes currentPage if the "next" link is clicked' , function ( ) {
52
+ var next = element . find ( 'li' ) . eq ( - 1 ) . find ( 'a' ) . eq ( 0 ) ;
53
+ next . click ( ) ;
54
+ $rootScope . $digest ( ) ;
55
+ expect ( $rootScope . currentPage ) . toBe ( 4 ) ;
56
+ } ) ;
57
+
58
+ it ( 'does not change the current page on "previous" click if already at first page' , function ( ) {
59
+ var previous = element . find ( 'li' ) . eq ( 0 ) . find ( 'a' ) . eq ( 0 ) ;
60
+ $rootScope . currentPage = 1 ;
61
+ $rootScope . $digest ( ) ;
62
+ previous . click ( ) ;
63
+ $rootScope . $digest ( ) ;
64
+ expect ( $rootScope . currentPage ) . toBe ( 1 ) ;
65
+ } ) ;
66
+
67
+ it ( 'does not change the current page on "next" click if already at last page' , function ( ) {
68
+ var next = element . find ( 'li' ) . eq ( - 1 ) . find ( 'a' ) . eq ( 0 ) ;
69
+ $rootScope . currentPage = 5 ;
70
+ $rootScope . $digest ( ) ;
71
+ next . click ( ) ;
72
+ $rootScope . $digest ( ) ;
73
+ expect ( $rootScope . currentPage ) . toBe ( 5 ) ;
74
+ } ) ;
75
+
76
+ it ( 'executes the onSelectPage expression when the current page changes' , function ( ) {
77
+ $rootScope . selectPageHandler = jasmine . createSpy ( 'selectPageHandler' ) ;
78
+ element = $compile ( '<pager num-pages="numPages" current-page="currentPage" on-select-page="selectPageHandler(page)"></pager>' ) ( $rootScope ) ;
79
+ $rootScope . $digest ( ) ;
80
+ var next = element . find ( 'li' ) . eq ( - 1 ) . find ( 'a' ) . eq ( 0 ) ;
81
+ next . click ( ) ;
82
+ $rootScope . $digest ( ) ;
83
+ expect ( $rootScope . selectPageHandler ) . toHaveBeenCalledWith ( 4 ) ;
84
+ } ) ;
85
+
86
+ it ( 'does not changes the number of items when numPages changes' , function ( ) {
87
+ $rootScope . numPages = 8 ;
88
+ $rootScope . $digest ( ) ;
89
+ expect ( element . find ( 'li' ) . length ) . toBe ( 2 ) ;
90
+ expect ( element . find ( 'li' ) . eq ( 0 ) . text ( ) ) . toBe ( '« Previous' ) ;
91
+ expect ( element . find ( 'li' ) . eq ( - 1 ) . text ( ) ) . toBe ( 'Next »' ) ;
92
+ } ) ;
93
+
94
+ it ( 'sets the current page to the last page if the numPages is changed to less than the current page' , function ( ) {
95
+ $rootScope . selectPageHandler = jasmine . createSpy ( 'selectPageHandler' ) ;
96
+ element = $compile ( '<pager num-pages="numPages" current-page="currentPage" on-select-page="selectPageHandler(page)"></pager>' ) ( $rootScope ) ;
97
+ $rootScope . $digest ( ) ;
98
+ $rootScope . numPages = 2 ;
99
+ $rootScope . $digest ( ) ;
100
+ expect ( element . find ( 'li' ) . length ) . toBe ( 2 ) ;
101
+ expect ( $rootScope . currentPage ) . toBe ( 2 ) ;
102
+ expect ( $rootScope . selectPageHandler ) . toHaveBeenCalledWith ( 2 ) ;
103
+ } ) ;
104
+ } ) ;
105
+
106
+ describe ( 'setting pagerConfig' , function ( ) {
107
+ var $rootScope , element ;
108
+ var originalConfig = { } ;
109
+ beforeEach ( module ( 'ui.bootstrap.pagination' ) ) ;
110
+ beforeEach ( module ( 'template/pagination/pager.html' ) ) ;
111
+ beforeEach ( inject ( function ( _$compile_ , _$rootScope_ , pagerConfig ) {
112
+ $compile = _$compile_ ;
113
+ $rootScope = _$rootScope_ ;
114
+ $rootScope . numPages = 5 ;
115
+ $rootScope . currentPage = 3 ;
116
+ angular . extend ( originalConfig , pagerConfig ) ;
117
+ pagerConfig . previousText = 'PR' ;
118
+ pagerConfig . nextText = 'NE' ;
119
+ pagerConfig . align = false ;
120
+ element = $compile ( '<pager num-pages="numPages" current-page="currentPage"></pager>' ) ( $rootScope ) ;
121
+ $rootScope . $digest ( ) ;
122
+ } ) ) ;
123
+ afterEach ( inject ( function ( pagerConfig ) {
124
+ // return it to the original state
125
+ angular . extend ( pagerConfig , originalConfig ) ;
126
+ } ) ) ;
127
+
128
+ it ( 'contains 2 li elements' , function ( ) {
129
+ expect ( element . find ( 'li' ) . length ) . toBe ( 2 ) ;
130
+ } ) ;
131
+
132
+ it ( 'should change paging text' , function ( ) {
133
+ expect ( element . find ( 'li' ) . eq ( 0 ) . text ( ) ) . toBe ( 'PR' ) ;
134
+ expect ( element . find ( 'li' ) . eq ( - 1 ) . text ( ) ) . toBe ( 'NE' ) ;
135
+ } ) ;
136
+
137
+ it ( 'should not align previous & next page link' , function ( ) {
138
+ expect ( element . find ( 'li' ) . eq ( 0 ) . hasClass ( 'previous' ) ) . toBe ( false ) ;
139
+ expect ( element . find ( 'li' ) . eq ( - 1 ) . hasClass ( 'next' ) ) . toBe ( false ) ;
140
+ } ) ;
141
+
142
+ } ) ;
143
+
144
+ describe ( 'pagination bypass configuration from attributes' , function ( ) {
145
+ var $rootScope , element ;
146
+ beforeEach ( module ( 'ui.bootstrap.pagination' ) ) ;
147
+ beforeEach ( module ( 'template/pagination/pager.html' ) ) ;
148
+ beforeEach ( inject ( function ( _$compile_ , _$rootScope_ ) {
149
+ $compile = _$compile_ ;
150
+ $rootScope = _$rootScope_ ;
151
+ $rootScope . numPages = 5 ;
152
+ $rootScope . currentPage = 3 ;
153
+ element = $compile ( '<pager align="false" previous-text="\'<\'" next-text="\'>\'" num-pages="numPages" current-page="currentPage"></pager>' ) ( $rootScope ) ;
154
+ $rootScope . $digest ( ) ;
155
+ } ) ) ;
156
+
157
+ it ( 'contains 2 li elements' , function ( ) {
158
+ expect ( element . find ( 'li' ) . length ) . toBe ( 2 ) ;
159
+ } ) ;
160
+
161
+ it ( 'should change paging text from attributes' , function ( ) {
162
+ expect ( element . find ( 'li' ) . eq ( 0 ) . text ( ) ) . toBe ( '<' ) ;
163
+ expect ( element . find ( 'li' ) . eq ( - 1 ) . text ( ) ) . toBe ( '>' ) ;
164
+ } ) ;
165
+
166
+ it ( 'should not align previous & next page link' , function ( ) {
167
+ expect ( element . find ( 'li' ) . eq ( 0 ) . hasClass ( 'previous' ) ) . toBe ( false ) ;
168
+ expect ( element . find ( 'li' ) . eq ( - 1 ) . hasClass ( 'next' ) ) . toBe ( false ) ;
169
+ } ) ;
170
+
171
+ } ) ;
0 commit comments