1
1
import 'mocha' ; // tslint:disable-line:no-import-side-effect
2
2
import * as assert from 'power-assert' ;
3
3
import Facade from '../../Facade' ;
4
- import Cursor from '../../types/Cursor' ;
4
+ import Cursor , { end , start } from '../../types/Cursor' ;
5
5
import Pagination from '../../types/Pagination' ;
6
6
import PaginationDirection , { backward , forward } from '../../types/PaginationDirection' ;
7
+ import Sort from '../../types/Sort' ;
8
+ import { asc } from '../../types/SortOrder' ;
9
+ import createCursorFromEntity from '../../utils/createCursorFromEntity' ;
7
10
import { TestEntity , testEntity } from '../utils/testEntity' ;
8
11
9
12
export default ( facade : Facade < TestEntity > ) => {
10
13
const firstId = 'test_id_1' ;
11
14
const secondId = 'test_id_2' ;
12
15
const firstEntity = { ...testEntity , id : firstId } ;
13
16
const secondEntity = { ...testEntity , id : secondId } ;
17
+ const sort : Sort < TestEntity > = { id : asc } ;
14
18
15
19
const createTestEntities = async ( ) => {
16
20
await facade . createEntity ( { id : firstId , entity : firstEntity } ) ;
@@ -19,61 +23,64 @@ export default (facade: Facade<TestEntity>) => {
19
23
20
24
const paginate = ( cursor : Cursor , direction : PaginationDirection ) => {
21
25
const pagination : Pagination = { cursor, direction, limit : 1 } ;
22
- return facade . getEntities ( { pagination } ) ;
26
+ return facade . getEntities ( { pagination, sort } ) ;
23
27
} ;
24
28
25
29
it ( 'should return all entities when pagination is not defined' , async ( ) => {
26
30
await createTestEntities ( ) ;
27
31
const result = await facade . getEntities ( { } ) ;
28
32
assert . deepEqual ( result . entities , [ firstEntity , secondEntity ] ) ;
33
+ assert . equal ( result . previousCursor , createCursorFromEntity ( firstEntity , sort ) ) ;
34
+ assert . equal ( result . nextCursor , end ) ;
29
35
} ) ;
30
36
31
- it ( 'should return first entity when there are two entities limitted to 1 ' , async ( ) => {
37
+ it ( 'should return first entity when paginating forward with start cursor ' , async ( ) => {
32
38
await createTestEntities ( ) ;
33
- const pagination : Pagination = { cursor : undefined , direction : forward , limit : 1 } ;
34
- const result = await facade . getEntities ( { pagination } ) ;
35
- assert . deepEqual ( result . entities , [ firstEntity ] ) ;
36
- } ) ;
37
-
38
- it ( 'should return first entity when paginating forward without cursor' , async ( ) => {
39
- await createTestEntities ( ) ;
40
- const finalResult = await paginate ( undefined , forward ) ;
39
+ const finalResult = await paginate ( start , forward ) ;
41
40
assert . deepEqual ( finalResult . entities , [ firstEntity ] ) ;
41
+ assert . equal ( finalResult . previousCursor , end ) ;
42
+ assert . equal ( finalResult . nextCursor , createCursorFromEntity ( firstEntity , sort ) ) ;
42
43
} ) ;
43
44
44
45
it ( 'should return second entity when paginating forward with first cursor' , async ( ) => {
45
46
await createTestEntities ( ) ;
46
- const firstResult = await paginate ( undefined , forward ) ;
47
+ const firstResult = await paginate ( start , forward ) ;
47
48
const finalResult = await paginate ( firstResult . nextCursor , forward ) ;
48
49
assert . deepEqual ( finalResult . entities , [ secondEntity ] ) ;
50
+ assert . equal ( finalResult . previousCursor , createCursorFromEntity ( secondEntity , sort ) ) ;
51
+ assert . equal ( finalResult . nextCursor , end ) ;
49
52
} ) ;
50
53
51
- it ( 'should return no entities when paginating forward with second cursor' , async ( ) => {
54
+ it ( 'should return no entities when paginating forward with end cursor' , async ( ) => {
52
55
await createTestEntities ( ) ;
53
- const firstResult = await paginate ( undefined , forward ) ;
54
- const secondResult = await paginate ( firstResult . nextCursor , forward ) ;
55
- const finalResult = await paginate ( secondResult . nextCursor , forward ) ;
56
+ const finalResult = await paginate ( end , forward ) ;
56
57
assert . deepEqual ( finalResult . entities , [ ] ) ;
58
+ assert . equal ( finalResult . previousCursor , start ) ;
59
+ assert . equal ( finalResult . nextCursor , end ) ;
57
60
} ) ;
58
61
59
- it ( 'should return second entity when paginating backward without cursor' , async ( ) => {
62
+ it ( 'should return second entity when paginating backward with start cursor' , async ( ) => {
60
63
await createTestEntities ( ) ;
61
- const finalResult = await paginate ( undefined , backward ) ;
64
+ const finalResult = await paginate ( start , backward ) ;
62
65
assert . deepEqual ( finalResult . entities , [ secondEntity ] ) ;
66
+ assert . equal ( finalResult . previousCursor , createCursorFromEntity ( secondEntity , sort ) ) ;
67
+ assert . equal ( finalResult . nextCursor , end ) ;
63
68
} ) ;
64
69
65
70
it ( 'should return first entity when paginating backward with first cursor' , async ( ) => {
66
71
await createTestEntities ( ) ;
67
- const firstResult = await paginate ( undefined , backward ) ;
72
+ const firstResult = await paginate ( start , backward ) ;
68
73
const finalResult = await paginate ( firstResult . previousCursor , backward ) ;
69
74
assert . deepEqual ( finalResult . entities , [ firstEntity ] ) ;
75
+ assert . equal ( finalResult . previousCursor , end ) ;
76
+ assert . equal ( finalResult . nextCursor , createCursorFromEntity ( firstEntity , sort ) ) ;
70
77
} ) ;
71
78
72
- it ( 'should return no entities when paginating backward with second cursor' , async ( ) => {
79
+ it ( 'should return no entities when paginating backward with end cursor' , async ( ) => {
73
80
await createTestEntities ( ) ;
74
- const firstResult = await paginate ( undefined , backward ) ;
75
- const secondResult = await paginate ( firstResult . previousCursor , backward ) ;
76
- const finalResult = await paginate ( secondResult . previousCursor , backward ) ;
81
+ const finalResult = await paginate ( end , backward ) ;
77
82
assert . deepEqual ( finalResult . entities , [ ] ) ;
83
+ assert . equal ( finalResult . previousCursor , end ) ;
84
+ assert . equal ( finalResult . nextCursor , start ) ;
78
85
} ) ;
79
86
} ;
0 commit comments