Skip to content

Commit 62cecd1

Browse files
committed
refactor: decouple cursor test case
1 parent 717a196 commit 62cecd1

File tree

5 files changed

+27
-33
lines changed

5 files changed

+27
-33
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib
2+
node_modules

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"test": "nyc mocha --require ts-node/register test/*.ts",
88
"coverage": "nyc report --reporter=text-lcov | coveralls",
99
"test:docker": "docker-compose up -d && npm run test && docker-compose down",
10-
"build": "rimraf lib && tsc",
10+
"build": "rimraf lib && tsc -p tsconfig.build.json",
1111
"prepublish": "npm run build"
1212
},
1313
"author": "Ben Hu <benjamin658gae@gmail.com>",

test/integration.ts

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from 'chai';
22
import { createConnection, getConnection } from 'typeorm';
33

44
import { createQueryBuilder } from './utils/createQueryBuilder';
5-
import { buildPaginator, PagingResult } from '../src/index';
5+
import { buildPaginator } from '../src/index';
66
import { Example } from './entities/Example';
77

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

24-
let firstPageResult: PagingResult<Example>;
25-
let nextPageResult: PagingResult<Example>;
26-
27-
it('should have afterCursor if the result set has next page', async () => {
24+
it('should paginate correctly with before and after cursor', async () => {
2825
const queryBuilder = createQueryBuilder();
29-
const paginator = buildPaginator({
26+
27+
const firstPagePaginator = buildPaginator({
3028
entity: Example,
3129
query: {
3230
limit: 1,
3331
},
3432
});
33+
const firstPageResult = await firstPagePaginator.paginate(queryBuilder.clone());
3534

36-
firstPageResult = await paginator.paginate(queryBuilder);
37-
38-
expect(firstPageResult.cursor.afterCursor).to.not.eq(null);
39-
expect(firstPageResult.cursor.beforeCursor).to.eq(null);
40-
expect(firstPageResult.data[0].id).to.eq(10);
41-
});
42-
43-
it('should have beforeCursor if the result set has prev page', async () => {
44-
const queryBuilder = createQueryBuilder();
45-
const paginator = buildPaginator({
35+
const nextPagePaginator = buildPaginator({
4636
entity: Example,
4737
query: {
4838
limit: 1,
4939
afterCursor: firstPageResult.cursor.afterCursor as string,
5040
},
5141
});
42+
const nextPageResult = await nextPagePaginator.paginate(queryBuilder.clone());
5243

53-
nextPageResult = await paginator.paginate(queryBuilder);
54-
55-
expect(nextPageResult.cursor.afterCursor).to.not.eq(null);
56-
expect(nextPageResult.cursor.beforeCursor).to.not.eq(null);
57-
expect(nextPageResult.data[0].id).to.eq(9);
58-
});
59-
60-
it('should return prev page result set if beforeCursor is set', async () => {
61-
const queryBuilder = createQueryBuilder();
62-
const paginator = buildPaginator({
44+
const prevPagePaginator = buildPaginator({
6345
entity: Example,
6446
query: {
6547
limit: 1,
6648
beforeCursor: nextPageResult.cursor.beforeCursor as string,
6749
},
6850
});
51+
const prevPageResult = await prevPagePaginator.paginate(queryBuilder.clone());
6952

70-
const result = await paginator.paginate(queryBuilder);
53+
expect(firstPageResult.cursor.beforeCursor).to.eq(null);
54+
expect(firstPageResult.cursor.afterCursor).to.not.eq(null);
55+
expect(firstPageResult.data[0].id).to.eq(10);
56+
57+
expect(nextPageResult.cursor.beforeCursor).to.not.eq(null);
58+
expect(nextPageResult.cursor.afterCursor).to.not.eq(null);
59+
expect(nextPageResult.data[0].id).to.eq(9);
7160

72-
expect(result.data[0].id).to.eq(10);
61+
expect(prevPageResult.cursor.beforeCursor).to.eq(null);
62+
expect(prevPageResult.cursor.afterCursor).to.not.eq(null);
63+
expect(prevPageResult.data[0].id).to.eq(10);
7364
});
7465

7566
it('should return entities with given order', async () => {

tsconfig.build.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"exclude": ["node_modules", "test"]
4+
}

tsconfig.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@
99
"declaration": true,
1010
"emitDecoratorMetadata": true,
1111
"experimentalDecorators": true
12-
},
13-
"include": [
14-
"./src/**/*",
15-
]
12+
}
1613
}

0 commit comments

Comments
 (0)