Skip to content

Commit 6717f26

Browse files
committed
fixing page info value handling
1 parent 7881dc5 commit 6717f26

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

src/page-info.type.spec.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import 'reflect-metadata';
2+
3+
import * as D from './decorator';
4+
import * as graphql from 'graphql';
5+
6+
import { SchemaFactoryError, SchemaFactoryErrorType, schemaFactory } from './schema_factory';
7+
8+
import { PageInfo } from './page-info.type';
9+
import { execute } from 'graphql/execution';
10+
import { parse } from 'graphql/language';
11+
import { validate } from 'graphql/validation';
12+
13+
const assert = require('assert');
14+
15+
// const parse = require("graphql/language").parse as (source: string) => any;
16+
// const validate = require("graphql/validation").validate as (schema: any, ast: any, ...args: any[]) => any[];
17+
// const execute = require("graphql/execution").execute as (schema: any, ast: any, ...args: any[]) => Promise<any>;
18+
19+
describe('PageInfo', function () {
20+
21+
it('should return correct pagination when all parameters are provided', function () {
22+
let pageInfo = new PageInfo(100, 0, 10);
23+
assert(pageInfo.hasNextPage === true);
24+
assert(pageInfo.hasPreviousPage === false);
25+
});
26+
27+
it('should return correct pagination when both limit and offset are not provided', function () {
28+
let pageInfo = new PageInfo(100, undefined, undefined);
29+
assert(pageInfo.hasNextPage === false);
30+
assert(pageInfo.hasPreviousPage === false);
31+
});
32+
33+
describe('limit', function () {
34+
35+
it('should return correct pagination when limit is bigger than count', function () {
36+
let pageInfo = new PageInfo(100, 10, 200);
37+
assert(pageInfo.hasNextPage === false);
38+
assert(pageInfo.hasPreviousPage === true);
39+
});
40+
41+
it('should return correct pagination when limit is smaller than count', function () {
42+
let pageInfo = new PageInfo(100, 10, 10);
43+
assert(pageInfo.hasNextPage === true);
44+
assert(pageInfo.hasPreviousPage === true);
45+
});
46+
47+
it('should return correct pagination when limit is a negative number', function () {
48+
let pageInfo = new PageInfo(100, 10, -10);
49+
assert(pageInfo.hasNextPage === false);
50+
assert(pageInfo.hasPreviousPage === true);
51+
});
52+
53+
it('should return correct pagination when limit is zero', function () {
54+
let pageInfo = new PageInfo(100, 10, 0);
55+
assert(pageInfo.hasNextPage === false);
56+
assert(pageInfo.hasPreviousPage === true);
57+
});
58+
59+
it('should return correct pagination when limit is not provided', function () {
60+
let pageInfo = new PageInfo(100, 10, undefined);
61+
assert(pageInfo.hasNextPage === false);
62+
assert(pageInfo.hasPreviousPage === true);
63+
});
64+
65+
});
66+
67+
describe('offset', function () {
68+
69+
it('should return correct pagination when offset is a bigger than count', function () {
70+
let pageInfo = new PageInfo(100, 200, 10);
71+
assert(pageInfo.hasNextPage === false);
72+
assert(pageInfo.hasPreviousPage === true);
73+
});
74+
75+
it('should return correct pagination when offset is smaller than count', function () {
76+
let pageInfo = new PageInfo(100, 5, 10);
77+
assert(pageInfo.hasNextPage === true);
78+
assert(pageInfo.hasPreviousPage === true);
79+
});
80+
81+
it('should return correct pagination when offset is a negative number', function () {
82+
let pageInfo = new PageInfo(100, -5, 10);
83+
assert(pageInfo.hasNextPage === true);
84+
assert(pageInfo.hasPreviousPage === false);
85+
});
86+
87+
it('should return correct pagination when offset is zero', function () {
88+
let pageInfo = new PageInfo(100, 0, 10);
89+
assert(pageInfo.hasNextPage === true);
90+
assert(pageInfo.hasPreviousPage === false);
91+
});
92+
93+
it('should return correct pagination when offset is not provided', function () {
94+
let pageInfo = new PageInfo(100, undefined, 10);
95+
assert(pageInfo.hasNextPage === true);
96+
assert(pageInfo.hasPreviousPage === false);
97+
});
98+
99+
});
100+
101+
});

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)