Skip to content

Commit

Permalink
fix: do not include route to generate if language is not handled
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbertrand committed Jan 18, 2022
1 parent 5541ac8 commit 14fa72c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion services/get-routes-to-generate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Prismic from '@prismicio/client'
import { language } from '../config/language'
import { SITES_PRISMIC_TAGS } from './available-sites'
import { DOCUMENTS } from './document-fetcher'

Expand All @@ -25,8 +26,10 @@ async function getRoutesInPage(api, page) {
}
)

const availableLangs = language.locales.map((locale) => locale.code)
const routes = results
.filter(({ uid, type }) => Boolean(uid))
.filter(({ uid }) => Boolean(uid))
.filter(({ lang }) => availableLangs.includes(lang))
.map(({ uid, lang }) => {
if (lang === 'fr-fr') return `/${uid}`
return `/${lang}/${uid}`
Expand Down
31 changes: 31 additions & 0 deletions tests/services/get-routes-to-generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,37 @@ describe('#getRoutesToGenerate', () => {
expect(result).toEqual(expected)
})

test('it should filter unhandled languages', async () => {
// Given
const expected = [
'/route-to-generate',
'/fr/route-to-generate',
'/en-gb/route-to-generate',
]
const prismicApi = {
query: jest.fn().mockResolvedValueOnce({
results: [
{ uid: 'route-to-generate', lang: 'fr-fr' },
{ uid: 'route-to-generate', lang: 'fr' },
{ uid: 'route-to-generate', lang: 'en-gb' },
{ uid: 'route-to-generate', lang: 'xh-ZA' },
],
}),
}
prismic.getApi = () => prismicApi

// When
const result = await getRoutesToGenerate(prismic)

// Then
expect(prismicApi.query).toBeCalledWith(prismicDocPredicates, {
lang: '*',
pageSize: 100,
page: 1,
})
expect(result).toEqual(expected)
})

test('it should not return null routes', async () => {
// Given
const expected = ['/route-to-generate']
Expand Down

0 comments on commit 14fa72c

Please sign in to comment.