From 93aa8579c327bb2fa0be43d0cd0ef55d6e487ce4 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Thu, 22 Apr 2021 11:40:27 +0200 Subject: [PATCH 1/2] Pagination: don't throw on first page when totalCount=0, validate params --- src/Pagination.ts | 7 +++-- test/Pagination.test.ts | 70 +++++++++++++++++++++++++++++------------ 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/src/Pagination.ts b/src/Pagination.ts index b86cd07..020a19e 100644 --- a/src/Pagination.ts +++ b/src/Pagination.ts @@ -6,11 +6,14 @@ export default class Pagination { public readonly totalCount: number; public constructor(page: number, perPage: number, totalCount: number) { + if (perPage <= 0) throw new Error('perPage must be >= 1'); + if (totalCount < 0) throw new Error('totalCount must be >= 0'); + this.page = page; this.perPage = perPage; this.totalCount = totalCount; - if (this.page < 1 || this.page > this.lastPage) { + if (this.page < 1 || this.page > 1 && this.page > this.lastPage) { throw new PageNotFoundError(this.page); } } @@ -24,7 +27,7 @@ export default class Pagination { } public get lastPage(): number { - return Math.ceil(this.totalCount / this.perPage); + return Math.max(Math.ceil(this.totalCount / this.perPage), 1); } public previousPages(contextSize: number): number[] { diff --git a/test/Pagination.test.ts b/test/Pagination.test.ts index 8cf66bc..3cf2bf9 100644 --- a/test/Pagination.test.ts +++ b/test/Pagination.test.ts @@ -5,15 +5,61 @@ describe('Pagination', () => { 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); @@ -21,6 +67,8 @@ describe('Pagination', () => { 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', () => { @@ -47,24 +95,6 @@ describe('Pagination', () => { expect(new Pagination(4, 5, 17).hasNext()).toBe(false); }); - 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('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([]); From 85e23b7f42c39de1c6b23d9693ab649428e23e3f Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Thu, 22 Apr 2021 12:13:46 +0200 Subject: [PATCH 2/2] Version 0.23.9 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bbcacf3..ca3e8ff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swaf", - "version": "0.23.8", + "version": "0.23.9", "description": "Structure Web Application Framework.", "repository": "https://eternae.ink/ashpie/swaf", "author": "Alice Gaudon ",