@@ -2,15 +2,15 @@ import { expect } from 'chai';
2
2
import { createConnection , getConnection } from 'typeorm' ;
3
3
4
4
import { createQueryBuilder } from './utils/createQueryBuilder' ;
5
- import { buildPaginator , Cursor } from '../src/index' ;
5
+ import { buildPaginator , PagingResult } from '../src/index' ;
6
6
import { Example } from './entities/Example' ;
7
7
8
8
describe ( 'TypeORM cursor-based pagination test' , ( ) => {
9
9
before ( async ( ) => {
10
10
await createConnection ( {
11
11
type : 'postgres' ,
12
12
host : 'localhost' ,
13
- port : 5432 ,
13
+ port : 5433 ,
14
14
username : 'test' ,
15
15
password : 'test' ,
16
16
database : 'test' ,
@@ -21,9 +21,8 @@ describe('TypeORM cursor-based pagination test', () => {
21
21
await getConnection ( ) . query ( 'CREATE TABLE example as SELECT generate_series(1, 10) AS id;' ) ;
22
22
} ) ;
23
23
24
- let firstPageResult : Example [ ] ;
25
- let nextPageResult : Example [ ] ;
26
- let cursor : Cursor ;
24
+ let firstPageResult : PagingResult < Example > ;
25
+ let nextPageResult : PagingResult < Example > ;
27
26
28
27
it ( 'should have afterCursor if the result set has next page' , async ( ) => {
29
28
const queryBuilder = createQueryBuilder ( ) ;
@@ -35,11 +34,10 @@ describe('TypeORM cursor-based pagination test', () => {
35
34
} ) ;
36
35
37
36
firstPageResult = await paginator . paginate ( queryBuilder ) ;
38
- cursor = paginator . getCursor ( ) ;
39
37
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 ) ;
43
41
} ) ;
44
42
45
43
it ( 'should have beforeCursor if the result set has prev page' , async ( ) => {
@@ -48,16 +46,15 @@ describe('TypeORM cursor-based pagination test', () => {
48
46
entity : Example ,
49
47
query : {
50
48
limit : 1 ,
51
- afterCursor : cursor . afterCursor as string ,
49
+ afterCursor : firstPageResult . cursor . afterCursor as string ,
52
50
} ,
53
51
} ) ;
54
52
55
53
nextPageResult = await paginator . paginate ( queryBuilder ) ;
56
- cursor = paginator . getCursor ( ) ;
57
54
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 ) ;
61
58
} ) ;
62
59
63
60
it ( 'should return prev page result set if beforeCursor is set' , async ( ) => {
@@ -66,13 +63,13 @@ describe('TypeORM cursor-based pagination test', () => {
66
63
entity : Example ,
67
64
query : {
68
65
limit : 1 ,
69
- beforeCursor : cursor . beforeCursor as string ,
66
+ beforeCursor : nextPageResult . cursor . beforeCursor as string ,
70
67
} ,
71
68
} ) ;
72
69
73
70
const result = await paginator . paginate ( queryBuilder ) ;
74
71
75
- expect ( result [ 0 ] . id ) . to . eq ( 10 ) ;
72
+ expect ( result . data [ 0 ] . id ) . to . eq ( 10 ) ;
76
73
} ) ;
77
74
78
75
it ( 'should return entities with given order' , async ( ) => {
@@ -97,8 +94,8 @@ describe('TypeORM cursor-based pagination test', () => {
97
94
const ascResult = await ascPaginator . paginate ( ascQueryBuilder ) ;
98
95
const descResult = await descPaginator . paginate ( descQueryBuilder ) ;
99
96
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 ) ;
102
99
} ) ;
103
100
104
101
it ( 'should return entities with given limit' , async ( ) => {
@@ -112,7 +109,19 @@ describe('TypeORM cursor-based pagination test', () => {
112
109
113
110
const result = await paginator . paginate ( queryBuilder ) ;
114
111
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 ) ;
116
125
} ) ;
117
126
118
127
after ( async ( ) => {
0 commit comments