Skip to content

Commit

Permalink
Add tests for pagination.component.tsx (#804)
Browse files Browse the repository at this point in the history
Co-authored-by: Anjula Shanaka <[email protected]>
  • Loading branch information
ayush-AI and anjula-sack authored Sep 21, 2023
1 parent 3a6ebc1 commit 0eb6147
Showing 1 changed file with 60 additions and 0 deletions.
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();
});
});

0 comments on commit 0eb6147

Please sign in to comment.