Skip to content

Commit

Permalink
Precise fallback meta description
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasgrilleres committed Feb 17, 2021
1 parent 6662583 commit aeeb00d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
3 changes: 2 additions & 1 deletion nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export default {
{
hid: 'description',
name: 'description',
content: process.env.npm_package_description || '',
content:
'Pix est le service public en ligne pour évaluer, développer et certifier ses compétences numériques tout au long de la vie.',
},
],
link: [
Expand Down
10 changes: 8 additions & 2 deletions services/meta-builder.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const fallbackDescription =
'Pix est le service public en ligne pour évaluer, développer et certifier ses compétences numériques tout au long de la vie.'

export default function getMeta(meta, currentPagePath, prismic) {
function getTwitterCard() {
const twitterMeta = meta.find((meta) => meta.slice_type === 'twitter_card')
Expand Down Expand Up @@ -39,7 +42,8 @@ export default function getMeta(meta, currentPagePath, prismic) {
{
hid: 'og:description',
property: 'og:description',
content: prismic.asText(ogMeta.primary.description),
content:
prismic.asText(ogMeta.primary.description) || fallbackDescription,
},
{ hid: 'og:type', property: 'og:type', content: 'article' },
{
Expand All @@ -65,7 +69,9 @@ export default function getMeta(meta, currentPagePath, prismic) {
{
hid: 'description',
name: 'description',
content: prismic.asText(generalCard.primary.description),
content:
prismic.asText(generalCard.primary.description) ||
fallbackDescription,
},
]
}
Expand Down
8 changes: 0 additions & 8 deletions tests/pages/pix-site/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Index Page get the corrects meta 1`] = `
VueWrapper {
"_emitted": Object {},
"_emittedByOrder": Array [],
"isFunctionalComponent": undefined,
}
`;

exports[`Index Page renders properly 1`] = `
"<div class=\\"index\\">
<slice-zone-stub slices=\\"[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]\\"></slice-zone-stub>
Expand Down
20 changes: 17 additions & 3 deletions tests/pages/pix-site/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import VueMeta from 'vue-meta'
import { getInitialised, createLocalVue } from './utils'
import { documentFetcher } from '~/services/document-fetcher'
import getMeta from '~/services/meta-builder'
import getMeta, { fallbackDescription } from '~/services/meta-builder'

const localVue = createLocalVue()
localVue.prototype.$getMeta = getMeta
Expand Down Expand Up @@ -63,16 +63,30 @@ describe('Index Page', () => {
expect(wrapper.html()).toMatchSnapshot()
})

test('get correct title info', () => {
test('gets the correct title', () => {
expect(wrapper.vm.$metaInfo.title).toBe(`${PRISMIC_TITLE} | Pix`)
expect(wrapper.vm.$data.title).toBe(PRISMIC_TITLE)
})

test('get the corrects meta', () => {
test('gets the correct meta description from Prismic', () => {
expect(findMetaContent('og:description')).toBe(PRISMIC_META)
expect(findMetaContent('description')).toBe(PRISMIC_META)
})

test('uses the fallback meta description when not filled in Prismic', async () => {
wrapper = await getInitialised('index', {
localVue,
computed: {
$prismic() {
return { asText: () => '' }
},
},
})

expect(findMetaContent('og:description')).toBe(fallbackDescription)
expect(findMetaContent('description')).toBe(fallbackDescription)
})

function findMetaContent(hid) {
const meta = wrapper.vm.$metaInfo.meta.find((m) => m.hid === hid)
expect(meta).toBeTruthy()
Expand Down

0 comments on commit aeeb00d

Please sign in to comment.