Skip to content

Commit

Permalink
Move logic about setting page language to page handler
Browse files Browse the repository at this point in the history
  • Loading branch information
irisfaraway committed Sep 21, 2023
1 parent 7259af0 commit 75fbd7e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exports[`The page handler function sets the value of pageData with displayAnalyt
"backRef": null,
"displayAnalytics": false,
"mssgs": undefined,
"pageLanguageSetToWelsh": false,
"uri": Object {
"analyticsFormAction": "/buy/process-analytics-preferences",
},
Expand Down Expand Up @@ -41,6 +42,7 @@ exports[`The page handler function sets the value of pageData with displayAnalyt
"backRef": null,
"displayAnalytics": true,
"mssgs": undefined,
"pageLanguageSetToWelsh": false,
"uri": Object {
"analyticsFormAction": "/buy/process-analytics-preferences",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,55 @@ describe('The page handler function', () => {
expect(set).toBeCalledWith(result)
})
})

describe('pageLanguageSetToWelsh', () => {
it('returns false when SHOW_WELSH_CONTENT is not true', async () => {
process.env.SHOW_WELSH_CONTENT = false
const { get } = pageHandler('', 'view', '/next/page')
const toolkit = getMockToolkit()

await get(getMockRequest(), toolkit)

expect(toolkit.view).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
pageLanguageSetToWelsh: false
})
)
})

it('returns false when SHOW_WELSH_CONTENT is true but the lang is not set to cy', async () => {
process.env.SHOW_WELSH_CONTENT = true
const { get } = pageHandler('', 'view', '/next/page')
const toolkit = getMockToolkit()

await get(getMockRequest(), toolkit)

expect(toolkit.view).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
pageLanguageSetToWelsh: false
})
)
})

it('returns true when SHOW_WELSH_CONTENT is true and the lang is set to cy', async () => {
process.env.SHOW_WELSH_CONTENT = true
const { get } = pageHandler('', 'view', '/next/page')
const query = { lang: 'cy' }
const request = getMockRequest({ query })
const toolkit = getMockToolkit()

await get(request, toolkit)

expect(toolkit.view).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
pageLanguageSetToWelsh: true
})
)
})
})
})

const getAnalytics = overides => ({
Expand All @@ -249,7 +298,7 @@ const getAnalytics = overides => ({
...overides
})

const getMockRequest = ({ setCurrentPermission = () => {}, path = '/buy/we/are/here', analytics, set = () => {} } = {}) => ({
const getMockRequest = ({ setCurrentPermission = () => {}, path = '/buy/we/are/here', query = {}, analytics, set = () => {} } = {}) => ({
cache: () => ({
helpers: {
page: {
Expand All @@ -275,6 +324,7 @@ const getMockRequest = ({ setCurrentPermission = () => {}, path = '/buy/we/are/h
getLocale: () => ''
},
path,
query,
url: {
search: ''
}
Expand Down
6 changes: 6 additions & 0 deletions packages/gafl-webapp-service/src/handlers/page-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ const omitPageFromAnalytics = async request => {
}
}

const pageLanguageSetToWelsh = request => {
const showWelshContent = process.env.SHOW_WELSH_CONTENT?.toLowerCase() === 'true'
return showWelshContent && request.query.lang === 'cy'
}

/**
* Flattens the error structure from joi for use in the templates
* @param e
Expand Down Expand Up @@ -123,6 +128,7 @@ export default (path, view, completion, getData) => ({
pageData.altLang = request.i18n.getLocales().filter(locale => locale !== request.i18n.getLocale())
pageData.backRef = await getBackReference(request, view)
pageData.uri = { ...(pageData.uri || {}), analyticsFormAction: addLanguageCodeToUri(request, PROCESS_ANALYTICS_PREFERENCES.uri) }
pageData.pageLanguageSetToWelsh = pageLanguageSetToWelsh(request)

const analytics = await request.cache().helpers.analytics.get()
pageData.analyticsMessageDisplayed = analytics ? analytics[ANALYTICS.seenMessage] : false
Expand Down
2 changes: 1 addition & 1 deletion packages/gafl-webapp-service/src/pages/layout/layout.njk
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{% from "button/macro.njk" import govukButton %}
{% from "footer/macro.njk" import govukFooter %}

{% if (SHOW_WELSH_CONTENT) and (_uri.queryParams.lang == 'cy') %}
{% if pageLanguageSetToWelsh %}
{% set htmlLang = "cy" %}
{% endif %}

Expand Down

0 comments on commit 75fbd7e

Please sign in to comment.