Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 16, 2025

Problem

atomic-pager displayed an extra non-existent page button on the last page. With 10 total pages, the last page showed buttons [7, 8, 9, 10, 11], where clicking page 11 resulted in an empty results page.

Root Cause

Index mismatch: Search Headless uses 1-indexed pages (currentPage: 1-10, maxPage: 10), while pager-utils expects 0-indexed internally (0-9). The code converted currentPage but not maxPage.

Changes

atomic-pager.ts line 147:

// Before
getCurrentPagesRange(
  this.pagerState.currentPage - 1,
  this.numberOfPages,
  this.pagerState.maxPage  // ❌ 1-indexed value
);

// After
getCurrentPagesRange(
  this.pagerState.currentPage - 1,
  this.numberOfPages,
  this.pagerState.maxPage - 1  // ✅ Convert to 0-indexed
);

Unit Tests Added:
Added comprehensive unit tests in atomic-pager.spec.ts that verify the pager displays the correct number of page buttons for various combinations of total results and results per page:

  • 100 results with 10 per page (maxPage = 10)
  • 47 results with 10 per page (maxPage = 5)
  • 250 results with 25 per page (maxPage = 10)
  • 23 results with 5 per page (maxPage = 5)

Each test calculates maxPage = Math.ceil(totalResults / resultsPerPage) and verifies that on the last page, only valid page buttons are displayed without showing non-existent pages. This prevents regression of the off-by-one bug.

Note: atomic-commerce-pager unaffected—Commerce Headless uses 0-indexing throughout.

https://coveord.atlassian.net/browse/KIT-5338


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix empty last page in atomic pager Fix atomic-pager showing non-existent page on last page Dec 16, 2025
Copilot AI requested a review from louis-bompart December 16, 2025 18:36
@jmazzah-coveo jmazzah-coveo marked this pull request as ready for review December 16, 2025 20:17
@jmazzah-coveo jmazzah-coveo requested a review from a team as a code owner December 16, 2025 20:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an off-by-one indexing bug in atomic-pager that caused a non-existent page button to appear on the last page. The root cause was an inconsistent index conversion between Search Headless (1-indexed) and the pager utility functions (0-indexed).

Key Changes:

  • Fixed getCurrentPagesRange call to convert maxPage from 1-indexed to 0-indexed
  • Aligned with existing conversions for currentPage parameter in the same function call
  • Ensures consistency with atomic-commerce-pager pattern (which doesn't need conversion as Commerce Headless is already 0-indexed)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@louis-bompart louis-bompart changed the title Fix atomic-pager showing non-existent page on last page fix: atomic-pager showing non-existent page on last page Dec 17, 2025
@louis-bompart
Copy link
Collaborator

@copilot add a unit tests that for a given number of results (and a given result per page number), the pager displays the right number of page button.

Copy link
Contributor Author

Copilot AI commented Dec 17, 2025

@copilot add a unit tests that for a given number of results (and a given result per page number), the pager displays the right number of page button.

Added unit tests in commit 7f471d3 that verify the pager displays the correct number of page buttons for various combinations of total results and results per page:

  • 100 results with 10 per page → maxPage = 10
  • 47 results with 10 per page → maxPage = 5
  • 250 results with 25 per page → maxPage = 10
  • 23 results with 5 per page → maxPage = 5

Each test calculates maxPage = Math.ceil(totalResults / resultsPerPage) and verifies that on the last page, only valid page buttons are displayed (not showing non-existent pages like the original bug).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chromatic Opt-in w/ Chromatic

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants