Move getPageIndex to utils, add tests
This commit is contained in:
@@ -23,3 +23,7 @@ export function getPrevPageIndexInfinte(currentPageIndex, pagesCount) {
|
||||
export function getPrevPageIndexFn(infinite) {
|
||||
return infinite ? getPrevPageIndexInfinte : getPrevPageIndexLimited
|
||||
}
|
||||
|
||||
export function getPageIndex(pageIndex, pagesCount) {
|
||||
return pageIndex < 0 ? 0 : Math.min(pageIndex, pagesCount - 1)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@ import {
|
||||
getNextPageIndexLimited,
|
||||
getNextPageIndexInfinte,
|
||||
getPrevPageIndexLimited,
|
||||
getPrevPageIndexInfinte
|
||||
getPrevPageIndexInfinte,
|
||||
getPageIndex
|
||||
} from './page.js';
|
||||
|
||||
describe('getNextPageIndexLimited', () => {
|
||||
@@ -64,3 +65,18 @@ describe('getPrevPageIndexInfinte', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPageIndex', () => {
|
||||
it('returns normalized page index as expected', () => {
|
||||
const testCases = [
|
||||
{ pageIndex: -5, pagesCount: 3, expected: 0 },
|
||||
{ pageIndex: 0, pagesCount: 3, expected: 0 },
|
||||
{ pageIndex: 1, pagesCount: 3, expected: 1 },
|
||||
{ pageIndex: 2, pagesCount: 3, expected: 2 },
|
||||
{ pageIndex: 7, pagesCount: 3, expected: 2 },
|
||||
]
|
||||
testCases.forEach(({ pageIndex, pagesCount, expected }) => {
|
||||
expect(getPageIndex(pageIndex, pagesCount)).toBe(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user