#69 : Fix unit tests
This commit is contained in:
@@ -8,7 +8,10 @@ export function getNextParticleIndexLimited({
|
||||
particlesToScroll,
|
||||
}) {
|
||||
if (particlesCount < 1) throw new Error('particlesCount must be at least 1')
|
||||
return currentParticleIndex + Math.min(particlesCount - (currentParticleIndex + 1) - particlesToScroll, particlesToScroll)
|
||||
return Math.max(
|
||||
currentParticleIndex + Math.min(particlesCount - (currentParticleIndex + 1) - particlesToScroll, particlesToScroll),
|
||||
0
|
||||
)
|
||||
}
|
||||
|
||||
export function getNextParticleIndexInfinte({
|
||||
|
||||
@@ -7,182 +7,188 @@ import {
|
||||
getPartialPageSize,
|
||||
} from './page.js'
|
||||
|
||||
// describe('getNextParticleIndexLimited', () => {
|
||||
// it('returns next page index as expected', () => {
|
||||
// const testCases = [
|
||||
// { currentParticleIndex: -5, particlesCount: 3, particlesToScroll: 1, expected: 0 },
|
||||
// { currentParticleIndex: 0, particlesCount: 3, particlesToScroll: 1, expected: 1 },
|
||||
// { currentParticleIndex: 1, particlesCount: 3, particlesToScroll: 1, expected: 2 },
|
||||
// { currentParticleIndex: 2, particlesCount: 3, particlesToScroll: 1, expected: 2 },
|
||||
// { currentParticleIndex: 7, particlesCount: 3, particlesToScroll: 1, expected: 2 },
|
||||
// ]
|
||||
// testCases.forEach(({
|
||||
// currentParticleIndex,
|
||||
// particlesCount,
|
||||
// particlesToScroll,
|
||||
// expected,
|
||||
// }) => {
|
||||
// expect(getNextParticleIndexLimited({
|
||||
// currentParticleIndex,
|
||||
// particlesCount,
|
||||
// particlesToScroll,
|
||||
// })).toBe(expected)
|
||||
// })
|
||||
// })
|
||||
// it('throws error if particlesCount is less than 1', () => {
|
||||
// const currentParticleIndex = 5
|
||||
// const particlesCount = 0
|
||||
// const particlesToScroll = 1
|
||||
// expect(
|
||||
// () => getNextParticleIndexLimited({
|
||||
// currentParticleIndex,
|
||||
// particlesCount,
|
||||
// particlesToScroll,
|
||||
// })
|
||||
// ).toThrowError('particlesCount must be at least 1')
|
||||
// })
|
||||
// })
|
||||
describe('getNextParticleIndexLimited', () => {
|
||||
it('returns next page index as expected', () => {
|
||||
const testCases = [
|
||||
{ currentParticleIndex: -5, particlesCount: 7, particlesToScroll: 2, expected: 0 },
|
||||
{ currentParticleIndex: 0, particlesCount: 7, particlesToScroll: 2, expected: 2 },
|
||||
{ currentParticleIndex: 2, particlesCount: 7, particlesToScroll: 2, expected: 4 },
|
||||
{ currentParticleIndex: 7, particlesCount: 7, particlesToScroll: 2, expected: 4 },
|
||||
]
|
||||
testCases.forEach(({
|
||||
currentParticleIndex,
|
||||
particlesCount,
|
||||
particlesToScroll,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getNextParticleIndexLimited({
|
||||
currentParticleIndex,
|
||||
particlesCount,
|
||||
particlesToScroll,
|
||||
})).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if particlesCount is less than 1', () => {
|
||||
const currentParticleIndex = 5
|
||||
const particlesCount = 0
|
||||
const particlesToScroll = 1
|
||||
expect(
|
||||
() => getNextParticleIndexLimited({
|
||||
currentParticleIndex,
|
||||
particlesCount,
|
||||
particlesToScroll,
|
||||
})
|
||||
).toThrowError('particlesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
// describe('getNextParticleIndexInfinte', () => {
|
||||
// it('returns next page index as expected', () => {
|
||||
// const testCases = [
|
||||
// { currentParticleIndex: -5, particlesCount: 3, expected: 1 },
|
||||
// { currentParticleIndex: 0, particlesCount: 3, expected: 1 },
|
||||
// { currentParticleIndex: 1, particlesCount: 3, expected: 2 },
|
||||
// { currentParticleIndex: 2, particlesCount: 3, expected: 0 },
|
||||
// { currentParticleIndex: 7, particlesCount: 3, expected: 0 },
|
||||
// ]
|
||||
// testCases.forEach(({
|
||||
// currentParticleIndex,
|
||||
// particlesCount,
|
||||
// expected,
|
||||
// }) => {
|
||||
// expect(getNextParticleIndexInfinte({
|
||||
// currentParticleIndex,
|
||||
// particlesCount,
|
||||
// })).toBe(expected)
|
||||
// })
|
||||
// })
|
||||
// it('throws error if particlesCount is less than 1', () => {
|
||||
// const currentParticleIndex = 5
|
||||
// const particlesCount = 0
|
||||
// expect(
|
||||
// () => getNextParticleIndexInfinte({
|
||||
// currentParticleIndex,
|
||||
// particlesCount,
|
||||
// })
|
||||
// ).toThrowError('particlesCount must be at least 1')
|
||||
// })
|
||||
// })
|
||||
describe('getNextParticleIndexInfinte', () => {
|
||||
it('returns next page index as expected', () => {
|
||||
const testCases = [
|
||||
{ currentParticleIndex: -5, particlesCount: 7, particlesToScroll: 2, clonesCountTail: 3, expected: 2 },
|
||||
{ currentParticleIndex: 0, particlesCount: 7, particlesToScroll: 2, clonesCountTail: 3, expected: 2 },
|
||||
{ currentParticleIndex: 2, particlesCount: 7, particlesToScroll: 2, clonesCountTail: 3, expected: 4 },
|
||||
{ currentParticleIndex: 7, particlesCount: 7, particlesToScroll: 2, clonesCountTail: 3, expected: 4 },
|
||||
]
|
||||
testCases.forEach(({
|
||||
currentParticleIndex,
|
||||
particlesCount,
|
||||
particlesToScroll,
|
||||
clonesCountTail,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getNextParticleIndexInfinte({
|
||||
currentParticleIndex,
|
||||
particlesCount,
|
||||
particlesToScroll,
|
||||
clonesCountTail,
|
||||
})).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if particlesCount is less than 1', () => {
|
||||
const currentParticleIndex = 5
|
||||
const particlesCount = 0
|
||||
expect(
|
||||
() => getNextParticleIndexInfinte({
|
||||
currentParticleIndex,
|
||||
particlesCount,
|
||||
})
|
||||
).toThrowError('particlesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
// describe('getPrevParticleIndexLimited', () => {
|
||||
// it('returns prev page index as expected', () => {
|
||||
// const testCases = [
|
||||
// { currentParticleIndex: -5, particlesCount: 3, expected: 0 },
|
||||
// { currentParticleIndex: 0, particlesCount: 3, expected: 0 },
|
||||
// { currentParticleIndex: 1, particlesCount: 3, expected: 0 },
|
||||
// { currentParticleIndex: 2, particlesCount: 3, expected: 1 },
|
||||
// { currentParticleIndex: 7, particlesCount: 3, expected: 2 },
|
||||
// ]
|
||||
// testCases.forEach(({
|
||||
// currentParticleIndex,
|
||||
// particlesCount,
|
||||
// expected,
|
||||
// }) => {
|
||||
// expect(getPrevParticleIndexLimited({
|
||||
// currentParticleIndex,
|
||||
// particlesCount,
|
||||
// })).toBe(expected)
|
||||
// })
|
||||
// })
|
||||
// it('throws error if particlesCount is less than 1', () => {
|
||||
// const currentParticleIndex = 5
|
||||
// const particlesCount = 0
|
||||
// expect(
|
||||
// () => getPrevParticleIndexLimited({
|
||||
// currentParticleIndex,
|
||||
// particlesCount,
|
||||
// })
|
||||
// ).toThrowError('particlesCount must be at least 1')
|
||||
// })
|
||||
// })
|
||||
describe('getPrevParticleIndexLimited', () => {
|
||||
it('returns prev page index as expected', () => {
|
||||
const testCases = [
|
||||
{ currentParticleIndex: -5, particlesCount: 7, particlesToScroll: 2, expected: 0 },
|
||||
{ currentParticleIndex: 0, particlesCount: 7, particlesToScroll: 2, expected: 0 },
|
||||
{ currentParticleIndex: 2, particlesCount: 7, particlesToScroll: 2, expected: 0 },
|
||||
{ currentParticleIndex: 4, particlesCount: 7, particlesToScroll: 2, expected: 2 },
|
||||
{ currentParticleIndex: 10, particlesCount: 7, particlesToScroll: 2, expected: 6 },
|
||||
]
|
||||
testCases.forEach(({
|
||||
currentParticleIndex,
|
||||
particlesCount,
|
||||
particlesToScroll,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getPrevParticleIndexLimited({
|
||||
currentParticleIndex,
|
||||
particlesCount,
|
||||
particlesToScroll,
|
||||
})).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if particlesCount is less than 1', () => {
|
||||
const currentParticleIndex = 5
|
||||
const particlesCount = 0
|
||||
expect(
|
||||
() => getPrevParticleIndexLimited({
|
||||
currentParticleIndex,
|
||||
particlesCount,
|
||||
})
|
||||
).toThrowError('particlesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
// describe('getPrevParticleIndexInfinte', () => {
|
||||
// it('returns prev page index as expected', () => {
|
||||
// const testCases = [
|
||||
// { currentParticleIndex: -5, particlesCount: 3, expected: 2 },
|
||||
// { currentParticleIndex: 0, particlesCount: 3, expected: 2 },
|
||||
// { currentParticleIndex: 1, particlesCount: 3, expected: 0 },
|
||||
// { currentParticleIndex: 2, particlesCount: 3, expected: 1 },
|
||||
// { currentParticleIndex: 7, particlesCount: 3, expected: 1 },
|
||||
// ]
|
||||
// testCases.forEach(({
|
||||
// currentParticleIndex,
|
||||
// particlesCount,
|
||||
// expected,
|
||||
// }) => {
|
||||
// expect(getPrevParticleIndexInfinte({
|
||||
// currentParticleIndex,
|
||||
// particlesCount,
|
||||
// })).toBe(expected)
|
||||
// })
|
||||
// })
|
||||
// it('throws error if particlesCount is less than 1', () => {
|
||||
// const currentParticleIndex = 5
|
||||
// const particlesCount = 0
|
||||
// expect(
|
||||
// () => getPrevParticleIndexInfinte({
|
||||
// currentParticleIndex,
|
||||
// particlesCount,
|
||||
// })
|
||||
// ).toThrowError('particlesCount must be at least 1')
|
||||
// })
|
||||
// })
|
||||
describe('getPrevParticleIndexInfinte', () => {
|
||||
it('returns prev page index as expected', () => {
|
||||
const testCases = [
|
||||
{ currentParticleIndex: -5, particlesCount: 7, particlesToScroll: 2, expected: 0 },
|
||||
{ currentParticleIndex: 2, particlesCount: 7, particlesToScroll: 2, expected: 0 },
|
||||
{ currentParticleIndex: 2, particlesCount: 7, particlesToScroll: 2, expected: 0 },
|
||||
{ currentParticleIndex: 10, particlesCount: 7, particlesToScroll: 2, expected: 4 },
|
||||
]
|
||||
testCases.forEach(({
|
||||
currentParticleIndex,
|
||||
particlesCount,
|
||||
particlesToScroll,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getPrevParticleIndexInfinte({
|
||||
currentParticleIndex,
|
||||
particlesCount,
|
||||
particlesToScroll,
|
||||
})).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if particlesCount is less than 1', () => {
|
||||
const currentParticleIndex = 2
|
||||
const particlesCount = 7
|
||||
const particlesToScroll = 2
|
||||
|
||||
expect(
|
||||
() => getPrevParticleIndexInfinte({
|
||||
currentParticleIndex,
|
||||
particlesCount,
|
||||
particlesToScroll,
|
||||
})
|
||||
).toThrowError('particlesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getPartialPageSize', () => {
|
||||
it('returns result as expected if particlesToShow <= particlesToScroll', () => {
|
||||
const testCases = [
|
||||
{
|
||||
particlesCountWithoutClones: 9,
|
||||
const testCases = [{
|
||||
pagesCountWithoutClones: 9,
|
||||
particlesToShow: 2,
|
||||
particlesToScroll: 3,
|
||||
expected: 0,
|
||||
}, {
|
||||
particlesCountWithoutClones: 15,
|
||||
pagesCountWithoutClones: 15,
|
||||
particlesToShow: 4,
|
||||
particlesToScroll: 5,
|
||||
expected: 0,
|
||||
}, {
|
||||
particlesCountWithoutClones: 16,
|
||||
pagesCountWithoutClones: 16,
|
||||
particlesToShow: 4,
|
||||
particlesToScroll: 5,
|
||||
expected: 1,
|
||||
}, {
|
||||
particlesCountWithoutClones: 17,
|
||||
pagesCountWithoutClones: 17,
|
||||
particlesToShow: 4,
|
||||
particlesToScroll: 5,
|
||||
expected: 2,
|
||||
}, {
|
||||
particlesCountWithoutClones: 18,
|
||||
pagesCountWithoutClones: 18,
|
||||
particlesToShow: 4,
|
||||
particlesToScroll: 5,
|
||||
expected: 3,
|
||||
}, {
|
||||
particlesCountWithoutClones: 8,
|
||||
pagesCountWithoutClones: 8,
|
||||
particlesToShow: 2,
|
||||
particlesToScroll: 2,
|
||||
expected: 0,
|
||||
}
|
||||
]
|
||||
}]
|
||||
|
||||
testCases.forEach(({
|
||||
particlesCountWithoutClones,
|
||||
pagesCountWithoutClones,
|
||||
particlesToShow,
|
||||
particlesToScroll,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getPartialPageSize({
|
||||
particlesCountWithoutClones,
|
||||
pagesCountWithoutClones,
|
||||
particlesToShow,
|
||||
particlesToScroll,
|
||||
})).toBe(expected)
|
||||
@@ -190,58 +196,56 @@ describe('getPartialPageSize', () => {
|
||||
})
|
||||
|
||||
it('returns result as expected if particlesToShow > particlesToScroll', () => {
|
||||
const testCases = [
|
||||
{
|
||||
particlesCountWithoutClones: 8,
|
||||
const testCases = [{
|
||||
pagesCountWithoutClones: 8,
|
||||
particlesToShow: 4,
|
||||
particlesToScroll: 2,
|
||||
expected: 2,
|
||||
}, {
|
||||
particlesCountWithoutClones: 7,
|
||||
pagesCountWithoutClones: 7,
|
||||
particlesToShow: 4,
|
||||
particlesToScroll: 3,
|
||||
expected: 1,
|
||||
}, {
|
||||
particlesCountWithoutClones: 8,
|
||||
pagesCountWithoutClones: 8,
|
||||
particlesToShow: 4,
|
||||
particlesToScroll: 3,
|
||||
expected: 2,
|
||||
}, {
|
||||
particlesCountWithoutClones: 8,
|
||||
pagesCountWithoutClones: 8,
|
||||
particlesToShow: 2,
|
||||
particlesToScroll: 2,
|
||||
expected: 0,
|
||||
}, {
|
||||
particlesCountWithoutClones: 9,
|
||||
pagesCountWithoutClones: 9,
|
||||
particlesToShow: 4,
|
||||
particlesToScroll: 3,
|
||||
expected: 3,
|
||||
}, {
|
||||
particlesCountWithoutClones: 8,
|
||||
pagesCountWithoutClones: 8,
|
||||
particlesToShow: 3,
|
||||
particlesToScroll: 2,
|
||||
expected: 2,
|
||||
}, {
|
||||
particlesCountWithoutClones: 6,
|
||||
pagesCountWithoutClones: 6,
|
||||
particlesToShow: 3,
|
||||
particlesToScroll: 1,
|
||||
expected: 2,
|
||||
}, {
|
||||
particlesCountWithoutClones: 7,
|
||||
pagesCountWithoutClones: 7,
|
||||
particlesToShow: 3,
|
||||
particlesToScroll: 1,
|
||||
expected: 2,
|
||||
}
|
||||
]
|
||||
}]
|
||||
|
||||
testCases.forEach(({
|
||||
particlesCountWithoutClones,
|
||||
pagesCountWithoutClones,
|
||||
particlesToShow,
|
||||
particlesToScroll,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getPartialPageSize({
|
||||
particlesCountWithoutClones,
|
||||
pagesCountWithoutClones,
|
||||
particlesToShow,
|
||||
particlesToScroll,
|
||||
})).toBe(expected)
|
||||
|
||||
Reference in New Issue
Block a user