58 lines
2.1 KiB
TypeScript
58 lines
2.1 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
import { render, screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { Pagination } from '../../../src/components/common/Pagination';
|
|
|
|
describe('Pagination', () => {
|
|
it('returns null when totalItems <= 0', () => {
|
|
const { container } = render(
|
|
<Pagination currentPage={1} totalItems={0} itemsPerPage={6} onPageChange={vi.fn()} />
|
|
);
|
|
expect(container.firstChild).toBeNull();
|
|
});
|
|
|
|
it('renders pages and handles Prev, Next, page click, and itemsPerPage change', async () => {
|
|
const user = userEvent.setup();
|
|
const onPageChange = vi.fn();
|
|
const onItemsPerPageChange = vi.fn();
|
|
|
|
const { container } = render(
|
|
<Pagination
|
|
currentPage={2}
|
|
totalItems={30}
|
|
itemsPerPage={6}
|
|
onPageChange={onPageChange}
|
|
onItemsPerPageChange={onItemsPerPageChange}
|
|
/>
|
|
);
|
|
|
|
expect(container.textContent).toContain('Showing 7 to 12 of 30 items');
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Prev' }));
|
|
expect(onPageChange).toHaveBeenCalledWith(1);
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Next' }));
|
|
expect(onPageChange).toHaveBeenCalledWith(3);
|
|
|
|
await user.click(screen.getByRole('button', { name: '5' }));
|
|
expect(onPageChange).toHaveBeenCalledWith(5);
|
|
|
|
await user.selectOptions(screen.getByRole('combobox'), '12');
|
|
expect(onItemsPerPageChange).toHaveBeenCalledWith(12);
|
|
expect(onPageChange).toHaveBeenCalledWith(1);
|
|
});
|
|
|
|
it('renders ellipsis branches for large page counts (start, middle, end)', () => {
|
|
const { rerender } = render(
|
|
<Pagination currentPage={2} totalItems={120} itemsPerPage={10} onPageChange={vi.fn()} />
|
|
);
|
|
expect(screen.getAllByText('...')).toHaveLength(1);
|
|
|
|
rerender(<Pagination currentPage={6} totalItems={120} itemsPerPage={10} onPageChange={vi.fn()} />);
|
|
expect(screen.getAllByText('...')).toHaveLength(2);
|
|
|
|
rerender(<Pagination currentPage={11} totalItems={120} itemsPerPage={10} onPageChange={vi.fn()} />);
|
|
expect(screen.getAllByText('...')).toHaveLength(1);
|
|
});
|
|
});
|