-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pagination): improve global code quality by removing magic numbe…
…rs & adding tests
- Loading branch information
1 parent
bb5c07c
commit 9c5e451
Showing
6 changed files
with
231 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
packages/ods/src/components/pagination/tests/controller/ods-pagination.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
import { computeActualTotalPages, createPageList, getActualPage } from '../../src/controller/ods-pagination'; | ||
|
||
describe('ods-pagination controller', () => { | ||
beforeEach(jest.clearAllMocks); | ||
|
||
describe('computeActualTotalPages', () => { | ||
it('should return totalPages if totalItems is undefined', () => { | ||
const itemPerPage = 10; | ||
const totalItems = undefined; | ||
const totalPages = 5; | ||
|
||
const result = computeActualTotalPages(itemPerPage, totalItems, totalPages); | ||
|
||
expect(result).toBe(totalPages); | ||
}); | ||
|
||
it('should return the correct number of total pages when totalItems is defined', () => { | ||
const itemPerPage = 10; | ||
const totalItems = 95; | ||
const totalPages = 5; | ||
|
||
const result = computeActualTotalPages(itemPerPage, totalItems, totalPages); | ||
|
||
expect(result).toBe(Math.ceil(totalItems / itemPerPage)); | ||
}); | ||
|
||
it('should handle itemPerPage being 0 or undefined', () => { | ||
const itemPerPage = 0; | ||
const totalItems = 50; | ||
const totalPages = 5; | ||
|
||
const result = computeActualTotalPages(itemPerPage, totalItems, totalPages); | ||
|
||
expect(result).toBe(Math.ceil(totalItems / 1)); | ||
}); | ||
|
||
it('should handle totalItems being 0', () => { | ||
const itemPerPage = 10; | ||
const totalItems = 0; | ||
const totalPages = 5; | ||
|
||
const result = computeActualTotalPages(itemPerPage, totalItems, totalPages); | ||
|
||
expect(result).toBe(totalPages); | ||
}); | ||
|
||
it('should handle large numbers of totalItems and itemPerPage correctly', () => { | ||
const itemPerPage = 1000; | ||
const totalItems = 100000; | ||
const totalPages = 100; | ||
|
||
const result = computeActualTotalPages(itemPerPage, totalItems, totalPages); | ||
|
||
expect(result).toBe(Math.ceil(totalItems / itemPerPage)); | ||
}); | ||
|
||
it('should handle small numbers of totalItems and itemPerPage correctly', () => { | ||
const itemPerPage = 2; | ||
const totalItems = 5; | ||
const totalPages = 3; | ||
|
||
const result = computeActualTotalPages(itemPerPage, totalItems, totalPages); | ||
|
||
expect(result).toBe(Math.ceil(totalItems / itemPerPage)); | ||
}); | ||
}); | ||
|
||
describe('createPageList', () => { | ||
it('should return all pages active when totalPages is less than or equal to MAX_VISIBLE_ITEMS', () => { | ||
const totalPages = 5; | ||
const pageSelected = 1; | ||
|
||
const result = createPageList(totalPages, pageSelected); | ||
|
||
expect(result).toHaveLength(totalPages); | ||
result.forEach((page) => expect(page.active).toBe(true)); | ||
}); | ||
|
||
it('should activate appropriate pages when totalPages is greater than MAX_VISIBLE_ITEMS and pageSelected is in the middle', () => { | ||
const totalPages = 10; | ||
const pageSelected = 5; | ||
|
||
const result = createPageList(totalPages, pageSelected); | ||
|
||
expect(result).toHaveLength(totalPages); | ||
result.forEach((page, index) => { | ||
if (index === 0 || index === totalPages - 1 || (index >= (pageSelected - 2) && index <= pageSelected)) { | ||
expect(page.active).toBe(true); | ||
} else { | ||
expect(page.active).toBe(false); | ||
} | ||
}); | ||
}); | ||
|
||
it('should activate the first page and the range around pageSelected when it is near the start', () => { | ||
const totalPages = 10; | ||
const pageSelected = 3; | ||
|
||
const result = createPageList(totalPages, pageSelected); | ||
|
||
expect(result).toHaveLength(totalPages); | ||
result.forEach((page, index) => { | ||
if (index <= 4 || index === totalPages - 1) { | ||
expect(page.active).toBe(true); | ||
} else { | ||
expect(page.active).toBe(false); | ||
} | ||
}); | ||
}); | ||
|
||
it('should activate the last page and the range around pageSelected when it is near the end', () => { | ||
const totalPages = 10; | ||
const pageSelected = 8; | ||
|
||
const result = createPageList(totalPages, pageSelected); | ||
|
||
expect(result).toHaveLength(totalPages); | ||
result.forEach((page, index) => { | ||
if (index === 0 || index === totalPages - 1 || index >= 5) { | ||
expect(page.active).toBe(true); | ||
} else { | ||
expect(page.active).toBe(false); | ||
} | ||
}); | ||
}); | ||
|
||
it('should handle a small number of totalPages correctly', () => { | ||
const totalPages = 3; | ||
const pageSelected = 2; | ||
|
||
const result = createPageList(totalPages, pageSelected); | ||
|
||
expect(result).toHaveLength(totalPages); | ||
result.forEach((page) => expect(page.active).toBe(true)); | ||
}); | ||
|
||
it('should handle a large number of totalPages correctly', () => { | ||
const totalPages = 100; | ||
const pageSelected = 50; | ||
|
||
const result = createPageList(totalPages, pageSelected); | ||
|
||
expect(result).toHaveLength(totalPages); | ||
result.forEach((page, index) => { | ||
if (index === 0 || index === totalPages - 1 || (index >= (pageSelected - 2) && index <= pageSelected)) { | ||
expect(page.active).toBe(true); | ||
} else { | ||
expect(page.active).toBe(false); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getActualPage', () => { | ||
it('should return actualTotalPages if defaultCurrentPage is greater than actualTotalPages', () => { | ||
const defaultCurrentPage = 10; | ||
const actualTotalPages = 5; | ||
const current = 1; | ||
|
||
const result = getActualPage(defaultCurrentPage, actualTotalPages, current); | ||
|
||
expect(result).toBe(actualTotalPages); | ||
}); | ||
|
||
it('should return 1 if defaultCurrentPage is less than 1', () => { | ||
const defaultCurrentPage = 0; | ||
const actualTotalPages = 5; | ||
const current = 1; | ||
|
||
const result = getActualPage(defaultCurrentPage, actualTotalPages, current); | ||
|
||
expect(result).toBe(1); | ||
}); | ||
|
||
it('should return defaultCurrentPage if it is within the valid range', () => { | ||
const defaultCurrentPage = 3; | ||
const actualTotalPages = 5; | ||
const current = 1; | ||
|
||
const result = getActualPage(defaultCurrentPage, actualTotalPages, current); | ||
|
||
expect(result).toBe(defaultCurrentPage); | ||
}); | ||
|
||
it('should handle defaultCurrentPage being 1 when actualTotalPages is also 1', () => { | ||
const defaultCurrentPage = 1; | ||
const actualTotalPages = 1; | ||
const current = 3; | ||
|
||
const result = getActualPage(defaultCurrentPage, actualTotalPages, current); | ||
|
||
expect(result).toBe(defaultCurrentPage); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$ods-pagination-button-width: 2.5rem; |