Skip to content

Instantly share code, notes, and snippets.

@joemaller
Last active May 14, 2019 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemaller/094ecd182a254f67530f58a06784abb9 to your computer and use it in GitHub Desktop.
Save joemaller/094ecd182a254f67530f58a06784abb9 to your computer and use it in GitHub Desktop.
Pagination Test
node_modules/
package-lock.json
{
"name": "pagination",
"version": "1.0.0",
"description": "testing pagination",
"repository": "https://gist.github.com/094ecd182a254f67530f58a06784abb9",
"license": "ISC",
"author": "Joe Maller <joe@ideasonpurpose.com>",
"main": "paginate.js",
"scripts": {
"test": "jest",
"watch": "jest --watchAll"
},
"devDependencies": {
"jest": "^24.8.0"
}
}
function paginate(thisPage, allLinks = 12, showLinks = 5) {
const arr = [...Array(allLinks + 1).keys()].slice(1);
let start = Math.max(thisPage - Math.ceil(showLinks / 2), 0);
start = allLinks - start < showLinks ? allLinks - showLinks : start;
const end = start + showLinks; // in PHP this is just showLinks
return arr.slice(start, end);
}
module.exports = paginate;
const paginate = require("./paginate");
test("1/12", () => {
expect(paginate(2, 12)).toEqual([1, 2, 3, 4, 5]);
});
test("2/12", () => {
expect(paginate(2, 12)).toEqual([1, 2, 3, 4, 5]);
});
test("4/12", () => {
expect(paginate(4, 12)).toEqual([2, 3, 4, 5, 6]);
});
test("5/12", () => {
expect(paginate(5, 12)).toEqual([3, 4, 5, 6, 7]);
});
test("8/12", () => {
expect(paginate(8, 12)).toEqual([6, 7, 8, 9, 10]);
});
test("9/12", () => {
expect(paginate(9, 12)).toEqual([7, 8, 9, 10, 11]);
});
test("10/12", () => {
expect(paginate(10, 12)).toEqual([8, 9, 10, 11, 12]);
});
test("11/12", () => {
expect(paginate(11, 12)).toEqual([8, 9, 10, 11, 12]);
});
test("12/12", () => {
expect(paginate(12, 12)).toEqual([8, 9, 10, 11, 12]);
});
test("1/12 (4)", () => {
expect(paginate(1, 12, 4)).toEqual([1, 2, 3, 4]);
});
test("3/12 (4)", () => {
expect(paginate(3, 12, 4)).toEqual([2, 3, 4, 5]);
});
test("10/12 (4)", () => {
expect(paginate(10, 12, 4)).toEqual([9, 10, 11, 12]);
});
test("12/12 (4)", () => {
expect(paginate(12, 12, 4)).toEqual([9, 10, 11, 12]);
});
test("1/5", () => {
expect(paginate(1, 5)).toEqual([1, 2, 3, 4, 5]);
});
test("3/5", () => {
expect(paginate(3, 5)).toEqual([1, 2, 3, 4, 5]);
});
test("4/5", () => {
expect(paginate(4, 5)).toEqual([1, 2, 3, 4, 5]);
});
test("1/6", () => {
expect(paginate(1, 6)).toEqual([1, 2, 3, 4, 5]);
});
test("3/6", () => {
expect(paginate(3, 6)).toEqual([1, 2, 3, 4, 5]);
});
test("4/6", () => {
expect(paginate(4, 6)).toEqual([2, 3, 4, 5, 6]);
});
test("5/6", () => {
expect(paginate(5, 6)).toEqual([2, 3, 4, 5, 6]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment