Skip to content

Commit

Permalink
refactor: decouple cursor test case
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin658 committed Mar 4, 2020
1 parent 717a196 commit 62cecd1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib
node_modules
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "nyc mocha --require ts-node/register test/*.ts",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"test:docker": "docker-compose up -d && npm run test && docker-compose down",
"build": "rimraf lib && tsc",
"build": "rimraf lib && tsc -p tsconfig.build.json",
"prepublish": "npm run build"
},
"author": "Ben Hu <[email protected]>",
Expand Down
47 changes: 19 additions & 28 deletions test/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai';
import { createConnection, getConnection } from 'typeorm';

import { createQueryBuilder } from './utils/createQueryBuilder';
import { buildPaginator, PagingResult } from '../src/index';
import { buildPaginator } from '../src/index';
import { Example } from './entities/Example';

describe('TypeORM cursor-based pagination test', () => {
Expand All @@ -21,55 +21,46 @@ describe('TypeORM cursor-based pagination test', () => {
await getConnection().query('CREATE TABLE example as SELECT generate_series(1, 10) AS id;');
});

let firstPageResult: PagingResult<Example>;
let nextPageResult: PagingResult<Example>;

it('should have afterCursor if the result set has next page', async () => {
it('should paginate correctly with before and after cursor', async () => {
const queryBuilder = createQueryBuilder();
const paginator = buildPaginator({

const firstPagePaginator = buildPaginator({
entity: Example,
query: {
limit: 1,
},
});
const firstPageResult = await firstPagePaginator.paginate(queryBuilder.clone());

firstPageResult = await paginator.paginate(queryBuilder);

expect(firstPageResult.cursor.afterCursor).to.not.eq(null);
expect(firstPageResult.cursor.beforeCursor).to.eq(null);
expect(firstPageResult.data[0].id).to.eq(10);
});

it('should have beforeCursor if the result set has prev page', async () => {
const queryBuilder = createQueryBuilder();
const paginator = buildPaginator({
const nextPagePaginator = buildPaginator({
entity: Example,
query: {
limit: 1,
afterCursor: firstPageResult.cursor.afterCursor as string,
},
});
const nextPageResult = await nextPagePaginator.paginate(queryBuilder.clone());

nextPageResult = await paginator.paginate(queryBuilder);

expect(nextPageResult.cursor.afterCursor).to.not.eq(null);
expect(nextPageResult.cursor.beforeCursor).to.not.eq(null);
expect(nextPageResult.data[0].id).to.eq(9);
});

it('should return prev page result set if beforeCursor is set', async () => {
const queryBuilder = createQueryBuilder();
const paginator = buildPaginator({
const prevPagePaginator = buildPaginator({
entity: Example,
query: {
limit: 1,
beforeCursor: nextPageResult.cursor.beforeCursor as string,
},
});
const prevPageResult = await prevPagePaginator.paginate(queryBuilder.clone());

const result = await paginator.paginate(queryBuilder);
expect(firstPageResult.cursor.beforeCursor).to.eq(null);
expect(firstPageResult.cursor.afterCursor).to.not.eq(null);
expect(firstPageResult.data[0].id).to.eq(10);

expect(nextPageResult.cursor.beforeCursor).to.not.eq(null);
expect(nextPageResult.cursor.afterCursor).to.not.eq(null);
expect(nextPageResult.data[0].id).to.eq(9);

expect(result.data[0].id).to.eq(10);
expect(prevPageResult.cursor.beforeCursor).to.eq(null);
expect(prevPageResult.cursor.afterCursor).to.not.eq(null);
expect(prevPageResult.data[0].id).to.eq(10);
});

it('should return entities with given order', async () => {
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test"]
}
5 changes: 1 addition & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"include": [
"./src/**/*",
]
}
}

0 comments on commit 62cecd1

Please sign in to comment.