Skip to content

Commit

Permalink
Merge pull request #15 from ReeceM/fix/paginator-error-issue-14
Browse files Browse the repository at this point in the history
Fix/paginator error issue 14
  • Loading branch information
ReeceM authored Jul 5, 2022
2 parents 4b9d7bb + 4e66736 commit ad6678c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion tests/buffer-example.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 23 additions & 2 deletions tests/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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"),
Expand All @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Using Sitemap Generator visiting page loads sitemap.xml 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" xmlns:news=\\"http://www.google.com/schemas/sitemap-news/0.9\\" xmlns:xhtml=\\"http://www.w3.org/1999/xhtml\\" xmlns:image=\\"http://www.google.com/schemas/sitemap-image/1.1\\" xmlns:video=\\"http://www.google.com/schemas/sitemap-video/1.1\\"><url><loc>https://example.com/notice/sldkfmlksdmfs</loc><lastmod>2020-07-16</lastmod><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>https://example.com/page/more-pages-here</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-worldi-need-to-make-a-demo-app2</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/what-a-lot-i-dont-got</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-page-of-cabbages</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/20-is-a-lot1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page21</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-worldi-need-to-make-a-demo-app1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/20-is-a-lot</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-page-of-cabbages1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page2</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/customer-reviews-page-example</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/20-is-a-lot11</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/another-page-here</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/what-is-the-time</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/what-is-the-time1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-world-to-the-cabbages</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/how-many-more-must-there-be</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-worldi-need-to-make-a-demo-app</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/blog/a-demo-blog-post</loc><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>https://example.com/blog/demo-post</loc><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>https://example.com/hello-to-me</loc><lastmod>2021-06-29</lastmod></url><url><loc>https://example.com/sup</loc><lastmod>2000-02-02</lastmod></url></urlset>"`;
exports[`Using Sitemap Generator visiting page loads sitemap.xml 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" xmlns:news=\\"http://www.google.com/schemas/sitemap-news/0.9\\" xmlns:xhtml=\\"http://www.w3.org/1999/xhtml\\" xmlns:image=\\"http://www.google.com/schemas/sitemap-image/1.1\\" xmlns:video=\\"http://www.google.com/schemas/sitemap-video/1.1\\"><url><loc>https://example.com/notice/sldkfmlksdmfs</loc><lastmod>2020-07-16</lastmod><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>https://example.com/page/more-pages-here</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-worldi-need-to-make-a-demo-app2</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/what-a-lot-i-dont-got</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-page-of-cabbages</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/20-is-a-lot1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page21</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-worldi-need-to-make-a-demo-app1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/20-is-a-lot</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-page-of-cabbages1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page2</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/customer-reviews-page-example</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/20-is-a-lot11</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/another-page-here</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/what-is-the-time</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/what-is-the-time1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-world-to-the-cabbages</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/how-many-more-must-there-be</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-worldi-need-to-make-a-demo-app</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/another-page-here1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/blog/a-demo-blog-post</loc><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>https://example.com/blog/demo-post</loc><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>https://example.com/hello-to-me</loc><lastmod>2021-06-29</lastmod></url><url><loc>https://example.com/sup</loc><lastmod>2000-02-02</lastmod></url></urlset>"`;
22 changes: 15 additions & 7 deletions utils/paginator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
}

Expand Down

0 comments on commit ad6678c

Please sign in to comment.