#69 : Add unit tests

This commit is contained in:
Vadim
2021-09-12 18:38:00 +03:00
parent 89b32eda32
commit beaf6989d0
4 changed files with 202 additions and 24 deletions

View File

@@ -14,7 +14,7 @@
} from '../../utils/event' } from '../../utils/event'
import { import {
applyParticleSizes, applyParticleSizes,
getCurrentPageIndex, getCurrentPageIndexByCurrentParticleIndex,
getPartialPageSize, getPartialPageSize,
getPagesCountByParticlesCount, getPagesCountByParticlesCount,
getParticleIndexByPageIndex, getParticleIndexByPageIndex,
@@ -157,10 +157,11 @@
}) })
let currentParticleIndex = 0 let currentParticleIndex = 0
$: currentPageIndex = getCurrentPageIndex({ $: currentPageIndex = getCurrentPageIndexByCurrentParticleIndex({
currentParticleIndex, currentParticleIndex,
particlesCount, particlesCount,
headClonesCount: clonesCount.head, clonesCountHead: clonesCount.head,
clonesCountTotal: clonesCount.total,
infinite, infinite,
particlesToScroll, particlesToScroll,
}) })
@@ -232,8 +233,8 @@
clonesToAppend, clonesToAppend,
clonesToPrepend, clonesToPrepend,
} = getClones({ } = getClones({
headClonesCount: clonesCount.head, clonesCountHead: clonesCount.head,
tailClonesCount: clonesCount.tail, clonesCountTail: clonesCount.tail,
particlesContainerChildren: particlesContainer.children, particlesContainerChildren: particlesContainer.children,
}) })
applyClones({ applyClones({

View File

@@ -1,17 +1,17 @@
export function getClones({ export function getClones({
headClonesCount, clonesCountHead,
tailClonesCount, clonesCountTail,
particlesContainerChildren, particlesContainerChildren,
}) { }) {
// TODO: add fns to remove clones if needed // TODO: add fns to remove clones if needed
const clonesToAppend = [] const clonesToAppend = []
for (let i=0; i<tailClonesCount; i++) { for (let i=0; i<clonesCountTail; i++) {
clonesToAppend.push(particlesContainerChildren[i].cloneNode(true)) clonesToAppend.push(particlesContainerChildren[i].cloneNode(true))
} }
const clonesToPrepend = [] const clonesToPrepend = []
const len = particlesContainerChildren.length const len = particlesContainerChildren.length
for (let i=len-1; i>len-1-headClonesCount; i--) { for (let i=len-1; i>len-1-clonesCountHead; i--) {
clonesToPrepend.push(particlesContainerChildren[i].cloneNode(true)) clonesToPrepend.push(particlesContainerChildren[i].cloneNode(true))
} }

View File

@@ -12,19 +12,50 @@ export function applyParticleSizes({
} }
} }
export function getCurrentPageIndex({ // getCurrentPageIndexByCurrentParticleIndex
export function _getCurrentPageIndexByCurrentParticleIndexInfinite({
currentParticleIndex, currentParticleIndex,
particlesCount, particlesCount,
headClonesCount, clonesCountHead,
clonesCountTotal,
particlesToScroll,
}) {
if (currentParticleIndex === particlesCount - clonesCountHead) return 0
if (currentParticleIndex === 0) return _getPagesCountByParticlesCountInfinite({
particlesCountWithoutClones: particlesCount - clonesCountTotal,
particlesToScroll,
}) - 1
return Math.floor((currentParticleIndex - clonesCountHead) / particlesToScroll)
}
export function _getCurrentPageIndexByCurrentParticleIndexLimited({
currentParticleIndex,
particlesToScroll,
}) {
return Math.ceil(currentParticleIndex / particlesToScroll)
}
export function getCurrentPageIndexByCurrentParticleIndex({
currentParticleIndex,
particlesCount,
clonesCountHead,
clonesCountTotal,
infinite, infinite,
particlesToScroll, particlesToScroll,
}) { }) {
if (infinite) { return infinite
if (currentParticleIndex === particlesCount - headClonesCount) return 0 ? _getCurrentPageIndexByCurrentParticleIndexInfinite({
if (currentParticleIndex === 0) return particlesCount - headClonesCount currentParticleIndex,
return Math.floor((currentParticleIndex - headClonesCount) / particlesToScroll) particlesCount,
} clonesCountHead,
return Math.ceil(currentParticleIndex / particlesToScroll) clonesCountTotal,
particlesToScroll,
})
: _getCurrentPageIndexByCurrentParticleIndexLimited({
currentParticleIndex,
particlesToScroll,
})
} }
// TODO: think about case if particlesCount < particlesToShow and particlesCount < particlesToScroll // TODO: think about case if particlesCount < particlesToShow and particlesCount < particlesToScroll
@@ -45,17 +76,41 @@ export function getPartialPageSize({
} }
} }
// getPagesCountByParticlesCount
export function _getPagesCountByParticlesCountInfinite({
particlesCountWithoutClones,
particlesToScroll,
}) {
return Math.ceil(particlesCountWithoutClones / particlesToScroll)
}
export function _getPagesCountByParticlesCountLimited({
particlesCountWithoutClones,
particlesToScroll,
}) {
return Math.round(particlesCountWithoutClones / particlesToScroll)
}
export function getPagesCountByParticlesCount({ export function getPagesCountByParticlesCount({
infinite, infinite,
particlesCountWithoutClones, particlesCountWithoutClones,
particlesToScroll, particlesToScroll,
}) { }) {
return infinite return infinite
? Math.ceil(particlesCountWithoutClones / particlesToScroll) ? _getPagesCountByParticlesCountInfinite({
: Math.round(particlesCountWithoutClones / particlesToScroll) particlesCountWithoutClones,
particlesToScroll,
})
: _getPagesCountByParticlesCountLimited({
particlesCountWithoutClones,
particlesToScroll,
})
} }
export function getParticleIndexByPageIndexInfinite({ // getParticleIndexByPageIndex
export function _getParticleIndexByPageIndexInfinite({
pageIndex, pageIndex,
clonesCountHead, clonesCountHead,
clonesCountTail, clonesCountTail,
@@ -63,13 +118,13 @@ export function getParticleIndexByPageIndexInfinite({
particlesCount, particlesCount,
}) { }) {
return getValueInRange( return getValueInRange(
clonesCountHead, 0,
Math.min(clonesCountHead + pageIndex * particlesToScroll, particlesCount - clonesCountTail), Math.min(clonesCountHead + pageIndex * particlesToScroll, particlesCount - clonesCountTail),
particlesCount - 1 particlesCount - 1
) )
} }
export function getParticleIndexByPageIndexLimited({ export function _getParticleIndexByPageIndexLimited({
pageIndex, pageIndex,
particlesToScroll, particlesToScroll,
particlesCount, particlesCount,
@@ -92,14 +147,14 @@ export function getParticleIndexByPageIndex({
particlesToShow, particlesToShow,
}) { }) {
return infinite return infinite
? getParticleIndexByPageIndexInfinite({ ? _getParticleIndexByPageIndexInfinite({
pageIndex, pageIndex,
clonesCountHead, clonesCountHead,
clonesCountTail, clonesCountTail,
particlesToScroll, particlesToScroll,
particlesCount, particlesCount,
}) })
: getParticleIndexByPageIndexLimited({ : _getParticleIndexByPageIndexLimited({
pageIndex, pageIndex,
particlesToScroll, particlesToScroll,
particlesCount, particlesCount,

View File

@@ -1,5 +1,6 @@
import { import {
getPartialPageSize, getPartialPageSize,
_getCurrentPageIndexByCurrentParticleIndexInfinite
// getCurrentPageIndex, // getCurrentPageIndex,
// getPagesCountByParticlesCount, // getPagesCountByParticlesCount,
// getParticleIndexByPageIndexInfinite, // getParticleIndexByPageIndexInfinite,
@@ -111,3 +112,124 @@ describe('getPartialPageSize', () => {
}) })
}) })
}) })
describe('_getCurrentPageIndexByCurrentParticleIndexInfinite', () => {
it('returns result as expected if particlesCount: 12; clonesCountHead: 2; clonesCountTotal: 5; particlesToScroll: 2', () => {
const testCases = [{
particlesCount: 12,
clonesCountHead: 2,
clonesCountTotal: 5,
particlesToScroll: 2,
currentParticleIndex: 0,
expected: 3,
}, {
particlesCount: 12,
clonesCountHead: 2,
clonesCountTotal: 5,
particlesToScroll: 2,
currentParticleIndex: 2,
expected: 0,
}, {
particlesCount: 12,
clonesCountHead: 2,
clonesCountTotal: 5,
particlesToScroll: 2,
currentParticleIndex: 4,
expected: 1,
}, {
particlesCount: 12,
clonesCountHead: 2,
clonesCountTotal: 5,
particlesToScroll: 2,
currentParticleIndex: 8,
expected: 3,
}, {
particlesCount: 12,
clonesCountHead: 2,
clonesCountTotal: 5,
particlesToScroll: 2,
currentParticleIndex: 10,
expected: 0,
}]
testCases.forEach(({
currentParticleIndex,
particlesCount,
clonesCountHead,
clonesCountTotal,
particlesToScroll,
expected,
}) => {
expect(_getCurrentPageIndexByCurrentParticleIndexInfinite({
currentParticleIndex,
particlesCount,
clonesCountHead,
clonesCountTotal,
particlesToScroll,
})).toBe(expected)
})
})
it('returns result as expected if particlesCount: 10; clonesCountHead: 1; clonesCountTotal: 3; particlesToScroll: 2', () => {
const testCases = [{
particlesCount: 10,
clonesCountHead: 1,
clonesCountTotal: 3,
particlesToScroll: 2,
currentParticleIndex: 0,
expected: 3,
}, {
particlesCount: 10,
clonesCountHead: 1,
clonesCountTotal: 3,
particlesToScroll: 2,
currentParticleIndex: 1,
expected: 0,
}, {
particlesCount: 10,
clonesCountHead: 1,
clonesCountTotal: 3,
particlesToScroll: 2,
currentParticleIndex: 3,
expected: 1,
}, {
particlesCount: 10,
clonesCountHead: 1,
clonesCountTotal: 3,
particlesToScroll: 2,
currentParticleIndex: 5,
expected: 2,
}, {
particlesCount: 10,
clonesCountHead: 1,
clonesCountTotal: 3,
particlesToScroll: 2,
currentParticleIndex: 7,
expected: 3,
}, {
particlesCount: 10,
clonesCountHead: 1,
clonesCountTotal: 3,
particlesToScroll: 2,
currentParticleIndex: 9,
expected: 0,
}]
testCases.forEach(({
currentParticleIndex,
particlesCount,
clonesCountHead,
clonesCountTotal,
particlesToScroll,
expected,
}) => {
expect(_getCurrentPageIndexByCurrentParticleIndexInfinite({
currentParticleIndex,
particlesCount,
clonesCountHead,
clonesCountTotal,
particlesToScroll,
})).toBe(expected)
})
})
})