Skip to content

Commit

Permalink
Add pagination component unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
afsaa committed Jan 23, 2024
1 parent 0bb82e1 commit f2b0ffc
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/components/Pagination/pagination.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import Pagination from './pagination';

describe('Pagination test cases', () => {
test('renders correctly', () => {
render(<Pagination itemsPerPage={10} totalItems={50} currentPage={1} onPageChange={() => {}} />);

for (let i = 1; i <= 5; i++) {
expect(screen.getByText(i.toString())).toBeInTheDocument();
}
});

test('calls onPageChange with correct page number when a page button is clicked', async () => {
const onPageChange = jest.fn();
render(<Pagination itemsPerPage={10} totalItems={50} currentPage={1} onPageChange={onPageChange} />);

await userEvent.click(screen.getByText('2'));

expect(onPageChange).toHaveBeenCalledWith(2);
});

test('disables the button of the current page', () => {
render(<Pagination itemsPerPage={10} totalItems={50} currentPage={1} onPageChange={() => {}} />);

expect(screen.getByText('1')).toBeDisabled();
});
});

0 comments on commit f2b0ffc

Please sign in to comment.