Skip to content

Commit cb74fe9

Browse files
committed
Add tests (with deno)
1 parent 507ea63 commit cb74fe9

File tree

4 files changed

+2982
-68
lines changed

4 files changed

+2982
-68
lines changed

__tests__/pagesBadges.test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
2+
import pagesBadges from "../index.js";
3+
4+
Deno.test("Should work with just 1 page", async () => {
5+
const input = { currentPage: 1, pages: 1 };
6+
const output = pagesBadges(input);
7+
const expected = [1];
8+
assertEquals(output, expected);
9+
});
10+
11+
Deno.test("Should work with just 5 pages", async () => {
12+
const input = { currentPage: 2, pages: 5 };
13+
const output = pagesBadges(input);
14+
const expected = [1, 2, 3, 4, 5];
15+
assertEquals(output, expected);
16+
});
17+
18+
Deno.test("Should work with pages bigger than 5", async () => {
19+
const input = { currentPage: 14, pages: 20 };
20+
const output = pagesBadges(input);
21+
const expected = [1, null, 13, 14, 15, null, 20];
22+
assertEquals(output, expected);
23+
});
24+
25+
Deno.test("Should work with pages bigger than 5 and current page as first | second | third", async () => {
26+
const input = { currentPage: 1, pages: 20 };
27+
const input2 = { currentPage: 2, pages: 20 };
28+
const input3 = { currentPage: 3, pages: 20 };
29+
30+
const output = pagesBadges(input);
31+
const output2 = pagesBadges(input2);
32+
const output3 = pagesBadges(input3);
33+
34+
const expected = [1, 2, 3, 4, null, 20];
35+
assertEquals(output, expected);
36+
assertEquals(output2, expected);
37+
assertEquals(output3, expected);
38+
});
39+
40+
Deno.test("Should work with pages bigger than 5 and current page as 4", async () => {
41+
const input = { currentPage: 4, pages: 7 };
42+
const input2 = { currentPage: 4, pages: 6 };
43+
44+
const output = pagesBadges(input);
45+
const output2 = pagesBadges(input2);
46+
47+
const expected = [1, null, 3, 4, 5, null, 7];
48+
const expected2 = [1, null, 3, 4, 5, 6];
49+
50+
assertEquals(output, expected);
51+
assertEquals(output2, expected2);
52+
});
53+
54+
Deno.test("Should work with pages bigger than 5 and current page as one of the 3 last ones", async () => {
55+
const input = { currentPage: 20, pages: 20 };
56+
const input2 = { currentPage: 19, pages: 20 };
57+
const input3 = { currentPage: 18, pages: 20 };
58+
59+
const output = pagesBadges(input);
60+
const output2 = pagesBadges(input2);
61+
const output3 = pagesBadges(input3);
62+
63+
const expected = [1, null, 17, 18, 19, 20];
64+
65+
assertEquals(output, expected);
66+
assertEquals(output2, expected);
67+
assertEquals(output3, expected);
68+
});
69+
70+
Deno.test("Should work with page 17/20", async () => {
71+
const input = { currentPage: 17, pages: 20 };
72+
const output = pagesBadges(input);
73+
const expected = [1, null, 16, 17, 18, null, 20];
74+
75+
assertEquals(output, expected);
76+
});
77+
78+
Deno.test("Should work with page 3/6", async () => {
79+
const input = { currentPage: 3, pages: 6 };
80+
const output = pagesBadges(input);
81+
const expected = [1, 2, 3, 4, null, 6];
82+
83+
assertEquals(output, expected);
84+
});

index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
export default function pagesBadges({ currentPage, pages, numBadges = 5 }) {
2-
const maxBadgesSide = numBadges - 2
2+
const maxBadgesSide = numBadges - 2;
33

44
// Without separators case
55
// ex: [1, 2, 3, 4, 5]
6-
if (pages <= numBadges) return [...Array(pages)].map((v, i) => i + 1)
6+
if (pages <= numBadges) return [...Array(pages)].map((v, i) => i + 1);
77

8-
const sideBadges = [...Array(numBadges - 1)]
8+
const sideBadges = [...Array(numBadges - 1)];
99

1010
// With a separator at the end case
1111
// ex: [1, 2, 3, 4, null, 49]
1212
if (currentPage <= maxBadgesSide) {
13-
return [...sideBadges.map((v, i) => i + 1), null, pages]
13+
return [...sideBadges.map((v, i) => i + 1), null, pages];
1414
}
1515

1616
// With a separator at the beginning case
1717
// ex: [1, null, 46, 47, 48, 49]
1818
if (currentPage > pages - maxBadgesSide) {
19-
return [1, null, ...sideBadges.map((v, i) => pages - i).reverse()]
19+
return [1, null, ...sideBadges.map((v, i) => pages - i).reverse()];
2020
}
2121

2222
// In the middle (separator left + right) case
2323
// ex: [1, null, 26, 27, 28, null, 49]
24-
sideBadges.pop()
25-
const curr = Math.floor(sideBadges.length / 2)
26-
const center = sideBadges.map((v, i) => currentPage - curr + i)
24+
sideBadges.pop();
25+
const curr = Math.floor(sideBadges.length / 2);
26+
const center = sideBadges.map((v, i) => currentPage - curr + i);
2727

28-
return [1, null, ...center, null, pages]
28+
return [1, null, ...center, null, pages];
2929
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"scripts": {
1010
"build": "microbundle",
1111
"dev": "microbundle watch",
12-
"prepublish": "yarn build"
12+
"prepublish": "yarn build",
13+
"test": "deno test ./__tests__/pagesBadges.test.ts"
1314
},
1415
"keywords": [
1516
"paging",
@@ -29,6 +30,7 @@
2930
},
3031
"license": "MIT",
3132
"devDependencies": {
32-
"microbundle": "0.12.0"
33+
"microbundle": "0.12.0",
34+
"jest": "26.0.1"
3335
}
3436
}

0 commit comments

Comments
 (0)