diff --git a/utils/paginate.js b/utils/paginate.js index 6efa20f..1b24af8 100644 --- a/utils/paginate.js +++ b/utils/paginate.js @@ -22,11 +22,11 @@ module.exports = ( totalPages, perPage = 3 ) => { - const pages = Math.ceil(totalPages / perPage); - _.times(pages, index => { + const pageCount = Math.ceil(totalPages / perPage); + _.times(pageCount, index => { createPage({ // Calculate the path for this page like `/blog`, `/blog/2` - path: paginationPath(basePath, index, totalPages), + path: paginationPath(basePath, index, pageCount), // Set the component as normal component: componentPath, // Pass the following context to the component @@ -36,11 +36,11 @@ module.exports = ( // How many posts to show on this paginated page limit: perPage, // How many paginated pages there are in total - totalPages, + totalPages: pageCount, // The path to the previous paginated page (or an empty string) - prevPath: paginationPath(basePath, index - 1, totalPages), + prevPath: paginationPath(basePath, index - 1, pageCount), // The path to the next paginated page (or an empty string) - nextPath: paginationPath(basePath, index + 1, totalPages), + nextPath: paginationPath(basePath, index + 1, pageCount), // Current page ID for displaying between chevrons pageID: index + 1 } diff --git a/utils/paginate.test.js b/utils/paginate.test.js new file mode 100644 index 0000000..9bfa6ef --- /dev/null +++ b/utils/paginate.test.js @@ -0,0 +1,22 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); + +const paginate = require('./paginate'); + +test('stops generating a next page link on the final paginated page', () => { + const createdPages = []; + + paginate( + page => createdPages.push(page), + '/tmp/component', + '/page', + 160, + 10 + ); + + assert.equal(createdPages.length, 16); + assert.equal(createdPages[0].context.nextPath, '/page/2'); + assert.equal(createdPages[14].context.nextPath, '/page/16'); + assert.equal(createdPages[15].context.pageID, 16); + assert.equal(createdPages[15].context.nextPath, ''); +});