diff --git a/tests/buffer-example.js b/tests/buffer-example.js index 2ea67db..452e7bd 100644 --- a/tests/buffer-example.js +++ b/tests/buffer-example.js @@ -1 +1 @@ -module.exports = Buffer.from('https://example.com/notice/sldkfmlksdmfs2020-07-16weekly0.8https://example.com/page/more-pages-heremonthly1.0https://example.com/page/a-demo-page1monthly1.0https://example.com/page/hello-worldi-need-to-make-a-demo-app2monthly1.0https://example.com/page/a-demo-pagemonthly1.0https://example.com/page/what-a-lot-i-dont-gotmonthly1.0https://example.com/page/a-page-of-cabbagesmonthly1.0https://example.com/page/20-is-a-lot1monthly1.0https://example.com/page/a-demo-page21monthly1.0https://example.com/page/hello-worldi-need-to-make-a-demo-app1monthly1.0https://example.com/page/20-is-a-lotmonthly1.0https://example.com/page/a-page-of-cabbages1monthly1.0https://example.com/page/a-demo-page2monthly1.0https://example.com/page/customer-reviews-page-examplemonthly1.0https://example.com/page/20-is-a-lot11monthly1.0https://example.com/page/another-page-heremonthly1.0https://example.com/page/what-is-the-timemonthly1.0https://example.com/page/what-is-the-time1monthly1.0https://example.com/page/hello-world-to-the-cabbagesmonthly1.0https://example.com/page/how-many-more-must-there-bemonthly1.0https://example.com/page/hello-worldi-need-to-make-a-demo-appmonthly1.0https://example.com/blog/a-demo-blog-postweekly0.8https://example.com/blog/demo-postweekly0.8') +module.exports = Buffer.from('https://example.com/notice/sldkfmlksdmfs2020-07-16weekly0.8https://example.com/page/more-pages-heremonthly1.0https://example.com/page/a-demo-page1monthly1.0https://example.com/page/hello-worldi-need-to-make-a-demo-app2monthly1.0https://example.com/page/a-demo-pagemonthly1.0https://example.com/page/what-a-lot-i-dont-gotmonthly1.0https://example.com/page/a-page-of-cabbagesmonthly1.0https://example.com/page/20-is-a-lot1monthly1.0https://example.com/page/a-demo-page21monthly1.0https://example.com/page/hello-worldi-need-to-make-a-demo-app1monthly1.0https://example.com/page/20-is-a-lotmonthly1.0https://example.com/page/a-page-of-cabbages1monthly1.0https://example.com/page/a-demo-page2monthly1.0https://example.com/page/customer-reviews-page-examplemonthly1.0https://example.com/page/20-is-a-lot11monthly1.0https://example.com/page/another-page-heremonthly1.0https://example.com/page/what-is-the-timemonthly1.0https://example.com/page/what-is-the-time1monthly1.0https://example.com/page/hello-world-to-the-cabbagesmonthly1.0https://example.com/page/how-many-more-must-there-bemonthly1.0https://example.com/page/hello-worldi-need-to-make-a-demo-appmonthly1.0https://example.com/page/another-page-here1monthly1.0https://example.com/blog/a-demo-blog-postweekly0.8https://example.com/blog/demo-postweekly0.8') diff --git a/tests/generator.test.js b/tests/generator.test.js index 46d5324..d007737 100644 --- a/tests/generator.test.js +++ b/tests/generator.test.js @@ -28,7 +28,7 @@ const sitemapConfig = { post: { changefreq: "weekly", priority: 0.8 }, latest_notice: (doc) => { return { - lastmod: doc.last_publication_date ? doc.last_publication_date : (new Date()).toJSON(), + lastmod: doc.last_publication_date ? doc.last_publication_date : "2020-07-16", changefreq: "weekly", priority: 0.8 } @@ -55,7 +55,7 @@ describe('tests for generator.js', () => { .toHaveBeenCalledTimes(1); }); - it('file write should have been called with ...', () => { + it('file write should have been called with sitemapData Buffer', () => { expect(jest.spyOn(fs, 'writeFileSync')).toHaveBeenCalledWith( path.join(__dirname, "../public/sitemap.xml"), @@ -73,6 +73,27 @@ describe('tests for generator.js', () => { // }) }); + +describe('Pagination Tests', () => { + it('file write should have been called with sitemapData Buffer', async () => { + + const paginationConfig = { + pagination: { + pageSize: 5, + }, ...sitemapConfig + }; + + jest.restoreAllMocks(); + await generator(paginationConfig); + + expect(jest.spyOn(fs, 'writeFileSync')).toHaveBeenCalledWith( + path.join(__dirname, "../public/sitemap.xml"), + require('./buffer-example'), + "utf-8" + ); + }); +}); + describe('generator errors', () => { it('will fail if linkResolver is not a function', async () => { expect(async () => { diff --git a/tests/integration/withPage/__snapshots__/index.test.js.snap b/tests/integration/withPage/__snapshots__/index.test.js.snap index 13468f0..5bf3146 100644 --- a/tests/integration/withPage/__snapshots__/index.test.js.snap +++ b/tests/integration/withPage/__snapshots__/index.test.js.snap @@ -1,3 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Using Sitemap Generator visiting page loads sitemap.xml 1`] = `"https://example.com/notice/sldkfmlksdmfs2020-07-16weekly0.8https://example.com/page/more-pages-heremonthly1.0https://example.com/page/a-demo-page1monthly1.0https://example.com/page/hello-worldi-need-to-make-a-demo-app2monthly1.0https://example.com/page/a-demo-pagemonthly1.0https://example.com/page/what-a-lot-i-dont-gotmonthly1.0https://example.com/page/a-page-of-cabbagesmonthly1.0https://example.com/page/20-is-a-lot1monthly1.0https://example.com/page/a-demo-page21monthly1.0https://example.com/page/hello-worldi-need-to-make-a-demo-app1monthly1.0https://example.com/page/20-is-a-lotmonthly1.0https://example.com/page/a-page-of-cabbages1monthly1.0https://example.com/page/a-demo-page2monthly1.0https://example.com/page/customer-reviews-page-examplemonthly1.0https://example.com/page/20-is-a-lot11monthly1.0https://example.com/page/another-page-heremonthly1.0https://example.com/page/what-is-the-timemonthly1.0https://example.com/page/what-is-the-time1monthly1.0https://example.com/page/hello-world-to-the-cabbagesmonthly1.0https://example.com/page/how-many-more-must-there-bemonthly1.0https://example.com/page/hello-worldi-need-to-make-a-demo-appmonthly1.0https://example.com/blog/a-demo-blog-postweekly0.8https://example.com/blog/demo-postweekly0.8https://example.com/hello-to-me2021-06-29https://example.com/sup2000-02-02"`; +exports[`Using Sitemap Generator visiting page loads sitemap.xml 1`] = `"https://example.com/notice/sldkfmlksdmfs2020-07-16weekly0.8https://example.com/page/more-pages-heremonthly1.0https://example.com/page/a-demo-page1monthly1.0https://example.com/page/hello-worldi-need-to-make-a-demo-app2monthly1.0https://example.com/page/a-demo-pagemonthly1.0https://example.com/page/what-a-lot-i-dont-gotmonthly1.0https://example.com/page/a-page-of-cabbagesmonthly1.0https://example.com/page/20-is-a-lot1monthly1.0https://example.com/page/a-demo-page21monthly1.0https://example.com/page/hello-worldi-need-to-make-a-demo-app1monthly1.0https://example.com/page/20-is-a-lotmonthly1.0https://example.com/page/a-page-of-cabbages1monthly1.0https://example.com/page/a-demo-page2monthly1.0https://example.com/page/customer-reviews-page-examplemonthly1.0https://example.com/page/20-is-a-lot11monthly1.0https://example.com/page/another-page-heremonthly1.0https://example.com/page/what-is-the-timemonthly1.0https://example.com/page/what-is-the-time1monthly1.0https://example.com/page/hello-world-to-the-cabbagesmonthly1.0https://example.com/page/how-many-more-must-there-bemonthly1.0https://example.com/page/hello-worldi-need-to-make-a-demo-appmonthly1.0https://example.com/page/another-page-here1monthly1.0https://example.com/blog/a-demo-blog-postweekly0.8https://example.com/blog/demo-postweekly0.8https://example.com/hello-to-me2021-06-29https://example.com/sup2000-02-02"`; diff --git a/utils/paginator.js b/utils/paginator.js index a8231b0..8f3882f 100644 --- a/utils/paginator.js +++ b/utils/paginator.js @@ -7,7 +7,7 @@ const paginator = { /** * * @param {Prismic} api prismic client - * @param {Object} options {pageSize, auto} + * @param {Object} options {pageSize} */ init(api, options = {}) { this.api = api; @@ -16,19 +16,27 @@ const paginator = { return this; }, + /** + * This paginates automatically over the document list from Prismic + * @todo Add Lang support for scoping to a selection of languages. + * + * @param {String} type The Document type + * @param {Number} next The Next Page type + * @returns Promise + */ async paginate(type, next = null) { const { results, total_pages, page } = await this.api.query( Prismic.Predicates.at("document.type", type), - { - lang: "*", - pageSize: this.pageSize, - page: next - } + { + lang: "*", + pageSize: this.pageSize, + page: next + } ); this.results.push(...results); - if (total_pages > this.nextPage) { + if (total_pages !== page) { return this.paginate(type, page + 1) }