This repository has been archived by the owner on May 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcddd4c
commit 4f9c825
Showing
1 changed file
with
74 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import React from 'react' | ||
import { | ||
pageInputWidth, | ||
pagesFromItems, | ||
changePageIfAppropiate, | ||
totals, | ||
getPaginationSlice | ||
} from '@auth0/cosmos/_helpers/pagination' | ||
|
||
describe('Pagination helper tests', () => { | ||
|
||
it('should calculate page input width', () => { | ||
expect(pageInputWidth(1)).toEqual(58) | ||
expect(pageInputWidth(10)).toEqual(66) | ||
expect(pageInputWidth(100)).toEqual(74) | ||
expect(pageInputWidth(1000)).toEqual(82) | ||
}) | ||
|
||
it('should calculate pagesFromItems', () => { | ||
expect(pagesFromItems(100,10)).toEqual(10) | ||
expect(pagesFromItems(104,10)).toEqual(11) | ||
expect(pagesFromItems(1,10)).toEqual(1) | ||
expect(pagesFromItems(0,10)).toEqual(0) | ||
}) | ||
|
||
it('should call change page handlers on last edge when appropiate', () => { | ||
const changePageHandler = jest.fn() | ||
|
||
let nextPage = 121 | ||
const totalItems = 1200 | ||
const perPage = 10 | ||
changePageIfAppropiate(nextPage, totalItems, perPage, changePageHandler) | ||
expect(changePageHandler).not.toHaveBeenCalled() | ||
|
||
nextPage = 120 | ||
changePageIfAppropiate(nextPage, totalItems, perPage, changePageHandler) | ||
expect(changePageHandler).toHaveBeenCalledWith(nextPage) | ||
}) | ||
|
||
it('should call change page handlers on first edge when appropiate', () => { | ||
const changePageHandler = jest.fn() | ||
|
||
let nextPage = -1 | ||
const totalItems = 1200 | ||
const perPage = 10 | ||
changePageIfAppropiate(nextPage, totalItems, perPage, changePageHandler) | ||
expect(changePageHandler).not.toHaveBeenCalled() | ||
|
||
nextPage = 1 | ||
changePageIfAppropiate(nextPage, totalItems, perPage, changePageHandler) | ||
expect(changePageHandler).toHaveBeenCalledWith(nextPage) | ||
}) | ||
|
||
it('should display totals', () => { | ||
expect(totals(75, 10, 12000)).toEqual("Showing 741 - 750 of 12000") | ||
expect(totals(75, 13, 12345)).toEqual("Showing 963 - 975 of 12345") | ||
expect(totals(1, 10, 54321)).toEqual("Showing 1 - 10 of 54321") | ||
expect(totals(1200, 10, 12000)).toEqual("Showing 11991 - 12000 of 12000") | ||
}) | ||
|
||
it('should generate pagination content', () => { | ||
const prettify = (p) => p.map(i => i.selected ? `[${i.label}]` : i.label) | ||
|
||
expect(prettify(getPaginationSlice(2, 100, 10, 10))) | ||
.toEqual([1, '[2]', 3, 4, 5, 6, 7, 8, 9, 10]) | ||
|
||
expect(prettify(getPaginationSlice(99, 1000, 10, 10))) | ||
.toEqual([91, 92, 93, 94, 95, 96, 97, 98, '[99]', 100]) | ||
|
||
expect(prettify(getPaginationSlice(93, 960, 10, 10))) | ||
.toEqual([91, 92, '[93]', 94, 95, 96]) | ||
}) | ||
}) | ||
|