fix: return only current link for zero results (#157)
This commit is contained in:
parent
71cf3d6c7d
commit
c8c2f22cc5
@ -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'],
|
||||||
|
@ -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),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user