@@ -2,15 +2,15 @@ import { expect } from 'chai';
22import { createConnection , getConnection } from 'typeorm' ;
33
44import { createQueryBuilder } from './utils/createQueryBuilder' ;
5- import { buildPaginator , Cursor } from '../src/index' ;
5+ import { buildPaginator , PagingResult } from '../src/index' ;
66import { Example } from './entities/Example' ;
77
88describe ( 'TypeORM cursor-based pagination test' , ( ) => {
99 before ( async ( ) => {
1010 await createConnection ( {
1111 type : 'postgres' ,
1212 host : 'localhost' ,
13- port : 5432 ,
13+ port : 5433 ,
1414 username : 'test' ,
1515 password : 'test' ,
1616 database : 'test' ,
@@ -21,9 +21,8 @@ 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 : Example [ ] ;
25- let nextPageResult : Example [ ] ;
26- let cursor : Cursor ;
24+ let firstPageResult : PagingResult < Example > ;
25+ let nextPageResult : PagingResult < Example > ;
2726
2827 it ( 'should have afterCursor if the result set has next page' , async ( ) => {
2928 const queryBuilder = createQueryBuilder ( ) ;
@@ -35,11 +34,10 @@ describe('TypeORM cursor-based pagination test', () => {
3534 } ) ;
3635
3736 firstPageResult = await paginator . paginate ( queryBuilder ) ;
38- cursor = paginator . getCursor ( ) ;
3937
40- expect ( cursor . afterCursor ) . to . not . eq ( null ) ;
41- expect ( cursor . beforeCursor ) . to . eq ( null ) ;
42- expect ( firstPageResult [ 0 ] . id ) . to . eq ( 10 ) ;
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 ) ;
4341 } ) ;
4442
4543 it ( 'should have beforeCursor if the result set has prev page' , async ( ) => {
@@ -48,16 +46,15 @@ describe('TypeORM cursor-based pagination test', () => {
4846 entity : Example ,
4947 query : {
5048 limit : 1 ,
51- afterCursor : cursor . afterCursor as string ,
49+ afterCursor : firstPageResult . cursor . afterCursor as string ,
5250 } ,
5351 } ) ;
5452
5553 nextPageResult = await paginator . paginate ( queryBuilder ) ;
56- cursor = paginator . getCursor ( ) ;
5754
58- expect ( cursor . afterCursor ) . to . not . eq ( null ) ;
59- expect ( cursor . beforeCursor ) . to . not . eq ( null ) ;
60- expect ( nextPageResult [ 0 ] . id ) . to . eq ( 9 ) ;
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 ) ;
6158 } ) ;
6259
6360 it ( 'should return prev page result set if beforeCursor is set' , async ( ) => {
@@ -66,13 +63,13 @@ describe('TypeORM cursor-based pagination test', () => {
6663 entity : Example ,
6764 query : {
6865 limit : 1 ,
69- beforeCursor : cursor . beforeCursor as string ,
66+ beforeCursor : nextPageResult . cursor . beforeCursor as string ,
7067 } ,
7168 } ) ;
7269
7370 const result = await paginator . paginate ( queryBuilder ) ;
7471
75- expect ( result [ 0 ] . id ) . to . eq ( 10 ) ;
72+ expect ( result . data [ 0 ] . id ) . to . eq ( 10 ) ;
7673 } ) ;
7774
7875 it ( 'should return entities with given order' , async ( ) => {
@@ -97,8 +94,8 @@ describe('TypeORM cursor-based pagination test', () => {
9794 const ascResult = await ascPaginator . paginate ( ascQueryBuilder ) ;
9895 const descResult = await descPaginator . paginate ( descQueryBuilder ) ;
9996
100- expect ( ascResult [ 0 ] . id ) . to . eq ( 1 ) ;
101- expect ( descResult [ 0 ] . id ) . to . eq ( 10 ) ;
97+ expect ( ascResult . data [ 0 ] . id ) . to . eq ( 1 ) ;
98+ expect ( descResult . data [ 0 ] . id ) . to . eq ( 10 ) ;
10299 } ) ;
103100
104101 it ( 'should return entities with given limit' , async ( ) => {
@@ -112,7 +109,19 @@ describe('TypeORM cursor-based pagination test', () => {
112109
113110 const result = await paginator . paginate ( queryBuilder ) ;
114111
115- expect ( result ) . length ( 10 ) ;
112+ expect ( result . data ) . length ( 10 ) ;
113+ } ) ;
114+
115+ it ( 'should return empty array and null cursor if no data' , async ( ) => {
116+ const queryBuilder = createQueryBuilder ( ) . where ( 'example.id > :id' , { id : 10 } ) ;
117+ const paginator = buildPaginator ( {
118+ entity : Example ,
119+ } ) ;
120+ const result = await paginator . paginate ( queryBuilder ) ;
121+
122+ expect ( result . data ) . length ( 0 ) ;
123+ expect ( result . cursor . beforeCursor ) . to . eq ( null ) ;
124+ expect ( result . cursor . afterCursor ) . to . eq ( null ) ;
116125 } ) ;
117126
118127 after ( async ( ) => {
0 commit comments