Skip to content

Commit

Permalink
Add tests for empty datasets and patch logic/math to properly handle …
Browse files Browse the repository at this point in the history
…them
  • Loading branch information
Spudz76 committed Nov 24, 2013
1 parent 0012fde commit 6060fac
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 107 deletions.
30 changes: 12 additions & 18 deletions angular-pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ paginationModule.factory("Pagination", function(){
* Total number of pages
* @type {number}
*/
pagination.prototype.pages = 1
pagination.prototype.pages = 0
/**
* Current page
* @type {number}
Expand All @@ -61,7 +61,7 @@ paginationModule.factory("Pagination", function(){
pagination.prototype.process = function(){
this.pages = Math.ceil(this.total / this.limit)
this.page = Math.ceil(this.start / this.limit) + 1
this.range.start = this.total > 0 ? this.start + 1 : 0
this.range.start = (this.total > 0) ? (this.start + 1) : 0
if(this.start + this.limit < this.total)
this.range.end = this.start + this.limit
else
Expand All @@ -73,14 +73,14 @@ paginationModule.factory("Pagination", function(){
* @returns {boolean}
*/
pagination.prototype.isFirst = function(){
return (this.page === 1)
return (1 === this.page)
}
/**
* Determine if this last page is current selected
* @returns {boolean}
*/
pagination.prototype.isLast = function(){
return (this.page === this.pages)
return ((0 === this.pages) || (this.page === this.pages))
}
/**
* Get the starting point of the first page
Expand All @@ -94,27 +94,21 @@ paginationModule.factory("Pagination", function(){
* @returns {number}
*/
pagination.prototype.previous = function(){
if(this.page <= 1)
return 0
else
return this.start - this.limit
return this.isFirst() ? 0 : this.start - this.limit
}
/**
* Get the safe starting point of the next page
* @returns {number}
*/
pagination.prototype.next = function(){
if(this.page === this.pages)
return this.start
else
return this.start + this.limit
return this.isLast() ? this.start : this.start + this.limit
}
/**
* Get the starting point of the last page
* @returns {number}
*/
pagination.prototype.last = function(){
return Math.floor((this.pages - 1) * this.limit)
return Math.max(0,Math.floor((this.pages - 1) * this.limit))
}
/**
* Get the starting point of a specific page
Expand All @@ -125,7 +119,7 @@ paginationModule.factory("Pagination", function(){
page = parseInt(page,10) || 1
if(page < 1) page = 1
if(page > this.pages) page = this.pages
return (page - 1) * this.limit
return Math.max(0,(page - 1) * this.limit)
}
/**
* Get a range of buttons
Expand All @@ -135,10 +129,10 @@ paginationModule.factory("Pagination", function(){
pagination.prototype.buttons = function(){
var buttons = []
, start, end
if(1 === this.page){
if(this.isFirst()){
start = 1
end = this.buttons_max <= this.pages ? this.buttons_max : this.pages
} else if(this.page === this.pages){
end = (this.buttons_max <= this.pages) ? this.buttons_max : this.pages
} else if(this.isLast()){
start = this.pages - (this.buttons_max - 1)
end = this.pages
if(start < 1) start = 1
Expand All @@ -164,9 +158,9 @@ paginationModule.factory("Pagination", function(){
*/
pagination.prototype.set = function(obj){
if("object" !== typeof obj) obj = {}
if("undefined" !== typeof obj.start) this.start = parseInt(obj.start,10)
if("undefined" !== typeof obj.limit) this.limit = parseInt(obj.limit,10)
if("undefined" !== typeof obj.total) this.total = parseInt(obj.total,10)
if("undefined" !== typeof obj.start) this.start = parseInt(obj.start,10)
this.process()
}
return pagination
Expand Down
275 changes: 186 additions & 89 deletions angular-pagination.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,176 @@ describe("Angular Pagination",function(){
beforeEach(inject(function(Pagination){
pg = new Pagination()
}))
it("should have proper defaults",function(){
expect(pg.pages).toBe(0)
expect(pg.page).toBe(1)
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(){
beforeEach(function(){
pg.set({start: 40,limit: 20,total: 120})
})
it("should have correct values after set()",function(){
expect(pg.pages).toBe(6)
expect(pg.page).toBe(3)
expect(pg.start).toBe(40)
expect(pg.limit).toBe(20)
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)
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)
})
})
it("should have correct values from last()",function(){
pg.set({start: pg.last()})
expect(pg.page).toBe(6)
expect(pg.start).toBe(100)
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)
})
})
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("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("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)
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)
})
})
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("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("Button Generator Checks",function(){
describe("Many-page Datasets",function(){
describe("Many-page Dataset",function(){
beforeEach(function(){
pg.set({start: 40,limit: 20,total: 120})
})
Expand Down Expand Up @@ -120,31 +216,32 @@ describe("Angular Pagination",function(){
expect(buttons.pop()).toBe(6)
})
})
describe("Three-page Datasets",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("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)
})
})
})

0 comments on commit 6060fac

Please sign in to comment.