From 0bdb14cbf3531adba94c25c4de3cdcc059eab814 Mon Sep 17 00:00:00 2001 From: Vadim Date: Wed, 27 Jan 2021 21:17:40 +0300 Subject: [PATCH] Added tests for page --- src/utils/page.test.js | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/utils/page.test.js diff --git a/src/utils/page.test.js b/src/utils/page.test.js new file mode 100644 index 0000000..f491a2d --- /dev/null +++ b/src/utils/page.test.js @@ -0,0 +1,66 @@ +import { + getNextPageIndexLimited, + getNextPageIndexInfinte, + getPrevPageIndexLimited, + getPrevPageIndexInfinte +} 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); + }); + }); +}); + +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); + }); + }); +}); + +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); + }); + }); +}); + +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); + }); + }); +});