Add pageCount check, add getAdjacentIndexes
This commit is contained in:
@@ -3,8 +3,9 @@ import {
|
||||
getNextPageIndexInfinte,
|
||||
getPrevPageIndexLimited,
|
||||
getPrevPageIndexInfinte,
|
||||
getPageIndex
|
||||
} from './page.js';
|
||||
getPageIndex,
|
||||
getAdjacentIndexes
|
||||
} from './page.js'
|
||||
|
||||
describe('getNextPageIndexLimited', () => {
|
||||
it('returns next page index as expected', () => {
|
||||
@@ -16,10 +17,17 @@ describe('getNextPageIndexLimited', () => {
|
||||
{ currentPageIndex: 7, pagesCount: 3, expected: 2 },
|
||||
]
|
||||
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => {
|
||||
expect(getNextPageIndexLimited(currentPageIndex, pagesCount)).toBe(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(getNextPageIndexLimited(currentPageIndex, pagesCount)).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const currentPageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getNextPageIndexLimited(currentPageIndex, pagesCount)
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getNextPageIndexInfinte', () => {
|
||||
it('returns next page index as expected', () => {
|
||||
@@ -31,10 +39,17 @@ describe('getNextPageIndexInfinte', () => {
|
||||
{ currentPageIndex: 7, pagesCount: 3, expected: 0 },
|
||||
]
|
||||
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => {
|
||||
expect(getNextPageIndexInfinte(currentPageIndex, pagesCount)).toBe(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(getNextPageIndexInfinte(currentPageIndex, pagesCount)).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const currentPageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getNextPageIndexInfinte(currentPageIndex, pagesCount)
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getPrevPageIndexLimited', () => {
|
||||
it('returns prev page index as expected', () => {
|
||||
@@ -46,10 +61,17 @@ describe('getPrevPageIndexLimited', () => {
|
||||
{ currentPageIndex: 7, pagesCount: 3, expected: 2 },
|
||||
]
|
||||
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => {
|
||||
expect(getPrevPageIndexLimited(currentPageIndex, pagesCount)).toBe(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(getPrevPageIndexLimited(currentPageIndex, pagesCount)).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const currentPageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getPrevPageIndexLimited(currentPageIndex, pagesCount)
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getPrevPageIndexInfinte', () => {
|
||||
it('returns prev page index as expected', () => {
|
||||
@@ -61,10 +83,17 @@ describe('getPrevPageIndexInfinte', () => {
|
||||
{ currentPageIndex: 7, pagesCount: 3, expected: 1 },
|
||||
]
|
||||
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => {
|
||||
expect(getPrevPageIndexInfinte(currentPageIndex, pagesCount)).toBe(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(getPrevPageIndexInfinte(currentPageIndex, pagesCount)).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const currentPageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getPrevPageIndexInfinte(currentPageIndex, pagesCount)
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getPageIndex', () => {
|
||||
it('returns normalized page index as expected', () => {
|
||||
@@ -76,7 +105,51 @@ describe('getPageIndex', () => {
|
||||
{ pageIndex: 7, pagesCount: 3, expected: 2 },
|
||||
]
|
||||
testCases.forEach(({ pageIndex, pagesCount, expected }) => {
|
||||
expect(getPageIndex(pageIndex, pagesCount)).toBe(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(getPageIndex(pageIndex, pagesCount)).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const pageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getPageIndex(pageIndex, pagesCount)
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getAdjacentIndexes', () => {
|
||||
it('returns indexes as expected if infinite', () => {
|
||||
const testCases = [
|
||||
{ pageIndex: 0, pagesCount: 1, expected: [0] },
|
||||
{ pageIndex: -5, pagesCount: 10, expected: [0, 1, 9] },
|
||||
{ pageIndex: 0, pagesCount: 10, expected: [0, 1, 9] },
|
||||
{ pageIndex: 5, pagesCount: 10, expected: [4, 5, 6] },
|
||||
{ pageIndex: 9, pagesCount: 10, expected: [0, 8, 9] },
|
||||
{ pageIndex: 15, pagesCount: 10, expected: [0, 8, 9] },
|
||||
]
|
||||
testCases.forEach(({ pageIndex, pagesCount, expected }) => {
|
||||
expect(getAdjacentIndexes(pageIndex, pagesCount, true)).toEqual(expected)
|
||||
})
|
||||
})
|
||||
it('returns indexes as expected if not infinite', () => {
|
||||
const testCases = [
|
||||
{ pageIndex: 0, pagesCount: 1, expected: [0] },
|
||||
{ pageIndex: -5, pagesCount: 10, expected: [0, 1] },
|
||||
{ pageIndex: 0, pagesCount: 10, expected: [0, 1] },
|
||||
{ pageIndex: 5, pagesCount: 10, expected: [4, 5, 6] },
|
||||
{ pageIndex: 9, pagesCount: 10, expected: [8, 9] },
|
||||
{ pageIndex: 15, pagesCount: 10, expected: [8, 9] },
|
||||
]
|
||||
testCases.forEach(({ pageIndex, pagesCount, expected }) => {
|
||||
expect(getAdjacentIndexes(pageIndex, pagesCount, false)).toEqual(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const pageIndex = 5
|
||||
const pagesCount = 0
|
||||
const infinite = true
|
||||
expect(
|
||||
() => getAdjacentIndexes(pageIndex, pagesCount, infinite)
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user