instantsearch icon indicating copy to clipboard operation
instantsearch copied to clipboard

getMaxPage assumes nbPages is always a number

Open tkrugg opened this issue 6 years ago • 1 comments

Describe the bug 🐛

getMaxPage assumes nbPages is always a number. This is nolonger true in 4.0.0 as it can be undefined

To Reproduce 🔍

Steps to reproduce the behavior:

  1. Download an run: https://github.com/algolia/doc-code-samples/tree/master/InstantSearch.js/conditional-request
  2. In index.html, update instantsearch source script to @4.0.0
  3. Reload
  4. See error

Uncaught (in promise) RangeError: Invalid array length

image

A live example helps a lot! We have a simple online template for you to use for your explanations:

https://codesandbox.io/s/conditional-request-n2cw3

Other widgets assume nbPages is a number:

  • InfiniteHits: https://github.com/algolia/instantsearch.js/blob/b454759f513b8681e5c7efd677b0b79da5fac137/src/connectors/infinite-hits/connectInfiniteHits.ts#L189
  • Stats: for nbPages and maybe nbHits https://github.com/algolia/instantsearch.js/blob/b454759f513b8681e5c7efd677b0b79da5fac137/src/components/Stats/Stats.js#L29 (people customizing might start getting NaNs)
  • Make sure we check for nbHits, page and hitsPerPage.

Expected behavior 💭 No errors

Screenshots 🖥 N/A

Environment:

  • OS: mac
  • Browser: chrome
  • Version: 78

Additional context N/A

tkrugg avatar Nov 04 '19 12:11 tkrugg

https://github.com/algolia/instantsearch.js/blob/8feef58c7c8e7b7b1b213d5ed70206304c2c10ed/src/connectors/pagination/connectPagination.ts#L89

this should become

      getMaxPage({ nbPages = 0 }) {

or a similar solution then I guess

Haroenv avatar Nov 04 '19 12:11 Haroenv

Actually this is in the contract of the API that nbPages, nbHits, page and hitsPerPage are always returned and a number. Therefore conditional fake responses should have those keys as well, and our documentation includes those keys already.

No further action is needed as this didn't come up for other people.

Haroenv avatar Oct 06 '22 08:10 Haroenv