-
Notifications
You must be signed in to change notification settings - Fork 4
/
angular-pagination.spec.js
executable file
·265 lines (265 loc) · 9.04 KB
/
angular-pagination.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
"use strict";
describe("Angular Pagination",function(){
var pg
// load the app
beforeEach(module("pagination"))
// get service
beforeEach(inject(function(Pagination){
pg = new Pagination()
}))
it("should have defaults as documented",function(){
expect(pg.start).toBe(0)
expect(pg.limit).toBe(10)
expect(pg.total).toBe(0)
expect(pg.pages).toBe(0)
expect(pg.page).toBe(1)
expect(pg.range.start).toBe(0)
expect(pg.range.end).toBe(0)
expect(pg.range.total).toBe(0)
})
describe("Value Checks",function(){
describe("Empty Dataset",function(){
beforeEach(function(){
pg.set({start: 0,limit: 10,total: 0})
})
it("should have correct values after set()",function(){
expect(pg.start).toBe(0)
expect(pg.limit).toBe(10)
expect(pg.total).toBe(0)
expect(pg.pages).toBe(0)
expect(pg.page).toBe(1)
expect(pg.range.start).toBe(0)
expect(pg.range.end).toBe(0)
expect(pg.range.total).toBe(0)
})
it("should have correct values from previous()",function(){
pg.set({start: pg.previous()})
expect(pg.page).toBe(1)
expect(pg.start).toBe(0)
})
it("should have correct values from next()",function(){
pg.set({start: pg.next()})
expect(pg.page).toBe(1)
expect(pg.start).toBe(0)
})
it("should have correct values from first()",function(){
pg.set({start: pg.first()})
expect(pg.page).toBe(1)
expect(pg.start).toBe(0)
})
it("should have correct values from last()",function(){
pg.set({start: pg.last()})
expect(pg.page).toBe(1)
expect(pg.start).toBe(0)
})
it("should have correct values from forPage(page)",function(){
pg.set({start: pg.forPage(2)})
expect(pg.page).toBe(1)
expect(pg.start).toBe(0)
})
})
describe("Single-entry Dataset",function(){
beforeEach(function(){
pg.set({start: 0,limit: 10,total: 1})
})
it("should have correct values after set()",function(){
expect(pg.start).toBe(0)
expect(pg.limit).toBe(10)
expect(pg.total).toBe(1)
expect(pg.pages).toBe(1)
expect(pg.page).toBe(1)
expect(pg.range.start).toBe(1)
expect(pg.range.end).toBe(1)
expect(pg.range.total).toBe(1)
})
it("should have correct values from previous()",function(){
pg.set({start: pg.previous()})
expect(pg.page).toBe(1)
expect(pg.start).toBe(0)
})
it("should have correct values from next()",function(){
pg.set({start: pg.next()})
expect(pg.page).toBe(1)
expect(pg.start).toBe(0)
})
it("should have correct values from first()",function(){
pg.set({start: pg.first()})
expect(pg.page).toBe(1)
expect(pg.start).toBe(0)
})
it("should have correct values from last()",function(){
pg.set({start: pg.last()})
expect(pg.page).toBe(1)
expect(pg.start).toBe(0)
})
it("should have correct values from forPage(page)",function(){
pg.set({start: pg.forPage(2)})
expect(pg.page).toBe(1)
expect(pg.start).toBe(0)
})
})
describe("Many-page Dataset",function(){
beforeEach(function(){
pg.set({start: 40,limit: 20,total: 120})
})
it("should have correct values after set()",function(){
expect(pg.start).toBe(40)
expect(pg.limit).toBe(20)
expect(pg.total).toBe(120)
expect(pg.pages).toBe(6)
expect(pg.page).toBe(3)
expect(pg.range.start).toBe(41)
expect(pg.range.end).toBe(60)
expect(pg.range.total).toBe(120)
})
it("should have correct values from previous()",function(){
pg.set({start: pg.previous()})
expect(pg.page).toBe(2)
expect(pg.start).toBe(20)
})
it("should have correct values from next()",function(){
pg.set({start: pg.next()})
expect(pg.page).toBe(4)
expect(pg.start).toBe(60)
})
it("should have correct values from first()",function(){
pg.set({start: pg.first()})
expect(pg.page).toBe(1)
expect(pg.start).toBe(0)
})
it("should have correct values from last()",function(){
pg.set({start: pg.last()})
expect(pg.page).toBe(6)
expect(pg.start).toBe(100)
})
it("should have correct values from forPage(page)",function(){
pg.set({start: pg.forPage(2)})
expect(pg.page).toBe(2)
expect(pg.start).toBe(20)
})
})
})
describe("Button Generator Checks",function(){
describe("Empty Dataset",function(){
beforeEach(function(){
pg.set({start: 0,limit: 10,total: 0})
})
it("should have no buttons",function(){
pg.set({start: pg.first()})
var buttons = pg.buttons()
expect(buttons.length).toBe(0)
})
})
describe("Three-page Dataset",function(){
beforeEach(function(){
pg.set({start: 0,limit: 10,total: 30})
})
it("should have 3 buttons with 2 on the right, on first() page",function(){
pg.set({start: pg.first()})
var buttons = pg.buttons()
expect(buttons.length).toBe(3)
expect(buttons.shift()).toBe(1)
expect(buttons.pop()).toBe(3)
})
it("should have 3 buttons with 1 on each side, on middle page",function(){
pg.set({start: pg.forPage(2)})
var buttons = pg.buttons()
expect(buttons.length).toBe(3)
expect(buttons.shift()).toBe(1)
expect(buttons.pop()).toBe(3)
})
it("should generate 3 buttons with 2 on the left, on last() page",function(){
pg.set({start: pg.last()})
var buttons = pg.buttons()
expect(buttons.length).toBe(3)
expect(buttons.shift()).toBe(1)
expect(buttons.pop()).toBe(3)
})
})
describe("Many-page Dataset",function(){
beforeEach(function(){
pg.set({start: 40,limit: 20,total: 120})
})
it("should have 5 buttons with 4 on the right, on first() page",function(){
pg.set({start: pg.first()})
var buttons = pg.buttons()
expect(buttons.length).toBe(pg.buttons_max)
expect(buttons.shift()).toBe(1)
expect(buttons.pop()).toBe(5)
})
it("should have 5 buttons with 3 on the right and 1 on the right, one page after first()",function(){
pg.set({start: pg.forPage(2)})
var buttons = pg.buttons()
expect(buttons.length).toBe(pg.buttons_max)
expect(buttons.shift()).toBe(1)
expect(buttons.pop()).toBe(5)
})
it("should have 5 buttons with 2 on each side, on midrange page of larger sets",function(){
pg.set({start: pg.forPage(3)})
var buttons = pg.buttons()
expect(buttons.length).toBe(pg.buttons_max)
expect(buttons.shift()).toBe(1)
expect(buttons.pop()).toBe(5)
})
it("should have 5 buttons with 3 on the left and 1 on the right, one page before last()",function(){
pg.set({start: pg.forPage(pg.pages - 1)})
var buttons = pg.buttons()
expect(buttons.length).toBe(pg.buttons_max)
expect(buttons.shift()).toBe(2)
expect(buttons.pop()).toBe(6)
})
it("should have 5 buttons with 4 on the left, on last() page",function(){
pg.set({start: pg.last()})
var buttons = pg.buttons()
expect(buttons.length).toBe(pg.buttons_max)
expect(buttons.shift()).toBe(2)
expect(buttons.pop()).toBe(6)
})
})
})
describe("Action Checks",function(){
beforeEach(function(){
pg.set({start: 40,limit: 20,total: 120})
})
it("should be unable to forPage() above max",function(){
pg.set({start: pg.forPage(20)})
expect(pg.page).toBe(6)
expect(pg.start).toBe(100)
})
it("should be unable to forPage() below min",function(){
pg.set({start: pg.forPage(-50)})
expect(pg.page).toBe(1)
expect(pg.start).toBe(0)
})
it("should be unable to previous() below min",function(){
pg.set({start: pg.first()})
pg.set({start: pg.previous()})
expect(pg.page).toBe(1)
expect(pg.start).toBe(0)
})
it("should be unable to next() above max",function(){
pg.set({start: pg.last()})
pg.set({start: pg.next()})
expect(pg.page).toBe(6)
expect(pg.start).toBe(100)
})
})
describe("Issues",function(){
it("should not display user input start values when greater than total count; issue #1",function(){
pg.set({start: 50, limit: 10, total: 1})
expect(pg.range.start).toBe(1)
expect(pg.range.end).toBe(1)
expect(pg.range.total).toBe(1)
})
it("should not use old start when limit changes on the last page; issue #3",function(){
pg.set({start: 0, limit: 10, total: 666})
pg.set({start: pg.last()})
expect(pg.start).toBe(660)
pg.set({limit: 50})
expect(pg.forLimitChange()).toBe(650)
expect(pg.forLimitChange(20)).toBe(660)
expect(pg.forLimitChange(40)).toBe(640)
expect(pg.forLimitChange(100)).toBe(600)
})
})
})