From dd48068b099d1b8eafef1dd3565b2685cf0cf103 Mon Sep 17 00:00:00 2001 From: Elbarae Rguig Date: Mon, 22 Aug 2022 14:46:23 +0100 Subject: [PATCH] fix: only add NULL_SORT when the nullSort field is provided, and add more tests (#297) --- README.md | 7 ++++--- src/paginate.spec.ts | 42 +++++++++++++++++++++++++++++++++++++++--- src/paginate.ts | 5 ++++- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b557f6f..de27a29 100644 --- a/README.md +++ b/README.md @@ -155,12 +155,13 @@ const paginateConfig: PaginateConfig { * Description: These are the columns that are valid to be sorted by. */ sortableColumns: ['id', 'name', 'color'], - + /** * Required: false * Type: 'first' | 'last' - * Default: 'last' - * Description: Define whether to put null values at the beginning or end of the result set. + * Default: 'first' + * Description: (ONLY WORKS WITH POSTGRES) Define whether to put null values + * at the beginning or end of the result set. */ nullSort: 'last', diff --git a/src/paginate.spec.ts b/src/paginate.spec.ts index 65cf44e..c6d80a9 100644 --- a/src/paginate.spec.ts +++ b/src/paginate.spec.ts @@ -200,7 +200,24 @@ describe('paginate', () => { const config: PaginateConfig = { sortableColumns: ['age', 'createdAt'], nullSort: 'last', - defaultSortBy: [['age', 'DESC']], + defaultSortBy: [['age', 'ASC']], + } + const query: PaginateQuery = { + path: '', + } + + const result = await paginate(query, catRepo, config) + const expectedResult = [...cats.slice(0, -1).reverse(), cats.slice(-1)[0]] + + expect(result.meta.sortBy).toStrictEqual([['age', 'ASC']]) + expect(result.data).toStrictEqual(expectedResult) + }) + + it('should put null values first when sorting', async () => { + const config: PaginateConfig = { + sortableColumns: ['age', 'createdAt'], + nullSort: 'first', + defaultSortBy: [['age', 'ASC']], } const query: PaginateQuery = { path: '', @@ -208,8 +225,27 @@ describe('paginate', () => { const result = await paginate(query, catRepo, config) - expect(result.meta.sortBy).toStrictEqual([['age', 'DESC']]) - expect(result.data).toStrictEqual(cats) + const expectedResult = [cats[cats.length - 1], ...cats.slice(0, cats.length - 1).reverse()] + + expect(result.meta.sortBy).toStrictEqual([['age', 'ASC']]) + expect(result.data).toStrictEqual(expectedResult) + }) + + it('should put null values first when nullSort is not specified', async () => { + const config: PaginateConfig = { + sortableColumns: ['age', 'createdAt'], + defaultSortBy: [['age', 'ASC']], + } + const query: PaginateQuery = { + path: '', + } + + const result = await paginate(query, catRepo, config) + + const expectedCats = cats.slice() + + expect(result.meta.sortBy).toStrictEqual([['age', 'ASC']]) + expect(result.data).toStrictEqual(expectedCats.reverse()) }) it('should sort result by multiple columns', async () => { diff --git a/src/paginate.ts b/src/paginate.ts index e15b16d..f81030d 100644 --- a/src/paginate.ts +++ b/src/paginate.ts @@ -173,7 +173,10 @@ export async function paginate( if (config.sortableColumns.length < 1) throw new ServiceUnavailableException() - const NULL_SORT = config.nullSort === 'last' ? 'NULLS LAST' : 'NULLS FIRST' + let NULL_SORT: 'NULLS LAST' | 'NULLS FIRST' + if (config.nullSort) { + NULL_SORT = config.nullSort === 'last' ? 'NULLS LAST' : 'NULLS FIRST' + } if (query.sortBy) { for (const order of query.sortBy) {