From c8c2f22cc58c7ccbde99f83e95de48c48d41a867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?bartosjiri=20=E2=9A=A1?= Date: Thu, 10 Feb 2022 19:21:59 +0100 Subject: [PATCH] fix: return only current link for zero results (#157) --- src/paginate.spec.ts | 23 ++++++++++++++++++++++- src/paginate.ts | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/paginate.spec.ts b/src/paginate.spec.ts index 409ae47..e3320fe 100644 --- a/src/paginate.spec.ts +++ b/src/paginate.spec.ts @@ -140,7 +140,7 @@ describe('paginate', () => { expect(result.data).toStrictEqual(cats.slice(0, 2)) }) - it('should return correct links', async () => { + it('should return correct links for some results', async () => { const config: PaginateConfig = { sortableColumns: ['id'], } @@ -159,6 +159,27 @@ describe('paginate', () => { expect(links.last).toBe('?page=3&limit=2&sortBy=id:ASC') }) + it('should return only current link if zero results', async () => { + const config: PaginateConfig = { + sortableColumns: ['id'], + searchableColumns: ['name'], + } + const query: PaginateQuery = { + path: '', + page: 1, + limit: 2, + search: 'Pluto', + } + + const { links } = await paginate(query, repo, config) + + expect(links.first).toBe(undefined) + expect(links.previous).toBe(undefined) + expect(links.current).toBe('?page=1&limit=2&sortBy=id:ASC&search=Pluto') + expect(links.next).toBe(undefined) + expect(links.last).toBe(undefined) + }) + it('should default to defaultSortBy if query sortBy does not exist', async () => { const config: PaginateConfig = { sortableColumns: ['id', 'createdAt'], diff --git a/src/paginate.ts b/src/paginate.ts index ff12926..e7375dd 100644 --- a/src/paginate.ts +++ b/src/paginate.ts @@ -278,7 +278,7 @@ export async function paginate( previous: page - 1 < 1 ? undefined : buildLink(page - 1), current: buildLink(page), next: page + 1 > totalPages ? undefined : buildLink(page + 1), - last: page == totalPages ? undefined : buildLink(totalPages), + last: page == totalPages || !totalItems ? undefined : buildLink(totalPages), }, }