-
-
Notifications
You must be signed in to change notification settings - Fork 657
Add Pagination Unit Tests & Accessibility Tweaks (Closes #1864) #1879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,134 @@ | ||
| import {within, render, fireEvent, screen, cleanup } from '@testing-library/react' | ||
| import '@testing-library/jest-dom' | ||
| import Pagination from 'components/Pagination' | ||
|
|
||
| afterEach(cleanup); | ||
|
|
||
| describe('<Pagination />', () => { | ||
| const onPageChange = jest.fn() | ||
|
|
||
| const renderComponent = (overrides: { | ||
| currentPage?: number | ||
| totalPages?: number | ||
| isLoaded?: boolean | ||
| } = {}) => { | ||
| const props = { | ||
| currentPage: overrides.currentPage ?? 1, | ||
| totalPages: overrides.totalPages ?? 5, | ||
| isLoaded: overrides.isLoaded ?? true, | ||
| onPageChange, | ||
| } | ||
| return render(<Pagination {...props} />) | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| onPageChange.mockClear() | ||
| }) | ||
|
|
||
| it('does not render when isLoaded is false', () => { | ||
| const { container } = renderComponent({ isLoaded: false }) | ||
| expect(container.firstChild).toBeNull() | ||
| }) | ||
|
|
||
| it('does not render when totalPages ≤ 1', () => { | ||
| const { container } = renderComponent({ totalPages: 1 }) | ||
| expect(container.firstChild).toBeNull() | ||
| }) | ||
|
|
||
| it('renders Prev and Next buttons and page numbers for small totalPages', () => { | ||
| renderComponent({ currentPage: 2, totalPages: 4 }) | ||
|
|
||
| // Prev / Next | ||
| const prev = screen.getByRole('button', { name: 'Prev' }) | ||
| const next = screen.getByRole('button', { name: 'Next' }) | ||
|
|
||
| expect(prev).toBeEnabled() | ||
| expect(next).toBeEnabled() | ||
|
|
||
| // Pages 1–4 | ||
| for (let n = 1; n <= 4; n++) { | ||
| expect(screen.getByRole('button', { name: String(n) })).toBeInTheDocument() | ||
| } | ||
| }) | ||
|
|
||
| it('disables Prev on first page', () => { | ||
| const { container } = renderComponent({ currentPage: 1, totalPages: 3 }) | ||
| const prevBtn = within(container).getByRole('button', { name: 'Prev' }) | ||
| expect(prevBtn).toBeDisabled() | ||
| }) | ||
|
|
||
| it('disables Next on last page', () => { | ||
| const { container } = renderComponent({ currentPage: 3, totalPages: 3 }) | ||
| const nextBtn = within(container).getByRole('button', { name: 'Next' }) | ||
| expect(nextBtn).toBeDisabled() | ||
| }) | ||
|
|
||
| it('calls onPageChange with correct page number on button clicks', () => { | ||
| renderComponent({ currentPage: 2, totalPages: 5 }) | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: 'Prev' })) | ||
| expect(onPageChange).toHaveBeenCalledWith(1) | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: 'Next' })) | ||
| expect(onPageChange).toHaveBeenCalledWith(3) | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: '4' })) | ||
| expect(onPageChange).toHaveBeenCalledWith(4) | ||
| }) | ||
|
|
||
| it('renders ellipses and correct pages for large totalPages', () => { | ||
| renderComponent({ currentPage: 10, totalPages: 20 }); | ||
|
|
||
| // Should show first 3 pages | ||
| [1, 2, 3].forEach((n) => { | ||
| expect(screen.getByRole('button', { name: String(n) })).toBeInTheDocument() | ||
| }) | ||
|
|
||
| // Should show an ellipsis span (no button) at two positions | ||
| const ellipses = screen.getAllByText((_, el) => el?.tagName === 'SPAN' && !el.textContent?.trim()) | ||
| expect(ellipses.length).toBeGreaterThanOrEqual(2); | ||
|
|
||
| // Should show pages around currentPage: 9, 10, 11 | ||
| [9, 10, 11].forEach((n) => | ||
| expect(screen.getByRole('button', { name: String(n) })).toBeInTheDocument() | ||
| ) | ||
|
|
||
| // Last page | ||
| expect(screen.getByRole('button', { name: '20' })).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('applies active styles and aria-current on the selected page', () => { | ||
| renderComponent({ currentPage: 3, totalPages: 5 }) | ||
|
|
||
| const activeBtn = screen.getByRole('button', { name: '3' }) | ||
| // check for class used in active state | ||
| expect(activeBtn).toHaveClass('bg-[#83a6cc]') | ||
| // accessibility: mark current page | ||
| expect(activeBtn).toHaveAttribute('aria-current', 'page') | ||
| }) | ||
|
|
||
| it('uses default values when props are missing', () => { | ||
| // No overrides → currentPage=1, totalPages=5, isLoaded=true | ||
| renderComponent() | ||
|
|
||
| expect(screen.getByRole('button', { name: 'Prev' })).toBeDisabled() | ||
| expect(screen.getByRole('button', { name: 'Next' })).toBeEnabled() | ||
| expect(screen.getByRole('button', { name: '5' })).toBeInTheDocument() | ||
| }) | ||
|
|
||
| // Edge-case: currentPage near the start of a large set | ||
| it('shows correct pages when currentPage = 4 of 10', () => { | ||
| renderComponent({ currentPage: 4, totalPages: 10 }); | ||
| // Should show 1,2,3,4,5 then ellipsis and 10 | ||
| [1, 2, 3, 4, 5].forEach(n => | ||
| expect(screen.getByRole('button', { name: String(n) })).toBeInTheDocument() | ||
| ) | ||
| expect(screen.getByRole('button', { name: '10' })).toBeInTheDocument() | ||
| }) | ||
|
|
||
| // Edge-case: very small totalPages (2) | ||
| it('renders exactly pages [1, 2] when totalPages = 2', () => { | ||
| renderComponent({ totalPages: 2, currentPage: 2 }) | ||
| expect(screen.getAllByRole('button', { name: /^(1|2)$/ })).toHaveLength(2) | ||
| }) | ||
| }) |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actual components should not have a
data-testidset on the elements. Please check other test files to see how to address this.