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