Skip to content

Commit

Permalink
[FEATURE] Générer les pages "orphelines" (PIX-2246).
Browse files Browse the repository at this point in the history
  • Loading branch information
pix-service-auto-merge authored Mar 1, 2021
2 parents 7a05a9e + c1d08f4 commit 0520834
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { transports } from 'winston'
import routes from './services/get-routes-to-generate'

export default {
generate: { routes },
target: process.env.SSR_ENABLED === 'true' ? 'server' : 'static',
publicRuntimeConfig: {
languageSwitchEnabled: process.env.LANGUAGE_SWITCH_ENABLED || false,
Expand Down
22 changes: 22 additions & 0 deletions services/get-routes-to-generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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) {
const { results, total_pages: totalPages } = await api.query('', {
pageSize: 100,
page,
})
const uids = _.map(results, 'uid')
const routes = _.reject(uids, _.isEmpty)
return { totalPages, routes }
}
74 changes: 74 additions & 0 deletions tests/services/get-routes-to-generate.test.js
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)
})
})

0 comments on commit 0520834

Please sign in to comment.