From e11a785f81231d66c67940f9e609a636e95d566c Mon Sep 17 00:00:00 2001 From: ppetzold Date: Sat, 27 Jun 2020 20:54:14 +0200 Subject: [PATCH] Add test --- src/decorator.spec.ts | 8 ++++---- src/paginate.spec.ts | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/decorator.spec.ts b/src/decorator.spec.ts index b6a3cb5..fdeb60c 100644 --- a/src/decorator.spec.ts +++ b/src/decorator.spec.ts @@ -22,8 +22,8 @@ function contextFactory(query: Request['query']): Partial { Object({ protocol: 'http', get: () => 'localhost', - baseUrl: '/items', - path: '/all', + baseUrl: '', + path: '/items', query: query, }), }), @@ -41,7 +41,7 @@ describe('Decorator', () => { page: undefined, limit: undefined, sortBy: undefined, - path: 'http://localhost/items/all', + path: 'http://localhost/items', }) }) @@ -61,7 +61,7 @@ describe('Decorator', () => { ['id', 'ASC'], ['createdAt', 'DESC'], ], - path: 'http://localhost/items/all', + path: 'http://localhost/items', }) }) }) diff --git a/src/paginate.spec.ts b/src/paginate.spec.ts index 9d87dce..60c9246 100644 --- a/src/paginate.spec.ts +++ b/src/paginate.spec.ts @@ -79,7 +79,7 @@ describe('paginate', () => { expect(links.last).toBe('?page=3&limit=2&sortBy=id:ASC') }) - it('should default to defaultOrderby if query sortBy does not exist', async () => { + it('should default to defaultSortBy if query sortBy does not exist', async () => { const config: PaginateConfig = { sortableColumns: ['id', 'createdAt'], defaultSortBy: [['createdAt', 'DESC']], @@ -93,6 +93,26 @@ describe('paginate', () => { expect(results.meta.sortBy).toStrictEqual([['createdAt', 'DESC']]) }) + it('should accept multiple columns to sort', async () => { + const config: PaginateConfig = { + sortableColumns: ['id', 'createdAt'], + } + const query: PaginateQuery = { + path: '', + sortBy: [ + ['createdAt', 'DESC'], + ['id', 'ASC'], + ], + } + + const { meta } = await paginate(query, repo, config) + + expect(meta.sortBy).toStrictEqual([ + ['createdAt', 'DESC'], + ['id', 'ASC'], + ]) + }) + it('should throw an error when no sortableColumns', async () => { const config: PaginateConfig = { sortableColumns: [],