fix: return only current link for zero results (#157)

This commit is contained in:
bartosjiri ⚡ 2022-02-10 19:21:59 +01:00 committed by GitHub
parent 71cf3d6c7d
commit c8c2f22cc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -140,7 +140,7 @@ describe('paginate', () => {
expect(result.data).toStrictEqual(cats.slice(0, 2)) 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<CatEntity> = { const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id'], sortableColumns: ['id'],
} }
@ -159,6 +159,27 @@ 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 return only current link if zero results', async () => {
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id'],
searchableColumns: ['name'],
}
const query: PaginateQuery = {
path: '',
page: 1,
limit: 2,
search: 'Pluto',
}
const { links } = await paginate<CatEntity>(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 () => { 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'],

View File

@ -278,7 +278,7 @@ export async function paginate<T>(
previous: page - 1 < 1 ? undefined : buildLink(page - 1), previous: page - 1 < 1 ? undefined : buildLink(page - 1),
current: buildLink(page), current: buildLink(page),
next: page + 1 > totalPages ? undefined : buildLink(page + 1), next: page + 1 > totalPages ? undefined : buildLink(page + 1),
last: page == totalPages ? undefined : buildLink(totalPages), last: page == totalPages || !totalItems ? undefined : buildLink(totalPages),
}, },
} }