forked from angular-ui/bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpager.spec.js
311 lines (245 loc) · 10.1 KB
/
pager.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
describe('pager directive', function() {
var $compile, $rootScope, $document, $templateCache, body, element;
beforeEach(module('ui.bootstrap.pagination'));
beforeEach(module('template/pagination/pager.html'));
beforeEach(inject(function(_$compile_, _$rootScope_, _$document_, _$templateCache_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$rootScope.total = 47; // 5 pages
$rootScope.currentPage = 3;
$document = _$document_;
$templateCache = _$templateCache_;
body = $document.find('body');
element = $compile('<uib-pager total-items="total" ng-model="currentPage"></uib-pager>')($rootScope);
$rootScope.$digest();
}));
function getPaginationBarSize() {
return element.find('li').length;
}
function getPaginationEl(index) {
return element.find('li').eq(index);
}
function clickPaginationEl(index) {
getPaginationEl(index).find('a').click();
}
function getPaginationLinkEl(elem, index) {
return elem.find('li').eq(index).find('a');
}
function updateCurrentPage(value) {
$rootScope.currentPage = value;
$rootScope.$digest();
}
it('has a "pager" css class', function() {
expect(element.hasClass('pager')).toBe(true);
});
it('contains 2 li elements', function() {
expect(getPaginationBarSize()).toBe(2);
expect(getPaginationEl(0).text()).toBe('« Previous');
expect(getPaginationEl(-1).text()).toBe('Next »');
});
it('aligns previous & next page', function() {
expect(getPaginationEl(0)).toHaveClass('previous');
expect(getPaginationEl(0)).not.toHaveClass('next');
expect(getPaginationEl(-1)).not.toHaveClass('previous');
expect(getPaginationEl(-1)).toHaveClass('next');
});
it('exposes the controller on the template', function() {
$templateCache.put('template/pagination/pager.html', '<div>{{pagination.text}}</div>');
element = $compile('<uib-pager></uib-pager>')($rootScope);
$rootScope.$digest();
var ctrl = element.controller('uibPager');
expect(ctrl).toBeDefined();
ctrl.text = 'foo';
$rootScope.$digest();
expect(element.html()).toBe('foo');
});
it('disables the "previous" link if current page is 1', function() {
updateCurrentPage(1);
expect(getPaginationEl(0)).toHaveClass('disabled');
});
it('disables the "next" link if current page is num-pages', function() {
updateCurrentPage(5);
expect(getPaginationEl(-1)).toHaveClass('disabled');
});
it('changes currentPage if the "previous" link is clicked', function() {
clickPaginationEl(0);
expect($rootScope.currentPage).toBe(2);
});
it('changes currentPage if the "next" link is clicked', function() {
clickPaginationEl(-1);
expect($rootScope.currentPage).toBe(4);
});
it('does not change the current page on "previous" click if already at first page', function() {
updateCurrentPage(1);
clickPaginationEl(0);
expect($rootScope.currentPage).toBe(1);
});
it('does not change the current page on "next" click if already at last page', function() {
updateCurrentPage(5);
clickPaginationEl(-1);
expect($rootScope.currentPage).toBe(5);
});
it('executes the `ng-change` expression when an element is clicked', function() {
$rootScope.selectPageHandler = jasmine.createSpy('selectPageHandler');
element = $compile('<uib-pager total-items="total" ng-model="currentPage" ng-change="selectPageHandler()"></uib-pager>')($rootScope);
$rootScope.$digest();
clickPaginationEl(-1);
expect($rootScope.selectPageHandler).toHaveBeenCalled();
});
it('does not changes the number of pages when `total-items` changes', function() {
$rootScope.total = 73; // 8 pages
$rootScope.$digest();
expect(getPaginationBarSize()).toBe(2);
expect(getPaginationEl(0).text()).toBe('« Previous');
expect(getPaginationEl(-1).text()).toBe('Next »');
});
it('should blur the "next" link after it has been clicked', function() {
body.append(element);
var linkEl = getPaginationLinkEl(element, -1);
linkEl.focus();
expect(linkEl).toHaveFocus();
linkEl.click();
expect(linkEl).not.toHaveFocus();
element.remove();
});
it('should blur the "prev" link after it has been clicked', function() {
body.append(element);
var linkEl = getPaginationLinkEl(element, -1);
linkEl.focus();
expect(linkEl).toHaveFocus();
linkEl.click();
expect(linkEl).not.toHaveFocus();
element.remove();
});
it('allows custom templates', function() {
$templateCache.put('foo/bar.html', '<div>baz</div>');
element = $compile('<pager template-url="foo/bar.html"></pager>')($rootScope);
$rootScope.$digest();
expect(element.html()).toBe('baz');
});
describe('`items-per-page`', function() {
beforeEach(function() {
$rootScope.perpage = 5;
element = $compile('<uib-pager total-items="total" items-per-page="perpage" ng-model="currentPage"></uib-pager>')($rootScope);
$rootScope.$digest();
});
it('does not change the number of pages', function() {
expect(getPaginationBarSize()).toBe(2);
expect(getPaginationEl(0).text()).toBe('« Previous');
expect(getPaginationEl(-1).text()).toBe('Next »');
});
it('selects the last page when it is too big', function() {
$rootScope.perpage = 30;
$rootScope.$digest();
expect($rootScope.currentPage).toBe(2);
expect(getPaginationBarSize()).toBe(2);
expect(getPaginationEl(0)).not.toHaveClass('disabled');
expect(getPaginationEl(-1)).toHaveClass('disabled');
});
});
describe('when `page` is not a number', function() {
it('handles string', function() {
updateCurrentPage('1');
expect(getPaginationEl(0)).toHaveClass('disabled');
updateCurrentPage('05');
expect(getPaginationEl(-1)).toHaveClass('disabled');
});
});
describe('`num-pages`', function() {
beforeEach(function() {
$rootScope.numpg = null;
element = $compile('<uib-pager total-items="total" ng-model="currentPage" num-pages="numpg"></uib-pager>')($rootScope);
$rootScope.$digest();
});
it('equals to total number of pages', function() {
expect($rootScope.numpg).toBe(5);
});
});
describe('setting `pagerConfig`', function() {
var originalConfig = {};
beforeEach(inject(function(uibPagerConfig) {
angular.extend(originalConfig, uibPagerConfig);
uibPagerConfig.previousText = 'PR';
uibPagerConfig.nextText = 'NE';
uibPagerConfig.align = false;
element = $compile('<uib-pager total-items="total" ng-model="currentPage"></uib-pager>')($rootScope);
$rootScope.$digest();
}));
afterEach(inject(function(uibPagerConfig) {
// return it to the original state
angular.extend(uibPagerConfig, originalConfig);
}));
it('should change paging text', function() {
expect(getPaginationEl(0).text()).toBe('PR');
expect(getPaginationEl(-1).text()).toBe('NE');
});
it('should not align previous & next page link', function() {
expect(getPaginationEl(0)).not.toHaveClass('previous');
expect(getPaginationEl(-1)).not.toHaveClass('next');
});
});
describe('override configuration from attributes', function() {
beforeEach(function() {
element = $compile('<uib-pager align="false" previous-text="<" next-text=">" total-items="total" ng-model="currentPage"></uib-pager>')($rootScope);
$rootScope.$digest();
});
it('contains 2 li elements', function() {
expect(getPaginationBarSize()).toBe(2);
});
it('should change paging text from attributes', function() {
expect(getPaginationEl(0).text()).toBe('<');
expect(getPaginationEl(-1).text()).toBe('>');
});
it('should not align previous & next page link', function() {
expect(getPaginationEl(0)).not.toHaveClass('previous');
expect(getPaginationEl(-1)).not.toHaveClass('next');
});
it('changes "previous" & "next" text from interpolated attributes', function() {
$rootScope.previousText = '<<';
$rootScope.nextText = '>>';
element = $compile('<uib-pager align="false" previous-text="{{previousText}}" next-text="{{nextText}}" total-items="total" ng-model="currentPage"></uib-pager>')($rootScope);
$rootScope.$digest();
expect(getPaginationEl(0).text()).toBe('<<');
expect(getPaginationEl(-1).text()).toBe('>>');
});
});
it('disables the component when ng-disabled is true', function() {
$rootScope.disable = true;
element = $compile('<uib-pager total-items="total" ng-disabled="disable" ng-model="currentPage"></uib-pager>')($rootScope);
$rootScope.$digest();
updateCurrentPage(2);
expect(getPaginationEl(0)).toHaveClass('disabled');
expect(getPaginationEl(-1)).toHaveClass('disabled');
$rootScope.disable = false;
$rootScope.$digest();
expect(getPaginationEl(0)).not.toHaveClass('disabled');
expect(getPaginationEl(-1)).not.toHaveClass('disabled');
$rootScope.disable = true;
$rootScope.$digest();
expect(getPaginationEl(0)).toHaveClass('disabled');
expect(getPaginationEl(-1)).toHaveClass('disabled');
});
});
describe('pager deprecation', function() {
beforeEach(module('ui.bootstrap.pagination'));
beforeEach(module('template/pagination/pager.html'));
it('should suppress warning', function() {
module(function($provide) {
$provide.value('$paginationSuppressWarning', true);
});
inject(function($compile, $log, $rootScope) {
spyOn($log, 'warn');
var element = $compile('<pager total-items="total" ng-model="currentPage"></pager>')($rootScope);
$rootScope.$digest();
expect($log.warn.calls.count()).toBe(0);
});
});
it('should give warning by default', inject(function($compile, $log, $rootScope) {
spyOn($log, 'warn');
var element = $compile('<pager total-items="total" ng-model="currentPage"></pager>')($rootScope);
$rootScope.$digest();
expect($log.warn.calls.count()).toBe(2);
expect($log.warn.calls.argsFor(0)).toEqual(['PaginationController is now deprecated. Use UibPaginationController instead.']);
expect($log.warn.calls.argsFor(1)).toEqual(['pager is now deprecated. Use uib-pager instead.']);
}));
});