11import 'mocha' ; // tslint:disable-line:no-import-side-effect
22import * as assert from 'power-assert' ;
33import Facade from '../../Facade' ;
4- import Cursor from '../../types/Cursor' ;
4+ import Cursor , { end , start } from '../../types/Cursor' ;
55import Pagination from '../../types/Pagination' ;
66import PaginationDirection , { backward , forward } from '../../types/PaginationDirection' ;
7+ import Sort from '../../types/Sort' ;
8+ import { asc } from '../../types/SortOrder' ;
9+ import createCursorFromEntity from '../../utils/createCursorFromEntity' ;
710import { TestEntity , testEntity } from '../utils/testEntity' ;
811
912export default ( facade : Facade < TestEntity > ) => {
1013 const firstId = 'test_id_1' ;
1114 const secondId = 'test_id_2' ;
1215 const firstEntity = { ...testEntity , id : firstId } ;
1316 const secondEntity = { ...testEntity , id : secondId } ;
17+ const sort : Sort < TestEntity > = { id : asc } ;
1418
1519 const createTestEntities = async ( ) => {
1620 await facade . createEntity ( { id : firstId , entity : firstEntity } ) ;
@@ -19,61 +23,64 @@ export default (facade: Facade<TestEntity>) => {
1923
2024 const paginate = ( cursor : Cursor , direction : PaginationDirection ) => {
2125 const pagination : Pagination = { cursor, direction, limit : 1 } ;
22- return facade . getEntities ( { pagination } ) ;
26+ return facade . getEntities ( { pagination, sort } ) ;
2327 } ;
2428
2529 it ( 'should return all entities when pagination is not defined' , async ( ) => {
2630 await createTestEntities ( ) ;
2731 const result = await facade . getEntities ( { } ) ;
2832 assert . deepEqual ( result . entities , [ firstEntity , secondEntity ] ) ;
33+ assert . equal ( result . previousCursor , createCursorFromEntity ( firstEntity , sort ) ) ;
34+ assert . equal ( result . nextCursor , end ) ;
2935 } ) ;
3036
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 ( ) => {
3238 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 ) ;
4140 assert . deepEqual ( finalResult . entities , [ firstEntity ] ) ;
41+ assert . equal ( finalResult . previousCursor , end ) ;
42+ assert . equal ( finalResult . nextCursor , createCursorFromEntity ( firstEntity , sort ) ) ;
4243 } ) ;
4344
4445 it ( 'should return second entity when paginating forward with first cursor' , async ( ) => {
4546 await createTestEntities ( ) ;
46- const firstResult = await paginate ( undefined , forward ) ;
47+ const firstResult = await paginate ( start , forward ) ;
4748 const finalResult = await paginate ( firstResult . nextCursor , forward ) ;
4849 assert . deepEqual ( finalResult . entities , [ secondEntity ] ) ;
50+ assert . equal ( finalResult . previousCursor , createCursorFromEntity ( secondEntity , sort ) ) ;
51+ assert . equal ( finalResult . nextCursor , end ) ;
4952 } ) ;
5053
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 ( ) => {
5255 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 ) ;
5657 assert . deepEqual ( finalResult . entities , [ ] ) ;
58+ assert . equal ( finalResult . previousCursor , start ) ;
59+ assert . equal ( finalResult . nextCursor , end ) ;
5760 } ) ;
5861
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 ( ) => {
6063 await createTestEntities ( ) ;
61- const finalResult = await paginate ( undefined , backward ) ;
64+ const finalResult = await paginate ( start , backward ) ;
6265 assert . deepEqual ( finalResult . entities , [ secondEntity ] ) ;
66+ assert . equal ( finalResult . previousCursor , createCursorFromEntity ( secondEntity , sort ) ) ;
67+ assert . equal ( finalResult . nextCursor , end ) ;
6368 } ) ;
6469
6570 it ( 'should return first entity when paginating backward with first cursor' , async ( ) => {
6671 await createTestEntities ( ) ;
67- const firstResult = await paginate ( undefined , backward ) ;
72+ const firstResult = await paginate ( start , backward ) ;
6873 const finalResult = await paginate ( firstResult . previousCursor , backward ) ;
6974 assert . deepEqual ( finalResult . entities , [ firstEntity ] ) ;
75+ assert . equal ( finalResult . previousCursor , end ) ;
76+ assert . equal ( finalResult . nextCursor , createCursorFromEntity ( firstEntity , sort ) ) ;
7077 } ) ;
7178
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 ( ) => {
7380 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 ) ;
7782 assert . deepEqual ( finalResult . entities , [ ] ) ;
83+ assert . equal ( finalResult . previousCursor , end ) ;
84+ assert . equal ( finalResult . nextCursor , start ) ;
7885 } ) ;
7986} ;
0 commit comments