Skip to content

Commit a72bbd9

Browse files
authored
Merge pull request #7 from indigotech/bugfix/page-info
Bugfix - Pagination info edge cases
2 parents 54cd863 + 15447c5 commit a72bbd9

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

src/page-info.type.spec.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { PageInfo } from './page-info.type';
2+
3+
const assert = require('assert');
4+
5+
describe('PageInfo', function () {
6+
7+
it('should return correct pagination when all parameters are provided', function () {
8+
let pageInfo = new PageInfo(100, 0, 10);
9+
assert(pageInfo.hasNextPage === true);
10+
assert(pageInfo.hasPreviousPage === false);
11+
});
12+
13+
it('should return correct pagination when both limit and offset are not provided', function () {
14+
let pageInfo = new PageInfo(100, undefined, undefined);
15+
assert(pageInfo.hasNextPage === false);
16+
assert(pageInfo.hasPreviousPage === false);
17+
});
18+
19+
describe('limit', function () {
20+
21+
it('should return correct pagination when limit is bigger than count', function () {
22+
let pageInfo = new PageInfo(100, 10, 200);
23+
assert(pageInfo.hasNextPage === false);
24+
assert(pageInfo.hasPreviousPage === true);
25+
});
26+
27+
it('should return correct pagination when limit is smaller than count', function () {
28+
let pageInfo = new PageInfo(100, 10, 10);
29+
assert(pageInfo.hasNextPage === true);
30+
assert(pageInfo.hasPreviousPage === true);
31+
});
32+
33+
it('should return correct pagination when limit is a negative number', function () {
34+
let pageInfo = new PageInfo(100, 10, -10);
35+
assert(pageInfo.hasNextPage === false);
36+
assert(pageInfo.hasPreviousPage === true);
37+
});
38+
39+
it('should return correct pagination when limit is zero', function () {
40+
let pageInfo = new PageInfo(100, 10, 0);
41+
assert(pageInfo.hasNextPage === false);
42+
assert(pageInfo.hasPreviousPage === true);
43+
});
44+
45+
it('should return correct pagination when limit is not provided', function () {
46+
let pageInfo = new PageInfo(100, 10, undefined);
47+
assert(pageInfo.hasNextPage === false);
48+
assert(pageInfo.hasPreviousPage === true);
49+
});
50+
51+
});
52+
53+
describe('offset', function () {
54+
55+
it('should return correct pagination when offset is a bigger than count', function () {
56+
let pageInfo = new PageInfo(100, 200, 10);
57+
assert(pageInfo.hasNextPage === false);
58+
assert(pageInfo.hasPreviousPage === true);
59+
});
60+
61+
it('should return correct pagination when offset is smaller than count', function () {
62+
let pageInfo = new PageInfo(100, 5, 10);
63+
assert(pageInfo.hasNextPage === true);
64+
assert(pageInfo.hasPreviousPage === true);
65+
});
66+
67+
it('should return correct pagination when offset is a negative number', function () {
68+
let pageInfo = new PageInfo(100, -5, 10);
69+
assert(pageInfo.hasNextPage === true);
70+
assert(pageInfo.hasPreviousPage === false);
71+
});
72+
73+
it('should return correct pagination when offset is zero', function () {
74+
let pageInfo = new PageInfo(100, 0, 10);
75+
assert(pageInfo.hasNextPage === true);
76+
assert(pageInfo.hasPreviousPage === false);
77+
});
78+
79+
it('should return correct pagination when offset is not provided', function () {
80+
let pageInfo = new PageInfo(100, undefined, 10);
81+
assert(pageInfo.hasNextPage === true);
82+
assert(pageInfo.hasPreviousPage === false);
83+
});
84+
85+
});
86+
87+
});

src/page-info.type.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import * as graphql from 'graphql';
2+
23
import { GraphQLObjectType } from 'graphql';
34

45
export class PageInfo {
56

6-
private readonly hasNextPage: boolean;
7-
private readonly hasPreviousPage: boolean;
7+
public readonly hasNextPage: boolean;
8+
public readonly hasPreviousPage: boolean;
89

910
constructor(count: number, offset: number, limit: number) {
10-
this.hasNextPage = offset !== null && limit !== null && count > (offset + limit);
11+
count = count || 0;
12+
offset = offset || 0;
13+
limit = limit > 0 ? limit : count;
14+
this.hasNextPage = count > (offset + limit);
1115
this.hasPreviousPage = offset > 0;
1216
}
1317

0 commit comments

Comments
 (0)