Pagination.spec.js
7.52 KB
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
define(function(require) {
'use strict';
var _ = require('underscore');
var Backbone = require('backbone');
var Pagination = require('solr/model/Pagination');
describe('Pagination', function() {
it('loads', function() {
expect(Pagination).toBeDefined();
});
describe(':pages', function() {
it('default value is not shared globally', function() {
var p1 = new Pagination(), p2 = new Pagination();
expect(p1.get('pages')).not.toBe(p2.get('pages'));
});
});
describe('singleton', function() {
var p;
function checkHas(hasNext, hasNextJump, hasPrevious, hasPreviousJump) {
var wanted = {
hasNext: hasNext,
hasNextJump: hasNextJump,
hasPrevious: hasPrevious,
hasPreviousJump: hasPreviousJump,
};
it('has', function() {
var got = {
hasNext: p.get('hasNext'),
hasNextJump: p.get('hasNextJump'),
hasPrevious: p.get('hasPrevious'),
hasPreviousJump: p.get('hasPreviousJump'),
};
expect(got).toEqual(wanted);
});
}
function checkPages(wantedLength, offset, currentPage) {
var pages;
beforeEach(function() {
pages = p.get('pages');
});
//console.log(JSON.stringify(pages));
var wanted = { length: wantedLength, pages: [] };
for (var i = 0; i < wantedLength; i++) {
wanted.pages[i] = { current: i + 1 === currentPage, number: i + 1 + offset };
}
it('pages', function() {
pages = p.get('pages');
var got = { length: pages.length, pages: [] };
for (var i = 0; i < pages.length; i++) {
var page = pages[i];
got.pages[i] = { current: page.current, number: page.number };
}
expect(got).toEqual(wanted);
});
}
beforeEach(function() {
p = new Pagination();
});
describe('default settings', function() {
checkHas(false, undefined, false, undefined);
checkPages(0, 0, 1);
});
describe('35 items', function() {
beforeEach(function() {
p.set({totalCount:35});
});
describe('initial settings', function() {
checkHas(true, false, false, false);
checkPages(4, 0, 1);
});
describe('clamping', function() {
describe('set currentPage=5', function() {
beforeEach(function() {
p.set({currentPage: 5});
});
checkHas(false, false, true, false);
checkPages(4, 0, 4);
});
describe('set currentPage=-1', function() {
beforeEach(function() {
p.set({currentPage: -1});
});
checkHas(true, false, false, false);
checkPages(4, 0, 1);
});
});
describe('next[1]', function() {
beforeEach(function() {
p.next();
});
checkPages(4, 0, 2);
describe('previous', function() {
beforeEach(function() {
p.previous();
});
checkHas(true, false, false, false);
checkPages(4, 0, 1);
});
describe('next[2]', function() {
beforeEach(function() {
p.next();
p.next();
});
checkHas(false, false, true, false);
checkPages(4, 0, 4);
describe('next[1] - clamp', function() {
beforeEach(function() {
p.next();
});
checkHas(false, false, true, false);
checkPages(4, 0, 4);
});
describe('page[0].jump', function() {
beforeEach(function() {
p.get('pages')[0].jump();
});
checkHas(true, false, false, false);
checkPages(4, 0, 1);
});
describe('page[3].jump', function() {
beforeEach(function() {
p.get('pages')[3].jump();
});
checkHas(false, false, true, false);
checkPages(4, 0, 4);
});
describe('decrease to 25 items', function() {
beforeEach(function() {
p.set({totalCount:25});
});
checkHas(false, false, true, false);
checkPages(3, 0, 3);
describe('increase to 150 items', function() {
beforeEach(function() {
p.set({totalCount:150});
});
checkHas(true, true, true, false);
checkPages(9, 0, 3);
});
});
});
});
describe('previous[1]', function() {
beforeEach(function() {
p.previous();
});
checkHas(true, false, false, false);
checkPages(4, 0, 1);
});
});
describe('150 items', function() {
beforeEach(function() {
p.set({totalCount:150});
});
checkHas(true, true, false, false);
checkPages(9, 0, 1);
describe('page[7].jump', function() {
beforeEach(function() {
p.get('pages')[7].jump();
});
checkHas(true, true, true, true);
checkPages(9, 3, 5);
describe('previousJump', function() {
beforeEach(function() {
p.previousJump();
});
checkHas(true, true, true, false);
checkPages(9, 0, 3);
});
});
});
describe('no page jump', function() {
beforeEach(function() {
p.set({pageJump: false});
p.set({totalCount:150});
});
checkHas(true, false, false, false);
checkPages(9, 0, 1);
});
});
});
});