This commit is contained in:
ppetzold 2020-06-27 20:54:14 +02:00
parent 1beda55f27
commit e11a785f81
2 changed files with 25 additions and 5 deletions

View File

@ -22,8 +22,8 @@ function contextFactory(query: Request['query']): Partial<ExecutionContext> {
Object({ Object({
protocol: 'http', protocol: 'http',
get: () => 'localhost', get: () => 'localhost',
baseUrl: '/items', baseUrl: '',
path: '/all', path: '/items',
query: query, query: query,
}), }),
}), }),
@ -41,7 +41,7 @@ describe('Decorator', () => {
page: undefined, page: undefined,
limit: undefined, limit: undefined,
sortBy: undefined, sortBy: undefined,
path: 'http://localhost/items/all', path: 'http://localhost/items',
}) })
}) })
@ -61,7 +61,7 @@ describe('Decorator', () => {
['id', 'ASC'], ['id', 'ASC'],
['createdAt', 'DESC'], ['createdAt', 'DESC'],
], ],
path: 'http://localhost/items/all', path: 'http://localhost/items',
}) })
}) })
}) })

View File

@ -79,7 +79,7 @@ describe('paginate', () => {
expect(links.last).toBe('?page=3&limit=2&sortBy=id:ASC') 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<CatEntity> = { const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id', 'createdAt'], sortableColumns: ['id', 'createdAt'],
defaultSortBy: [['createdAt', 'DESC']], defaultSortBy: [['createdAt', 'DESC']],
@ -93,6 +93,26 @@ describe('paginate', () => {
expect(results.meta.sortBy).toStrictEqual([['createdAt', 'DESC']]) expect(results.meta.sortBy).toStrictEqual([['createdAt', 'DESC']])
}) })
it('should accept multiple columns to sort', async () => {
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id', 'createdAt'],
}
const query: PaginateQuery = {
path: '',
sortBy: [
['createdAt', 'DESC'],
['id', 'ASC'],
],
}
const { meta } = await paginate<CatEntity>(query, repo, config)
expect(meta.sortBy).toStrictEqual([
['createdAt', 'DESC'],
['id', 'ASC'],
])
})
it('should throw an error when no sortableColumns', async () => { it('should throw an error when no sortableColumns', async () => {
const config: PaginateConfig<CatEntity> = { const config: PaginateConfig<CatEntity> = {
sortableColumns: [], sortableColumns: [],