import {Pagination} from "../../src/common/Pagination.js"; describe('Pagination', () => { const pagination = new Pagination(3, 5, 31); test('Should have correct page', () => { expect(pagination.page).toBe(3); }); test('Should have correct perPage', () => { expect(pagination.perPage).toBe(5); }); test('Should have correct totalCount', () => { expect(pagination.totalCount).toBe(31); }); // Parameter validation test('Invalid perPage should throw', () => { expect(() => { new Pagination(1, 0, 5); }).toThrow('perPage must be >= 1'); expect(() => { new Pagination(1, -1, 5); }).toThrow('perPage must be >= 1'); expect(() => { new Pagination(1, -5, 5); }).toThrow('perPage must be >= 1'); }); test('Invalid totalCount should throw', () => { expect(() => { new Pagination(1, 5, -1); }).toThrow('totalCount must be >= 0'); expect(() => { new Pagination(2, 5, -5); }).toThrow('totalCount must be >= 0'); }); test('Should throw on out of bound creation attempt', () => { expect(() => { new Pagination(-1, 5, 15); }).toThrow('Page -1 not found.'); expect(() => { new Pagination(-20, 5, 15); }).toThrow('Page -20 not found.'); expect(() => { new Pagination(3, 5, 15); }).not.toThrow(); expect(() => { new Pagination(4, 5, 15); }).toThrow('Page 4 not found.'); expect(() => { new Pagination(20, 5, 15); }).toThrow('Page 20 not found.'); }); test('Page 1 should not throw with totalCount=0', () => { expect(() => { new Pagination(1, 5, 0); }).not.toThrow(); expect(() => { new Pagination(2, 5, 0); }).toThrow('Page 2 not found.'); }); test('Should calculate correct last page', () => { expect(new Pagination(3, 5, 30).lastPage).toBe(6); expect(new Pagination(3, 5, 31).lastPage).toBe(7); expect(new Pagination(3, 5, 32).lastPage).toBe(7); expect(new Pagination(3, 5, 34).lastPage).toBe(7); expect(new Pagination(3, 5, 35).lastPage).toBe(7); expect(new Pagination(3, 5, 36).lastPage).toBe(8); expect(new Pagination(1, 5, 0).lastPage).toBe(1); }); test('Should properly tell whether has a previous page', () => { expect(pagination.hasPrevious()).toBe(true); expect(new Pagination(1, 5, 15).hasPrevious()).toBe(false); expect(new Pagination(2, 5, 15).hasPrevious()).toBe(true); expect(new Pagination(3, 5, 15).hasPrevious()).toBe(true); expect(new Pagination(1, 5, 17).hasPrevious()).toBe(false); expect(new Pagination(2, 5, 17).hasPrevious()).toBe(true); expect(new Pagination(3, 5, 17).hasPrevious()).toBe(true); expect(new Pagination(4, 5, 17).hasPrevious()).toBe(true); }); test('Should properly tell whether has a next page', () => { expect(pagination.hasPrevious()).toBe(true); expect(new Pagination(1, 5, 15).hasNext()).toBe(true); expect(new Pagination(2, 5, 15).hasNext()).toBe(true); expect(new Pagination(3, 5, 15).hasNext()).toBe(false); expect(new Pagination(1, 5, 17).hasNext()).toBe(true); expect(new Pagination(2, 5, 17).hasNext()).toBe(true); expect(new Pagination(3, 5, 17).hasNext()).toBe(true); expect(new Pagination(4, 5, 17).hasNext()).toBe(false); }); test('Should generate proper previous pages with context', () => { expect(new Pagination(1, 1, 100).previousPages(1)).toStrictEqual([]); expect(new Pagination(1, 1, 100).previousPages(2)).toStrictEqual([]); expect(new Pagination(1, 1, 100).previousPages(3)).toStrictEqual([]); expect(new Pagination(2, 1, 100).previousPages(1)).toStrictEqual([1]); expect(new Pagination(2, 1, 100).previousPages(2)).toStrictEqual([1]); expect(new Pagination(2, 1, 100).previousPages(3)).toStrictEqual([1]); expect(new Pagination(3, 1, 100).previousPages(1)).toStrictEqual([1, 2]); expect(new Pagination(3, 1, 100).previousPages(2)).toStrictEqual([1, 2]); expect(new Pagination(3, 1, 100).previousPages(3)).toStrictEqual([1, 2]); expect(new Pagination(10, 1, 100).previousPages(1)).toStrictEqual([1, -1, 9]); expect(new Pagination(10, 1, 100).previousPages(2)).toStrictEqual([1, 2, -1, 8, 9]); expect(new Pagination(10, 1, 100).previousPages(3)).toStrictEqual([1, 2, 3, -1, 7, 8, 9]); }); test('Should generate proper next pages with context', () => { let pagination = new Pagination(100, 1, 100); expect(pagination.nextPages(1)).toStrictEqual([]); expect(pagination.nextPages(2)).toStrictEqual([]); expect(pagination.nextPages(3)).toStrictEqual([]); pagination = new Pagination(99, 1, 100); expect(pagination.nextPages(1)).toStrictEqual([100]); expect(pagination.nextPages(2)).toStrictEqual([100]); expect(pagination.nextPages(3)).toStrictEqual([100]); pagination = new Pagination(98, 1, 100); expect(pagination.nextPages(1)).toStrictEqual([99, 100]); expect(pagination.nextPages(2)).toStrictEqual([99, 100]); expect(pagination.nextPages(3)).toStrictEqual([99, 100]); pagination = new Pagination(90, 1, 100); expect(pagination.nextPages(1)).toStrictEqual([91, -1, 100]); expect(pagination.nextPages(2)).toStrictEqual([91, 92, -1, 99, 100]); expect(pagination.nextPages(3)).toStrictEqual([91, 92, 93, -1, 98, 99, 100]); }); test('Should serialize properly', () => { expect(new Pagination(5, 6, 1000).serialize()).toStrictEqual('{"page":5,"perPage":6,"totalCount":1000}'); }); test('Should deserialize properly', () => { const pagination = Pagination.deserialize('{"page":5,"perPage":6,"totalCount":1000}'); expect(pagination.page).toStrictEqual(5); expect(pagination.perPage).toStrictEqual(6); expect(pagination.totalCount).toStrictEqual(1000); }); });