diff --git a/src/utils/lazy.test.js b/src/utils/lazy.test.js index 96bea24..02e9add 100644 --- a/src/utils/lazy.test.js +++ b/src/utils/lazy.test.js @@ -73,74 +73,67 @@ describe('getIndexesOfPagesWithoutClonesInScroll', () => { describe('getAdjacentIndexes', () => { it('returns indexes as expected if it is infinite', () => { - const testCases = [ - { - pageIndex: 0, - pagesCount: 2, - particlesCount: 4, - particlesToShow: 2, - particlesToScroll: 2, - expected: { - pageIndexes: [0, 1], - particleIndexes: [0, 1, 2, 3], - }, + const testCases = [{ + pageIndex: 0, + pagesCount: 2, + particlesCount: 4, + particlesToShow: 2, + particlesToScroll: 2, + expected: { + pageIndexes: [0, 1], + particleIndexes: [0, 1, 2, 3], }, - { - pageIndex: -5, - pagesCount: 5, - particlesCount: 10, - particlesToShow: 2, - particlesToScroll: 2, - expected: { - pageIndexes: [0, 1, 4], - particleIndexes: [0, 1, 2, 3, 8, 9], - }, + }, { + pageIndex: -5, + pagesCount: 5, + particlesCount: 10, + particlesToShow: 2, + particlesToScroll: 2, + expected: { + pageIndexes: [0, 1, 4], + particleIndexes: [0, 1, 2, 3, 8, 9], }, - { - pageIndex: 0, - pagesCount: 5, - particlesCount: 10, - particlesToShow: 2, - particlesToScroll: 2, - expected: { - pageIndexes: [0, 1, 4], - particleIndexes: [0, 1, 2, 3, 8, 9], - }, + }, { + pageIndex: 0, + pagesCount: 5, + particlesCount: 10, + particlesToShow: 2, + particlesToScroll: 2, + expected: { + pageIndexes: [0, 1, 4], + particleIndexes: [0, 1, 2, 3, 8, 9], }, - { - pageIndex: 2, - pagesCount: 5, - particlesCount: 10, - particlesToShow: 2, - particlesToScroll: 2, - expected: { - pageIndexes: [0, 1, 2, 3, 4], - particleIndexes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], - }, + }, { + pageIndex: 2, + pagesCount: 5, + particlesCount: 10, + particlesToShow: 2, + particlesToScroll: 2, + expected: { + pageIndexes: [0, 1, 2, 3, 4], + particleIndexes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], }, - { - pageIndex: 4, - pagesCount: 5, - particlesCount: 10, - particlesToShow: 2, - particlesToScroll: 2, - expected: { - pageIndexes: [0, 3, 4], - particleIndexes: [0, 1, 6, 7, 8, 9], - }, + }, { + pageIndex: 4, + pagesCount: 5, + particlesCount: 10, + particlesToShow: 2, + particlesToScroll: 2, + expected: { + pageIndexes: [0, 3, 4], + particleIndexes: [0, 1, 6, 7, 8, 9], }, - { - pageIndex: 15, - pagesCount: 5, - particlesCount: 10, - particlesToShow: 2, - particlesToScroll: 2, - expected: { - pageIndexes: [0, 3, 4], - particleIndexes: [0, 1, 6, 7, 8, 9], - }, + }, { + pageIndex: 15, + pagesCount: 5, + particlesCount: 10, + particlesToShow: 2, + particlesToScroll: 2, + expected: { + pageIndexes: [0, 3, 4], + particleIndexes: [0, 1, 6, 7, 8, 9], }, - ] + }] testCases.forEach(({ pageIndex, pagesCount, @@ -161,74 +154,67 @@ describe('getAdjacentIndexes', () => { }) it('returns indexes as expected if it is not infinite', () => { - const testCases = [ - { - pageIndex: 0, - pagesCount: 2, - particlesCount: 4, - particlesToShow: 2, - particlesToScroll: 2, - expected: { - pageIndexes: [0, 1], - particleIndexes: [0, 1, 2, 3], - }, + const testCases = [{ + pageIndex: 0, + pagesCount: 2, + particlesCount: 4, + particlesToShow: 2, + particlesToScroll: 2, + expected: { + pageIndexes: [0, 1], + particleIndexes: [0, 1, 2, 3], }, - { - pageIndex: -5, - pagesCount: 5, - particlesCount: 10, - particlesToShow: 2, - particlesToScroll: 2, - expected: { - pageIndexes: [0, 1, 4], - particleIndexes: [0, 1, 2, 3, 8, 9], - }, + }, { + pageIndex: -5, + pagesCount: 5, + particlesCount: 10, + particlesToShow: 2, + particlesToScroll: 2, + expected: { + pageIndexes: [0, 1, 4], + particleIndexes: [0, 1, 2, 3, 8, 9], }, - { - pageIndex: 0, - pagesCount: 5, - particlesCount: 10, - particlesToShow: 2, - particlesToScroll: 2, - expected: { - pageIndexes: [0, 1, 4], - particleIndexes: [0, 1, 2, 3, 8, 9], - }, + }, { + pageIndex: 0, + pagesCount: 5, + particlesCount: 10, + particlesToShow: 2, + particlesToScroll: 2, + expected: { + pageIndexes: [0, 1, 4], + particleIndexes: [0, 1, 2, 3, 8, 9], }, - { - pageIndex: 2, - pagesCount: 5, - particlesCount: 10, - particlesToShow: 2, - particlesToScroll: 2, - expected: { - pageIndexes: [0, 1, 2, 3, 4], - particleIndexes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], - }, + }, { + pageIndex: 2, + pagesCount: 5, + particlesCount: 10, + particlesToShow: 2, + particlesToScroll: 2, + expected: { + pageIndexes: [0, 1, 2, 3, 4], + particleIndexes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], }, - { - pageIndex: 4, - pagesCount: 5, - particlesCount: 10, - particlesToShow: 2, - particlesToScroll: 2, - expected: { - pageIndexes: [0, 3, 4], - particleIndexes: [0, 1, 6, 7, 8, 9], - }, + }, { + pageIndex: 4, + pagesCount: 5, + particlesCount: 10, + particlesToShow: 2, + particlesToScroll: 2, + expected: { + pageIndexes: [0, 3, 4], + particleIndexes: [0, 1, 6, 7, 8, 9], }, - { - pageIndex: 15, - pagesCount: 5, - particlesCount: 10, - particlesToShow: 2, - particlesToScroll: 2, - expected: { - pageIndexes: [0, 3, 4], - particleIndexes: [0, 1, 6, 7, 8, 9], - }, + }, { + pageIndex: 15, + pagesCount: 5, + particlesCount: 10, + particlesToShow: 2, + particlesToScroll: 2, + expected: { + pageIndexes: [0, 3, 4], + particleIndexes: [0, 1, 6, 7, 8, 9], }, - ] + }] testCases.forEach(({ pageIndex, pagesCount, diff --git a/src/utils/math.js b/src/utils/math.js index ffc08cd..b33900d 100644 --- a/src/utils/math.js +++ b/src/utils/math.js @@ -7,4 +7,4 @@ export const getDistance = (p1, p2) => { export function getValueInRange(min, value, max) { return Math.max(min, Math.min(value, max)) -} \ No newline at end of file +} diff --git a/src/utils/page.js b/src/utils/page.js index d0e0a98..5bcc0da 100644 --- a/src/utils/page.js +++ b/src/utils/page.js @@ -2,7 +2,6 @@ import { getValueInRange, } from './math' - // getCurrentPageIndexByCurrentParticleIndex export function _getCurrentPageIndexByCurrentParticleIndexInfinite({ diff --git a/src/utils/page.test.js b/src/utils/page.test.js index 4bdf332..7a9cbb0 100644 --- a/src/utils/page.test.js +++ b/src/utils/page.test.js @@ -120,42 +120,42 @@ describe('getPartialPageSize', () => { }) describe('_getCurrentPageIndexByCurrentParticleIndexInfinite', () => { - it('returns result as expected if particlesCount: 12; clonesCountHead: 2; clonesCountTotal: 5; particlesToScroll: 2 (particlesToShow: 3)', () => { + it('returns result as expected if particlesCount: 13; clonesCountHead: 2; clonesCountTotal: 5; particlesToScroll: 2 (particlesToShow: 3)', () => { const testCases = [{ - particlesCount: 12, + particlesCount: 13, clonesCountHead: 2, clonesCountTotal: 5, particlesToScroll: 2, currentParticleIndex: 0, expected: 3, }, { - particlesCount: 12, + particlesCount: 13, clonesCountHead: 2, clonesCountTotal: 5, particlesToScroll: 2, currentParticleIndex: 2, expected: 0, }, { - particlesCount: 12, + particlesCount: 13, clonesCountHead: 2, clonesCountTotal: 5, particlesToScroll: 2, currentParticleIndex: 4, expected: 1, }, { - particlesCount: 12, + particlesCount: 13, clonesCountHead: 2, clonesCountTotal: 5, particlesToScroll: 2, currentParticleIndex: 8, expected: 3, }, { - particlesCount: 12, + particlesCount: 13, clonesCountHead: 2, clonesCountTotal: 5, particlesToScroll: 2, currentParticleIndex: 10, - expected: 0, + expected: 4, }] testCases.forEach(({ @@ -488,3 +488,261 @@ describe('_getPagesCountByParticlesCountLimited', () => { }) }) }) + +describe('_getParticleIndexByPageIndexInfinite', () => { + it('returns result as expected if particlesCount: 11; clonesCountHead: 2; clonesCountTail: 3; particlesToScroll: 2 (particlesToShow: 3)', () => { + const testCases = [{ + pageIndex: 0, + clonesCountHead: 1, + clonesCountTail: 3, + particlesToScroll: 2, + particlesCount: 11, + expected: 1, + }, { + pageIndex: 1, + clonesCountHead: 1, + clonesCountTail: 3, + particlesToScroll: 2, + particlesCount: 11, + expected: 3, + }, { + pageIndex: 2, + clonesCountHead: 1, + clonesCountTail: 3, + particlesToScroll: 2, + particlesCount: 11, + expected: 5, + }, { + pageIndex: 3, + clonesCountHead: 1, + clonesCountTail: 3, + particlesToScroll: 2, + particlesCount: 11, + expected: 7, + }] + + testCases.forEach(({ + pageIndex, + clonesCountHead, + clonesCountTail, + particlesToScroll, + particlesCount, + expected, + }) => { + expect(_getParticleIndexByPageIndexInfinite({ + pageIndex, + clonesCountHead, + clonesCountTail, + particlesToScroll, + particlesCount, + })).toBe(expected) + }) + }) + + it('returns result as expected if particlesCount: 10; clonesCountHead: 1; clonesCountTail: 2; particlesToScroll: 2 (particlesToShow: 2)', () => { + const testCases = [{ + pageIndex: 0, + clonesCountHead: 1, + clonesCountTail: 2, + particlesToScroll: 2, + particlesCount: 10, + expected: 1, + }, { + pageIndex: 1, + clonesCountHead: 1, + clonesCountTail: 2, + particlesToScroll: 2, + particlesCount: 10, + expected: 3, + }, { + pageIndex: 2, + clonesCountHead: 1, + clonesCountTail: 2, + particlesToScroll: 2, + particlesCount: 10, + expected: 5, + }, { + pageIndex: 3, + clonesCountHead: 1, + clonesCountTail: 2, + particlesToScroll: 2, + particlesCount: 10, + expected: 7, + }] + + testCases.forEach(({ + pageIndex, + clonesCountHead, + clonesCountTail, + particlesToScroll, + particlesCount, + expected, + }) => { + expect(_getParticleIndexByPageIndexInfinite({ + pageIndex, + clonesCountHead, + clonesCountTail, + particlesToScroll, + particlesCount, + })).toBe(expected) + }) + }) + + it('returns result as expected if particlesCount: 10; clonesCountHead: 1; clonesCountTail: 2; particlesToScroll: 3 (particlesToShow: 2)', () => { + const testCases = [{ + pageIndex: 0, + clonesCountHead: 1, + clonesCountTail: 2, + particlesToScroll: 3, + particlesCount: 10, + expected: 1, + }, { + pageIndex: 1, + clonesCountHead: 1, + clonesCountTail: 2, + particlesToScroll: 3, + particlesCount: 10, + expected: 4, + }, { + pageIndex: 2, + clonesCountHead: 1, + clonesCountTail: 2, + particlesToScroll: 3, + particlesCount: 10, + expected: 7, + }] + + testCases.forEach(({ + pageIndex, + clonesCountHead, + clonesCountTail, + particlesToScroll, + particlesCount, + expected, + }) => { + expect(_getParticleIndexByPageIndexInfinite({ + pageIndex, + clonesCountHead, + clonesCountTail, + particlesToScroll, + particlesCount, + })).toBe(expected) + }) + }) +}) + +describe('_getParticleIndexByPageIndexLimited', () => { + it('returns result as expected if particlesCount: 6; particlesToScroll: 2; particlesToShow: 3', () => { + const testCases = [{ + pageIndex: 0, + particlesToScroll: 2, + particlesCount: 6, + particlesToShow: 3, + expected: 0, + }, { + pageIndex: 1, + particlesToScroll: 2, + particlesCount: 6, + particlesToShow: 3, + expected: 2, + }, { + pageIndex: 2, + particlesToScroll: 2, + particlesCount: 6, + particlesToShow: 3, + expected: 3, + }] + + testCases.forEach(({ + pageIndex, + particlesToScroll, + particlesCount, + particlesToShow, + expected, + }) => { + expect(_getParticleIndexByPageIndexLimited({ + pageIndex, + particlesToScroll, + particlesCount, + particlesToShow, + particlesCount, + })).toBe(expected) + }) + }) + + it('returns result as expected if particlesCount: 6; particlesToScroll: 2; particlesToShow: 2', () => { + const testCases = [{ + pageIndex: 0, + particlesToScroll: 2, + particlesCount: 6, + particlesToShow: 2, + expected: 0, + }, { + pageIndex: 1, + particlesToScroll: 2, + particlesCount: 6, + particlesToShow: 2, + expected: 2, + }, { + pageIndex: 2, + particlesToScroll: 2, + particlesCount: 6, + particlesToShow: 2, + expected: 4, + }] + + testCases.forEach(({ + pageIndex, + particlesToScroll, + particlesCount, + particlesToShow, + expected, + }) => { + expect(_getParticleIndexByPageIndexLimited({ + pageIndex, + particlesToScroll, + particlesCount, + particlesToShow, + particlesCount, + })).toBe(expected) + }) + }) + + it('returns result as expected if particlesCount: 6; particlesToScroll: 3; particlesToShow: 2', () => { + const testCases = [{ + pageIndex: 0, + particlesToScroll: 3, + particlesCount: 6, + particlesToShow: 2, + expected: 0, + }, { + pageIndex: 1, + particlesToScroll: 3, + particlesCount: 6, + particlesToShow: 2, + expected: 3, + }, { + pageIndex: 2, + particlesToScroll: 3, + particlesCount: 6, + particlesToShow: 2, + expected: 4, + }] + + testCases.forEach(({ + pageIndex, + particlesToScroll, + particlesCount, + particlesToShow, + expected, + }) => { + expect(_getParticleIndexByPageIndexLimited({ + pageIndex, + particlesToScroll, + particlesCount, + particlesToShow, + particlesCount, + })).toBe(expected) + }) + }) +})