Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/page-info.type.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { PageInfo } from './page-info.type';

const assert = require('assert');

describe('PageInfo', function () {

it('should return correct pagination when all parameters are provided', function () {
let pageInfo = new PageInfo(100, 0, 10);
assert(pageInfo.hasNextPage === true);
assert(pageInfo.hasPreviousPage === false);
});

it('should return correct pagination when both limit and offset are not provided', function () {
let pageInfo = new PageInfo(100, undefined, undefined);
assert(pageInfo.hasNextPage === false);
assert(pageInfo.hasPreviousPage === false);
});

describe('limit', function () {

it('should return correct pagination when limit is bigger than count', function () {
let pageInfo = new PageInfo(100, 10, 200);
assert(pageInfo.hasNextPage === false);
assert(pageInfo.hasPreviousPage === true);
});

it('should return correct pagination when limit is smaller than count', function () {
let pageInfo = new PageInfo(100, 10, 10);
assert(pageInfo.hasNextPage === true);
assert(pageInfo.hasPreviousPage === true);
});

it('should return correct pagination when limit is a negative number', function () {
let pageInfo = new PageInfo(100, 10, -10);
assert(pageInfo.hasNextPage === false);
assert(pageInfo.hasPreviousPage === true);
});

it('should return correct pagination when limit is zero', function () {
let pageInfo = new PageInfo(100, 10, 0);
assert(pageInfo.hasNextPage === false);
assert(pageInfo.hasPreviousPage === true);
});

it('should return correct pagination when limit is not provided', function () {
let pageInfo = new PageInfo(100, 10, undefined);
assert(pageInfo.hasNextPage === false);
assert(pageInfo.hasPreviousPage === true);
});

});

describe('offset', function () {

it('should return correct pagination when offset is a bigger than count', function () {
let pageInfo = new PageInfo(100, 200, 10);
assert(pageInfo.hasNextPage === false);
assert(pageInfo.hasPreviousPage === true);
});

it('should return correct pagination when offset is smaller than count', function () {
let pageInfo = new PageInfo(100, 5, 10);
assert(pageInfo.hasNextPage === true);
assert(pageInfo.hasPreviousPage === true);
});

it('should return correct pagination when offset is a negative number', function () {
let pageInfo = new PageInfo(100, -5, 10);
assert(pageInfo.hasNextPage === true);
assert(pageInfo.hasPreviousPage === false);
});

it('should return correct pagination when offset is zero', function () {
let pageInfo = new PageInfo(100, 0, 10);
assert(pageInfo.hasNextPage === true);
assert(pageInfo.hasPreviousPage === false);
});

it('should return correct pagination when offset is not provided', function () {
let pageInfo = new PageInfo(100, undefined, 10);
assert(pageInfo.hasNextPage === true);
assert(pageInfo.hasPreviousPage === false);
});

});

});
10 changes: 7 additions & 3 deletions src/page-info.type.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import * as graphql from 'graphql';

import { GraphQLObjectType } from 'graphql';

export class PageInfo {

private readonly hasNextPage: boolean;
private readonly hasPreviousPage: boolean;
public readonly hasNextPage: boolean;
public readonly hasPreviousPage: boolean;

constructor(count: number, offset: number, limit: number) {
this.hasNextPage = offset !== null && limit !== null && count > (offset + limit);
count = count || 0;
offset = offset || 0;
limit = limit > 0 ? limit : count;
this.hasNextPage = count > (offset + limit);
this.hasPreviousPage = offset > 0;
}

Expand Down