Skip to main content

Pagination Tests

Overview

The Pagination component has comprehensive test coverage using Vitest and React Testing Library.

Tests are located in src/components/Pagination.test.jsx.

Running Tests

# Run all tests
npm test

# Run only Pagination tests
npm test Pagination

# Run tests in watch mode
npm test -- --watch

Test Categories

Rendering Tests

Tests that verify the component renders correctly under various conditions:

TestDescription
Should not render when totalPages is 1Component returns null for single page
Should not render when totalPages is 0Component returns null for zero pages
Should render when totalPages > 1Navigation element is present
Should render with correct aria-labelAccessibility label is set
Should render previous and next buttonsArrow buttons are present

Page Button Tests

Tests for individual page number buttons:

TestDescription
Should render page numbers correctly1-indexed display verification
Should mark current page with aria-currentAccessibility current indicator
Should apply active class to current pageVisual styling verification
Should not have aria-current on non-active pagesCorrect ARIA usage

Ellipsis Display Tests

Tests for the smart ellipsis behavior:

TestDescription
Should show leading ellipsisWhen not on first pages
Should show trailing ellipsisWhen not on last pages
Should show first page buttonWhen far from start
Should show last page buttonWhen far from end
Should not show leading ellipsis on first pageEdge case handling
Should not show trailing ellipsis on last pageEdge case handling

Tests for Previous/Next buttons:

TestDescription
Should disable previous button on first pageBoundary handling
Should disable next button on last pageBoundary handling
Should enable previous button when not on first pageNormal state
Should enable next button when not on last pageNormal state

Click Interaction Tests

Tests for mouse/touch interactions:

TestDescription
Should call onChange with previous pagePrevious button click
Should call onChange with next pageNext button click
Should call onChange with correct pagePage number click
Should call onChange with 0 for first pageFirst page button click
Should call onChange with last indexLast page button click

Keyboard Navigation Tests

Tests for keyboard accessibility:

TestDescription
Should go to next page on ArrowRightRight arrow key
Should go to previous page on ArrowLeftLeft arrow key
Should not go beyond last page on ArrowRightBoundary handling
Should not go before first page on ArrowLeftBoundary handling
Should ignore navigation in input fieldsForm field awareness
Should ignore navigation in textareaForm field awareness
Should ignore other keysOnly arrow keys work

Visible Pages Calculation Tests

Tests for the page windowing algorithm:

TestDescription
Should show adjacent pages around currentDelta calculation
Should handle edge case at beginningFirst page display
Should handle edge case at endLast page display

Cleanup Tests

Tests for proper React lifecycle handling:

TestDescription
Should remove event listener on unmountMemory leak prevention

Test Utilities

CSS Module Mock

vi.mock('./Pagination.module.css', () => ({
default: {
pagination: 'mocked-pagination-class',
arrow: 'mocked-arrow-class',
page: 'mocked-page-class',
active: 'mocked-active-class',
ellipsis: 'mocked-ellipsis-class',
},
}));

Common Test Setup

const mockOnChange = vi.fn();

beforeEach(() => {
mockOnChange.mockClear();
});

Coverage Areas

  • Conditional rendering
  • Accessibility attributes
  • Click handlers
  • Keyboard navigation
  • Disabled states
  • Ellipsis logic
  • Edge cases (first/last page)
  • Event listener cleanup
  • Form field awareness