Merge pull request #15 from vadimkorr/feature/#14_Setup-jest-test
Feature/#14 setup jest test
This commit is contained in:
15
.babelrc
Normal file
15
.babelrc
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"presets": [
|
||||||
|
["@babel/preset-env", {
|
||||||
|
"modules": false,
|
||||||
|
"targets": {
|
||||||
|
"browsers": "ie >= 11"
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
],
|
||||||
|
"env": {
|
||||||
|
"test": {
|
||||||
|
"presets": [["@babel/preset-env"]]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,10 +28,12 @@
|
|||||||
"build-storybook": "build-storybook",
|
"build-storybook": "build-storybook",
|
||||||
"build:docs": "set DOCS=true && rollup -c",
|
"build:docs": "set DOCS=true && rollup -c",
|
||||||
"dev:docs": "set DOCS=true && rollup -c -w",
|
"dev:docs": "set DOCS=true && rollup -c -w",
|
||||||
|
"test": "jest",
|
||||||
"prepublishOnly": "npm run build"
|
"prepublishOnly": "npm run build"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "^7.12.10",
|
"@babel/core": "^7.12.10",
|
||||||
|
"@babel/preset-env": "^7.12.11",
|
||||||
"@rollup/plugin-commonjs": "^17.0.0",
|
"@rollup/plugin-commonjs": "^17.0.0",
|
||||||
"@rollup/plugin-json": "^4.1.0",
|
"@rollup/plugin-json": "^4.1.0",
|
||||||
"@rollup/plugin-node-resolve": "^11.0.0",
|
"@rollup/plugin-node-resolve": "^11.0.0",
|
||||||
@@ -40,9 +42,11 @@
|
|||||||
"@storybook/addon-links": "^6.1.14",
|
"@storybook/addon-links": "^6.1.14",
|
||||||
"@storybook/svelte": "^6.1.14",
|
"@storybook/svelte": "^6.1.14",
|
||||||
"babel-loader": "^8.2.2",
|
"babel-loader": "^8.2.2",
|
||||||
|
"jest": "^26.6.3",
|
||||||
"lodash": "^4.17.20",
|
"lodash": "^4.17.20",
|
||||||
"mdsvex": "^0.8.9",
|
"mdsvex": "^0.8.9",
|
||||||
"rollup": "^2.3.4",
|
"rollup": "^2.3.4",
|
||||||
|
"rollup-plugin-babel": "^4.4.0",
|
||||||
"rollup-plugin-css-only": "^3.1.0",
|
"rollup-plugin-css-only": "^3.1.0",
|
||||||
"rollup-plugin-livereload": "^2.0.0",
|
"rollup-plugin-livereload": "^2.0.0",
|
||||||
"rollup-plugin-svelte": "^7.0.0",
|
"rollup-plugin-svelte": "^7.0.0",
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import resolve from '@rollup/plugin-node-resolve';
|
|||||||
import json from '@rollup/plugin-json';
|
import json from '@rollup/plugin-json';
|
||||||
import livereload from 'rollup-plugin-livereload';
|
import livereload from 'rollup-plugin-livereload';
|
||||||
import { terser } from 'rollup-plugin-terser';
|
import { terser } from 'rollup-plugin-terser';
|
||||||
|
import babel from 'rollup-plugin-babel';
|
||||||
import css from 'rollup-plugin-css-only';
|
import css from 'rollup-plugin-css-only';
|
||||||
import { mdsvex } from "mdsvex";
|
import { mdsvex } from "mdsvex";
|
||||||
|
|
||||||
@@ -97,6 +98,10 @@ export default {
|
|||||||
production && terser(),
|
production && terser(),
|
||||||
|
|
||||||
json(),
|
json(),
|
||||||
|
|
||||||
|
babel({
|
||||||
|
exclude: 'node_modules/**',
|
||||||
|
})
|
||||||
],
|
],
|
||||||
watch: {
|
watch: {
|
||||||
clearScreen: false
|
clearScreen: false
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
addResizeEventListener,
|
addResizeEventListener,
|
||||||
removeResizeEventListener
|
removeResizeEventListener
|
||||||
} from '../../utils/event'
|
} from '../../utils/event'
|
||||||
|
import { getAdjacentIndexes } from '../../utils/page'
|
||||||
|
|
||||||
const directionFnDescription = {
|
const directionFnDescription = {
|
||||||
[NEXT]: showNextPage,
|
[NEXT]: showNextPage,
|
||||||
@@ -60,19 +61,14 @@
|
|||||||
let currentPageIndex = 0
|
let currentPageIndex = 0
|
||||||
$: originalCurrentPageIndex = currentPageIndex - Number(infinite);
|
$: originalCurrentPageIndex = currentPageIndex - Number(infinite);
|
||||||
let pagesCount = 0
|
let pagesCount = 0
|
||||||
$: originalPagesCount = Math.max(pagesCount - (infinite ? 2 : 0), 0) // without clones
|
$: originalPagesCount = Math.max(pagesCount - (infinite ? 2 : 0), 1) // without clones
|
||||||
let pageWidth = 0
|
let pageWidth = 0
|
||||||
let offset = 0
|
let offset = 0
|
||||||
let pageWindowElement
|
let pageWindowElement
|
||||||
let pagesElement
|
let pagesElement
|
||||||
|
|
||||||
// used for lazy loading images, preloaded only current, adjacent and cloanable images
|
// used for lazy loading images, preloaded only current, adjacent and cloanable images
|
||||||
$: loaded = [
|
$: loaded = getAdjacentIndexes(originalCurrentPageIndex, originalPagesCount)
|
||||||
...new Set([
|
|
||||||
0, originalPagesCount-1,
|
|
||||||
originalCurrentPageIndex - 1, originalCurrentPageIndex, originalCurrentPageIndex + 1
|
|
||||||
])
|
|
||||||
].filter(index => index >= 0)
|
|
||||||
|
|
||||||
function applyPageSizes() {
|
function applyPageSizes() {
|
||||||
const children = pagesElement.children
|
const children = pagesElement.children
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import { writable } from 'svelte/store';
|
import { writable } from 'svelte/store';
|
||||||
import { getNextPageIndexFn, getPrevPageIndexFn } from './utils/page'
|
import {
|
||||||
|
getNextPageIndexFn,
|
||||||
|
getPrevPageIndexFn,
|
||||||
|
getPageIndex
|
||||||
|
} from './utils/page'
|
||||||
|
|
||||||
const initState = {
|
const initState = {
|
||||||
currentPageIndex: 0,
|
currentPageIndex: 0,
|
||||||
@@ -26,7 +30,7 @@ function createStore() {
|
|||||||
update(store => {
|
update(store => {
|
||||||
return {
|
return {
|
||||||
...store,
|
...store,
|
||||||
currentPageIndex: pageIndex < 0 ? 0 : Math.min(pageIndex, pagesCount - 1),
|
currentPageIndex: getPageIndex(pageIndex, pagesCount),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
export function getNextPageIndexLimited(currentPageIndex, pagesCount) {
|
export function getNextPageIndexLimited(currentPageIndex, pagesCount) {
|
||||||
return Math.min(currentPageIndex + 1, pagesCount - 1)
|
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||||
|
return Math.min(Math.max(currentPageIndex + 1, 0), pagesCount - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getNextPageIndexInfinte(currentPageIndex, pagesCount) {
|
export function getNextPageIndexInfinte(currentPageIndex, pagesCount) {
|
||||||
const newCurrentPageIndex = currentPageIndex + 1
|
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||||
return newCurrentPageIndex > pagesCount - 1 ? 0 : newCurrentPageIndex
|
const newCurrentPageIndex = Math.max(currentPageIndex, 0) + 1
|
||||||
|
return newCurrentPageIndex > pagesCount - 1 ? 0 : Math.max(newCurrentPageIndex, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getNextPageIndexFn(infinite) {
|
export function getNextPageIndexFn(infinite) {
|
||||||
@@ -12,14 +14,39 @@ export function getNextPageIndexFn(infinite) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getPrevPageIndexLimited(currentPageIndex, pagesCount) {
|
export function getPrevPageIndexLimited(currentPageIndex, pagesCount) {
|
||||||
return Math.max(currentPageIndex - 1, 0)
|
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||||
|
return Math.max(Math.min(currentPageIndex - 1, pagesCount - 1), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPrevPageIndexInfinte(currentPageIndex, pagesCount) {
|
export function getPrevPageIndexInfinte(currentPageIndex, pagesCount) {
|
||||||
const newCurrentPageIndex = currentPageIndex - 1
|
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||||
return newCurrentPageIndex >= 0 ? newCurrentPageIndex : pagesCount - 1
|
const newCurrentPageIndex = Math.min(currentPageIndex, pagesCount - 1) - 1
|
||||||
|
return newCurrentPageIndex >= 0 ? Math.min(newCurrentPageIndex, pagesCount - 1) : pagesCount - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPrevPageIndexFn(infinite) {
|
export function getPrevPageIndexFn(infinite) {
|
||||||
return infinite ? getPrevPageIndexInfinte : getPrevPageIndexLimited
|
return infinite ? getPrevPageIndexInfinte : getPrevPageIndexLimited
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getPageIndex(pageIndex, pagesCount) {
|
||||||
|
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||||
|
return pageIndex < 0 ? 0 : Math.min(pageIndex, pagesCount - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getAdjacentIndexes(pageIndex, pagesCount, infinite) {
|
||||||
|
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||||
|
const _pageIndex = Math.max(0, Math.min(pageIndex, pagesCount - 1))
|
||||||
|
let rangeStart = _pageIndex - 1;
|
||||||
|
let rangeEnd = _pageIndex + 1;
|
||||||
|
rangeStart = rangeStart < 0
|
||||||
|
? infinite
|
||||||
|
? pagesCount - 1
|
||||||
|
: 0
|
||||||
|
: rangeStart
|
||||||
|
rangeEnd = rangeEnd > pagesCount - 1
|
||||||
|
? infinite
|
||||||
|
? 0
|
||||||
|
: pagesCount - 1
|
||||||
|
: rangeEnd
|
||||||
|
return [...new Set([rangeStart, rangeEnd, _pageIndex])].sort((a, b) => a - b)
|
||||||
|
}
|
||||||
|
|||||||
155
src/utils/page.test.js
Normal file
155
src/utils/page.test.js
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
import {
|
||||||
|
getNextPageIndexLimited,
|
||||||
|
getNextPageIndexInfinte,
|
||||||
|
getPrevPageIndexLimited,
|
||||||
|
getPrevPageIndexInfinte,
|
||||||
|
getPageIndex,
|
||||||
|
getAdjacentIndexes
|
||||||
|
} from './page.js'
|
||||||
|
|
||||||
|
describe('getNextPageIndexLimited', () => {
|
||||||
|
it('returns next page index as expected', () => {
|
||||||
|
const testCases = [
|
||||||
|
{ currentPageIndex: -5, pagesCount: 3, expected: 0 },
|
||||||
|
{ currentPageIndex: 0, pagesCount: 3, expected: 1 },
|
||||||
|
{ currentPageIndex: 1, pagesCount: 3, expected: 2 },
|
||||||
|
{ currentPageIndex: 2, pagesCount: 3, expected: 2 },
|
||||||
|
{ currentPageIndex: 7, pagesCount: 3, expected: 2 },
|
||||||
|
]
|
||||||
|
testCases.forEach(({ currentPageIndex, pagesCount, 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', () => {
|
||||||
|
const testCases = [
|
||||||
|
{ currentPageIndex: -5, pagesCount: 3, expected: 1 },
|
||||||
|
{ currentPageIndex: 0, pagesCount: 3, expected: 1 },
|
||||||
|
{ currentPageIndex: 1, pagesCount: 3, expected: 2 },
|
||||||
|
{ currentPageIndex: 2, pagesCount: 3, expected: 0 },
|
||||||
|
{ currentPageIndex: 7, pagesCount: 3, expected: 0 },
|
||||||
|
]
|
||||||
|
testCases.forEach(({ currentPageIndex, pagesCount, 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', () => {
|
||||||
|
const testCases = [
|
||||||
|
{ currentPageIndex: -5, pagesCount: 3, expected: 0 },
|
||||||
|
{ currentPageIndex: 0, pagesCount: 3, expected: 0 },
|
||||||
|
{ currentPageIndex: 1, pagesCount: 3, expected: 0 },
|
||||||
|
{ currentPageIndex: 2, pagesCount: 3, expected: 1 },
|
||||||
|
{ currentPageIndex: 7, pagesCount: 3, expected: 2 },
|
||||||
|
]
|
||||||
|
testCases.forEach(({ currentPageIndex, pagesCount, 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', () => {
|
||||||
|
const testCases = [
|
||||||
|
{ currentPageIndex: -5, pagesCount: 3, expected: 2 },
|
||||||
|
{ currentPageIndex: 0, pagesCount: 3, expected: 2 },
|
||||||
|
{ currentPageIndex: 1, pagesCount: 3, expected: 0 },
|
||||||
|
{ currentPageIndex: 2, pagesCount: 3, expected: 1 },
|
||||||
|
{ currentPageIndex: 7, pagesCount: 3, expected: 1 },
|
||||||
|
]
|
||||||
|
testCases.forEach(({ currentPageIndex, pagesCount, 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', () => {
|
||||||
|
const testCases = [
|
||||||
|
{ pageIndex: -5, pagesCount: 3, expected: 0 },
|
||||||
|
{ pageIndex: 0, pagesCount: 3, expected: 0 },
|
||||||
|
{ pageIndex: 1, pagesCount: 3, expected: 1 },
|
||||||
|
{ pageIndex: 2, pagesCount: 3, expected: 2 },
|
||||||
|
{ pageIndex: 7, pagesCount: 3, expected: 2 },
|
||||||
|
]
|
||||||
|
testCases.forEach(({ pageIndex, pagesCount, 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