Skip to content

Commit

Permalink
fix(pagination): fix #668 edge case
Browse files Browse the repository at this point in the history
Also code simplification
  • Loading branch information
Alexandre Stanislawski committed Nov 23, 2015
1 parent 91e91f7 commit d8f1196
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dev/debug.css
25 changes: 17 additions & 8 deletions src/components/Pagination/Paginator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,31 @@ class Paginator {
}

pages() {
let current = this.currentPage;
let padding = this.padding;
let paddingLeft = Math.min(this.calculatePaddingLeft(current, padding, this.total), this.total);
let paddingRight = Math.max(Math.min(2 * padding + 1, this.total) - paddingLeft, 1);
let first = Math.max(current - paddingLeft, 0);
let last = current + paddingRight;
const {total, currentPage, padding} = this;

const totalDisplayedPages = this.nbPagesDisplayed(padding, total);
if (totalDisplayedPages === total) return range(0, total);

const paddingLeft = this.calculatePaddingLeft(currentPage, padding, total, totalDisplayedPages);
const paddingRight = totalDisplayedPages - paddingLeft;

const first = currentPage - paddingLeft;
const last = currentPage + paddingRight;

return range(first, last);
}

calculatePaddingLeft(current, padding, total) {
nbPagesDisplayed(padding, total) {
return Math.min(2 * padding + 1, total);
}

calculatePaddingLeft(current, padding, total, totalDisplayedPages) {
if (current <= padding) {
return current;
}

if (current >= (total - padding)) {
return 2 * padding + 1 - (total - current);
return totalDisplayedPages - (total - current);
}

return padding;
Expand Down
161 changes: 161 additions & 0 deletions src/components/Pagination/__tests__/Paginator-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/* eslint-env mocha */

import expect from 'expect';

import Paginator from '../Paginator';

describe('paginator: simple cases', () => {
context('on the first page', () => {
const pager = new Paginator({
currentPage: 0,
total: 10,
padding: 2
});

it('should return the pages', () => {
const pages = pager.pages();
expect(pages.length).toBe(5);
expect(pages).toEqual([0, 1, 2, 3, 4]);
});

it('should be the first page', () => {
expect(pager.isFirstPage()).toBe(true);
});

it('should not be the last page', () => {
expect(pager.isLastPage()).toBe(false);
});
});

context('on 3rd page', () => {
const pager = new Paginator({
currentPage: 2,
total: 10,
padding: 2
});

it('should return the pages', () => {
const pages = pager.pages();
expect(pages.length).toBe(5);
expect(pages).toEqual([0, 1, 2, 3, 4]);
});

it('should not be the first page', () => {
expect(pager.isFirstPage()).toBe(false);
});

it('should not be the last page', () => {
expect(pager.isLastPage()).toBe(false);
});
});

context('on 5th page', () => {
const pager = new Paginator({
currentPage: 5,
total: 10,
padding: 2
});

it('should return the pages', () => {
const pages = pager.pages();
expect(pages.length).toBe(5);
expect(pages).toEqual([3, 4, 5, 6, 7]);
});

it('should not be the first page', () => {
expect(pager.isFirstPage()).toBe(false);
});

it('should not be the last page', () => {
expect(pager.isLastPage()).toBe(false);
});
});

context('on the page before the last', () => {
const pager = new Paginator({
currentPage: 8,
total: 10,
padding: 2
});

it('should return the pages', () => {
const pages = pager.pages();
expect(pages.length).toBe(5);
expect(pages).toEqual([5, 6, 7, 8, 9]);
});

it('should not be the first page', () => {
expect(pager.isFirstPage()).toBe(false);
});

it('should not be the last page', () => {
expect(pager.isLastPage()).toBe(false);
});
});

context('on last page', () => {
const pager = new Paginator({
currentPage: 9,
total: 10,
padding: 2
});

it('should return the pages', () => {
const pages = pager.pages();
expect(pages.length).toBe(5);
expect(pages).toEqual([5, 6, 7, 8, 9]);
});

it('should not be the first page', () => {
expect(pager.isFirstPage()).toBe(false);
});

it('should not be the last page', () => {
expect(pager.isLastPage()).toBe(true);
});
});
});

describe('paginator: number of pages is less than 2*padding+1', () => {
const pager = new Paginator({
currentPage: 0,
total: 1,
padding: 2
});

it('should return the pages', () => {
const pages = pager.pages();
expect(pages.length).toBe(1);
expect(pages).toEqual([0]);
});

it('should be the first page', () => {
expect(pager.isFirstPage()).toBe(true);
});

it('should not be the last page', () => {
expect(pager.isLastPage()).toBe(true);
});
});

describe('paginator: bug #668', () => {
const pager = new Paginator({
currentPage: 4,
total: 6,
padding: 3
});

it('should return the pages', () => {
const pages = pager.pages();
expect(pages.length).toBe(6);
expect(pages).toEqual([0, 1, 2, 3, 4, 5]);
});

it('should be the first page', () => {
expect(pager.isFirstPage()).toBe(false);
});

it('should not be the last page', () => {
expect(pager.isLastPage()).toBe(false);
});
});

0 comments on commit d8f1196

Please sign in to comment.