summaryrefslogtreecommitdiff
path: root/src/components/numbered-pagination.test.tsx
diff options
context:
space:
mode:
authorBertrand Yuan <189593334+bertyuan@users.noreply.github.com>2026-03-26 00:19:31 +0800
committerGitHub <noreply@github.com>2026-03-26 00:19:31 +0800
commitf247a8c4a863ec430f4a705b5c493d652c8429bd (patch)
tree71d0985970984c105582f6e3c370b254f38e9bbe /src/components/numbered-pagination.test.tsx
parentf7a02fe0e112cf108fc5f22872f1efc077e99fe8 (diff)
parentcd3c4bc89c169616b38bdb7443bb4eb7571b020c (diff)
Merge pull request #12 from bertyuan/fix-vitestv1.1
Fix vitest
Diffstat (limited to 'src/components/numbered-pagination.test.tsx')
-rw-r--r--src/components/numbered-pagination.test.tsx64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/components/numbered-pagination.test.tsx b/src/components/numbered-pagination.test.tsx
new file mode 100644
index 0000000..d99ab46
--- /dev/null
+++ b/src/components/numbered-pagination.test.tsx
@@ -0,0 +1,64 @@
+import { fireEvent, render, screen } from '@testing-library/react';
+import { describe, expect, test, vi } from 'vitest';
+import { NumberedPagination } from './numbered-pagination';
+
+describe('NumberedPagination', () => {
+ test('renders page controls and current page', () => {
+ render(
+ <NumberedPagination
+ currentPage={5}
+ totalPages={10}
+ paginationItemsToDisplay={5}
+ onPageChange={vi.fn()}
+ />,
+ );
+
+ expect(screen.getByRole('link', { name: 'Go to previous page' })).toBeInTheDocument();
+ expect(screen.getByRole('link', { name: 'Go to next page' })).toBeInTheDocument();
+ expect(screen.getByRole('link', { name: '5' })).toHaveAttribute('aria-current');
+ expect(screen.getAllByText('...')).toHaveLength(2);
+ });
+
+ test('calls onPageChange when selecting a valid page', () => {
+ const onPageChange = vi.fn();
+ render(
+ <NumberedPagination
+ currentPage={2}
+ totalPages={5}
+ paginationItemsToDisplay={5}
+ onPageChange={onPageChange}
+ />,
+ );
+
+ fireEvent.click(screen.getByRole('link', { name: '3' }));
+ expect(onPageChange).toHaveBeenCalledWith(3);
+ });
+
+ test('does not call onPageChange for out-of-range previous/next actions', () => {
+ const onPageChange = vi.fn();
+ const { rerender } = render(
+ <NumberedPagination
+ currentPage={1}
+ totalPages={3}
+ paginationItemsToDisplay={5}
+ onPageChange={onPageChange}
+ />,
+ );
+
+ fireEvent.click(screen.getByRole('link', { name: 'Go to previous page' }));
+ expect(onPageChange).not.toHaveBeenCalled();
+
+ rerender(
+ <NumberedPagination
+ currentPage={3}
+ totalPages={3}
+ paginationItemsToDisplay={5}
+ onPageChange={onPageChange}
+ />,
+ );
+
+ fireEvent.click(screen.getByRole('link', { name: 'Go to next page' }));
+ expect(onPageChange).not.toHaveBeenCalled();
+ });
+});
+