#69 : Fix case, update tests

This commit is contained in:
Vadim
2021-09-10 22:17:25 +03:00
parent f03a6d0cbe
commit 8c0a1fbd4c
4 changed files with 30 additions and 27 deletions

View File

@@ -389,6 +389,7 @@
infinite, infinite,
particlesCount, particlesCount,
particlesToScroll, particlesToScroll,
particlesToShow,
clonesCountTail: clonesCount.tail, clonesCountTail: clonesCount.tail,
}), }),
options, options,

View File

@@ -39,6 +39,7 @@ function createStore() {
infinite, infinite,
particlesCount, particlesCount,
particlesToScroll, particlesToScroll,
particlesToShow,
clonesCountTail, clonesCountTail,
}) { }) {
update(store => { update(store => {
@@ -46,6 +47,7 @@ function createStore() {
currentParticleIndex: store.currentParticleIndex, currentParticleIndex: store.currentParticleIndex,
particlesCount, particlesCount,
particlesToScroll, particlesToScroll,
particlesToShow,
clonesCountTail, clonesCountTail,
}) })
return { return {

View File

@@ -6,12 +6,10 @@ export function getNextParticleIndexLimited({
currentParticleIndex, currentParticleIndex,
particlesCount, particlesCount,
particlesToScroll, particlesToScroll,
particlesToShow,
}) { }) {
if (particlesCount < 1) throw new Error('particlesCount must be at least 1') if (particlesCount < 1) throw new Error('particlesCount must be at least 1')
return Math.max( return getValueInRange(0, currentParticleIndex + particlesToScroll, particlesCount - particlesToShow)
currentParticleIndex + Math.min(particlesCount - (currentParticleIndex + 1) - particlesToScroll, particlesToScroll),
0
)
} }
export function getNextParticleIndexInfinte({ export function getNextParticleIndexInfinte({

View File

@@ -9,21 +9,23 @@ import {
describe('getNextParticleIndexLimited', () => { describe('getNextParticleIndexLimited', () => {
it('returns next page index as expected', () => { it('returns next page index as expected', () => {
const testCases = [ const testCases = [
{ currentParticleIndex: -5, particlesCount: 7, particlesToScroll: 2, expected: 0 }, { currentParticleIndex: -5, particlesCount: 7, particlesToScroll: 2, particlesToShow: 2, expected: 0 },
{ currentParticleIndex: 0, particlesCount: 7, particlesToScroll: 2, expected: 2 }, { currentParticleIndex: 0, particlesCount: 7, particlesToScroll: 2, particlesToShow: 2, expected: 2 },
{ currentParticleIndex: 2, particlesCount: 7, particlesToScroll: 2, expected: 4 }, { currentParticleIndex: 2, particlesCount: 7, particlesToScroll: 2, particlesToShow: 2, expected: 4 },
{ currentParticleIndex: 7, particlesCount: 7, particlesToScroll: 2, expected: 4 }, { currentParticleIndex: 7, particlesCount: 7, particlesToScroll: 2, particlesToShow: 2, expected: 5 },
] ]
testCases.forEach(({ testCases.forEach(({
currentParticleIndex, currentParticleIndex,
particlesCount, particlesCount,
particlesToScroll, particlesToScroll,
particlesToShow,
expected, expected,
}) => { }) => {
expect(getNextParticleIndexLimited({ expect(getNextParticleIndexLimited({
currentParticleIndex, currentParticleIndex,
particlesCount, particlesCount,
particlesToScroll, particlesToScroll,
particlesToShow,
})).toBe(expected) })).toBe(expected)
}) })
}) })
@@ -133,7 +135,7 @@ describe('getPrevParticleIndexInfinte', () => {
}) })
it('throws error if particlesCount is less than 1', () => { it('throws error if particlesCount is less than 1', () => {
const currentParticleIndex = 2 const currentParticleIndex = 2
const particlesCount = 7 const particlesCount = 0
const particlesToScroll = 2 const particlesToScroll = 2
expect( expect(
@@ -149,45 +151,45 @@ describe('getPrevParticleIndexInfinte', () => {
describe('getPartialPageSize', () => { describe('getPartialPageSize', () => {
it('returns result as expected if particlesToShow <= particlesToScroll', () => { it('returns result as expected if particlesToShow <= particlesToScroll', () => {
const testCases = [{ const testCases = [{
pagesCountWithoutClones: 9, particlesCountWithoutClones: 9,
particlesToShow: 2, particlesToShow: 2,
particlesToScroll: 3, particlesToScroll: 3,
expected: 0, expected: 0,
}, { }, {
pagesCountWithoutClones: 15, particlesCountWithoutClones: 15,
particlesToShow: 4, particlesToShow: 4,
particlesToScroll: 5, particlesToScroll: 5,
expected: 0, expected: 0,
}, { }, {
pagesCountWithoutClones: 16, particlesCountWithoutClones: 16,
particlesToShow: 4, particlesToShow: 4,
particlesToScroll: 5, particlesToScroll: 5,
expected: 1, expected: 1,
}, { }, {
pagesCountWithoutClones: 17, particlesCountWithoutClones: 17,
particlesToShow: 4, particlesToShow: 4,
particlesToScroll: 5, particlesToScroll: 5,
expected: 2, expected: 2,
}, { }, {
pagesCountWithoutClones: 18, particlesCountWithoutClones: 18,
particlesToShow: 4, particlesToShow: 4,
particlesToScroll: 5, particlesToScroll: 5,
expected: 3, expected: 3,
}, { }, {
pagesCountWithoutClones: 8, particlesCountWithoutClones: 8,
particlesToShow: 2, particlesToShow: 2,
particlesToScroll: 2, particlesToScroll: 2,
expected: 0, expected: 0,
}] }]
testCases.forEach(({ testCases.forEach(({
pagesCountWithoutClones, particlesCountWithoutClones,
particlesToShow, particlesToShow,
particlesToScroll, particlesToScroll,
expected, expected,
}) => { }) => {
expect(getPartialPageSize({ expect(getPartialPageSize({
pagesCountWithoutClones, particlesCountWithoutClones,
particlesToShow, particlesToShow,
particlesToScroll, particlesToScroll,
})).toBe(expected) })).toBe(expected)
@@ -196,55 +198,55 @@ describe('getPartialPageSize', () => {
it('returns result as expected if particlesToShow > particlesToScroll', () => { it('returns result as expected if particlesToShow > particlesToScroll', () => {
const testCases = [{ const testCases = [{
pagesCountWithoutClones: 8, particlesCountWithoutClones: 8,
particlesToShow: 4, particlesToShow: 4,
particlesToScroll: 2, particlesToScroll: 2,
expected: 2, expected: 2,
}, { }, {
pagesCountWithoutClones: 7, particlesCountWithoutClones: 7,
particlesToShow: 4, particlesToShow: 4,
particlesToScroll: 3, particlesToScroll: 3,
expected: 1, expected: 1,
}, { }, {
pagesCountWithoutClones: 8, particlesCountWithoutClones: 8,
particlesToShow: 4, particlesToShow: 4,
particlesToScroll: 3, particlesToScroll: 3,
expected: 2, expected: 2,
}, { }, {
pagesCountWithoutClones: 8, particlesCountWithoutClones: 8,
particlesToShow: 2, particlesToShow: 2,
particlesToScroll: 2, particlesToScroll: 2,
expected: 0, expected: 0,
}, { }, {
pagesCountWithoutClones: 9, particlesCountWithoutClones: 9,
particlesToShow: 4, particlesToShow: 4,
particlesToScroll: 3, particlesToScroll: 3,
expected: 3, expected: 3,
}, { }, {
pagesCountWithoutClones: 8, particlesCountWithoutClones: 8,
particlesToShow: 3, particlesToShow: 3,
particlesToScroll: 2, particlesToScroll: 2,
expected: 2, expected: 2,
}, { }, {
pagesCountWithoutClones: 6, particlesCountWithoutClones: 6,
particlesToShow: 3, particlesToShow: 3,
particlesToScroll: 1, particlesToScroll: 1,
expected: 2, expected: 2,
}, { }, {
pagesCountWithoutClones: 7, particlesCountWithoutClones: 7,
particlesToShow: 3, particlesToShow: 3,
particlesToScroll: 1, particlesToScroll: 1,
expected: 2, expected: 2,
}] }]
testCases.forEach(({ testCases.forEach(({
pagesCountWithoutClones, particlesCountWithoutClones,
particlesToShow, particlesToShow,
particlesToScroll, particlesToScroll,
expected, expected,
}) => { }) => {
expect(getPartialPageSize({ expect(getPartialPageSize({
pagesCountWithoutClones, particlesCountWithoutClones,
particlesToShow, particlesToShow,
particlesToScroll, particlesToScroll,
})).toBe(expected) })).toBe(expected)