Merge branch 'develop'
This commit is contained in:
commit
adf50862f0
@ -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 <alice@gaudon.pro>",
|
||||
|
@ -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[] {
|
||||
|
@ -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([]);
|
||||
|
Loading…
Reference in New Issue
Block a user