-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for pagination.component.tsx (#804)
Co-authored-by: Anjula Shanaka <[email protected]>
- Loading branch information
1 parent
3a6ebc1
commit 0eb6147
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
packages/esm-patient-search-app/src/ui-components/pagination/pagination.test.tsx
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,60 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import Pagination from './pagination.component'; | ||
|
||
describe('Pagination', () => { | ||
it('should render correctly with page numbers', () => { | ||
render(<Pagination totalPages={5} currentPage={3} setCurrentPage={() => {}} hasMore={true} />); | ||
|
||
for (let i = 1; i <= 5; i++) { | ||
const pageButtons = screen.getByRole('button', { name: `${i}` }); | ||
expect(pageButtons).toBeInTheDocument(); | ||
} | ||
}); | ||
|
||
it('should disable previous button on first page', () => { | ||
render(<Pagination totalPages={5} currentPage={1} setCurrentPage={() => {}} hasMore={true} />); | ||
const previousButton = screen.getByLabelText(/previous page/i); | ||
expect(previousButton).toBeDisabled(); | ||
}); | ||
|
||
it('should disable next button on last page when hasMore is false', () => { | ||
render(<Pagination totalPages={5} currentPage={5} setCurrentPage={() => {}} hasMore={false} />); | ||
const nextButton = screen.getByLabelText(/next page/i); | ||
expect(nextButton).toBeDisabled(); | ||
}); | ||
|
||
it('should increment the page when next button is clicked', () => { | ||
const setCurrentPageMock = jest.fn(); | ||
render(<Pagination totalPages={5} currentPage={1} setCurrentPage={setCurrentPageMock} hasMore={true} />); | ||
|
||
const nextButton = screen.getByLabelText(/next page/i); | ||
fireEvent.click(nextButton); | ||
expect(setCurrentPageMock).toHaveBeenCalledWith(2); | ||
}); | ||
|
||
it('should decrement the page when previous button is clicked', () => { | ||
const setCurrentPageMock = jest.fn(); | ||
render(<Pagination totalPages={5} currentPage={3} setCurrentPage={setCurrentPageMock} hasMore={true} />); | ||
|
||
const previousButton = screen.getByLabelText(/previous page/i); | ||
fireEvent.click(previousButton); | ||
expect(setCurrentPageMock).toHaveBeenCalledWith(2); | ||
}); | ||
|
||
it('should call setCurrentPage when page button is clicked', () => { | ||
const setCurrentPageMock = jest.fn(); | ||
render(<Pagination totalPages={5} currentPage={3} setCurrentPage={setCurrentPageMock} hasMore={true} />); | ||
|
||
const pageButton = screen.getByRole('button', { name: '4' }); | ||
fireEvent.click(pageButton); | ||
expect(setCurrentPageMock).toHaveBeenCalledWith(4); | ||
}); | ||
|
||
it('should render empty component when totalPages is 1', () => { | ||
render(<Pagination totalPages={1} currentPage={1} setCurrentPage={() => {}} hasMore={true} />); | ||
const pagination = screen.queryByRole('button', { name: 'next page' }); | ||
expect(pagination).not.toBeInTheDocument(); | ||
}); | ||
}); |