-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Specify routes to generate fetching prismic document
This allows to generate unlinked pages
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import prismic from 'prismic-javascript' | ||
import _ from 'lodash' | ||
|
||
export default async function () { | ||
const api = await prismic.getApi(process.env.PRISMIC_API_ENDPOINT) | ||
const { routes, totalPages } = await getRoutesInPage(api, 1) | ||
for (let page = 2; page <= totalPages; page++) { | ||
const { routes: nextPageRoutes } = await getRoutesInPage(api, page) | ||
routes.push(...nextPageRoutes) | ||
} | ||
return routes | ||
} | ||
|
||
async function getRoutesInPage(api, page) { | ||
// eslint-disable-next-line camelcase | ||
const { results, total_pages } = await api.query('', { | ||
pageSize: 100, | ||
page, | ||
}) | ||
const uids = _.map(results, 'uid') | ||
const routes = _.reject(uids, _.isEmpty) | ||
return { totalPages: total_pages, routes } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import getRoutesToGenerate from '@/services/get-routes-to-generate' | ||
import prismic from 'prismic-javascript' | ||
jest.mock('prismic-javascript') | ||
|
||
describe('#getRoutesToGenerate', () => { | ||
test('it should fetch routes to generate', async () => { | ||
// Given | ||
const expected = ['route-to-generate'] | ||
const prismicApi = { | ||
query: jest | ||
.fn() | ||
.mockResolvedValueOnce({ results: [{ uid: 'route-to-generate' }] }), | ||
} | ||
prismic.getApi = () => prismicApi | ||
|
||
// When | ||
const result = await getRoutesToGenerate(prismic) | ||
|
||
// Then | ||
expect(prismicApi.query).toBeCalledWith('', { pageSize: 100, page: 1 }) | ||
expect(result).toEqual(expected) | ||
}) | ||
|
||
test('it should not return null routes', async () => { | ||
// Given | ||
const expected = ['route-to-generate'] | ||
const prismicApi = { | ||
query: jest.fn().mockReturnValueOnce({ | ||
results: [{ uid: 'route-to-generate' }, {}, { uid: null }], | ||
}), | ||
} | ||
prismic.getApi = () => prismicApi | ||
|
||
// When | ||
const result = await getRoutesToGenerate(prismic) | ||
|
||
// Then | ||
expect(prismicApi.query).toBeCalledWith('', { pageSize: 100, page: 1 }) | ||
expect(result).toEqual(expected) | ||
}) | ||
|
||
test('it should handle pagination', async () => { | ||
// Given | ||
const expected = [ | ||
'route-to-generate-from-first-page', | ||
'route-to-generate-from-second-page', | ||
] | ||
|
||
const firstPage = { | ||
total_pages: 2, | ||
results: [{ uid: 'route-to-generate-from-first-page' }], | ||
} | ||
const secondPage = { | ||
total_pages: 2, | ||
results: [{ uid: 'route-to-generate-from-second-page' }], | ||
} | ||
|
||
const prismicApi = { | ||
query: jest | ||
.fn() | ||
.mockResolvedValueOnce(firstPage) | ||
.mockResolvedValueOnce(secondPage), | ||
} | ||
prismic.getApi = () => prismicApi | ||
|
||
// When | ||
const result = await getRoutesToGenerate(prismic) | ||
|
||
// Then | ||
expect(prismicApi.query).toBeCalledWith('', { pageSize: 100, page: 1 }) | ||
expect(prismicApi.query).toBeCalledWith('', { pageSize: 100, page: 2 }) | ||
expect(result).toEqual(expected) | ||
}) | ||
}) |