Skip to content

Commit

Permalink
Merge pull request #5191 from alphagov/fix-back-link-falsy-values
Browse files Browse the repository at this point in the history
Fix rendering of Back link's `href` and `text` for falsy values
  • Loading branch information
romaricpascal authored Aug 6, 2024
2 parents 4d2eca9 + 315e0c3 commit 521b2d8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
73 changes: 56 additions & 17 deletions src/govuk/components/back-link/template.jsdom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,71 @@ describe('back-link component', () => {
expect($component).toHaveClass('app-back-link--custom-class')
})

it('allows the link to be customised using the `href` option', () => {
document.body.innerHTML = render('back-link', examples['with custom link'])
describe('the `href` option', () => {
it('allows the link to be customised', () => {
document.body.innerHTML = render(
'back-link',
examples['with custom link']
)

const $component = document.querySelector('.govuk-back-link')
expect($component).toHaveAttribute('href', '/home')
})

const $component = document.querySelector('.govuk-back-link')
expect($component).toHaveAttribute('href', '/home')
it.each(['', 0, false, null, undefined])('uses `#` for `%s`', (href) => {
document.body.innerHTML = render('back-link', { context: { href } })
const $component = document.querySelector('.govuk-back-link')

expect($component).toHaveAttribute('href', '#')
})
})

it('allows the text to be customised using the `text` option', () => {
document.body.innerHTML = render('back-link', examples['with custom text'])
describe('the `text` option', () => {
it('allows the text to be customised', () => {
document.body.innerHTML = render(
'back-link',
examples['with custom text']
)

const $component = document.querySelector('.govuk-back-link')
expect($component).toHaveTextContent('Back to home')
})
const $component = document.querySelector('.govuk-back-link')
expect($component).toHaveTextContent('Back to home')
})

it('escapes HTML when using the `text` option', () => {
document.body.innerHTML = render('back-link', examples['html as text'])
it('escapes HTML', () => {
document.body.innerHTML = render('back-link', examples['html as text'])

const $component = document.querySelector('.govuk-back-link')
expect($component).toHaveTextContent('<b>Home</b>')
const $component = document.querySelector('.govuk-back-link')
expect($component).toHaveTextContent('<b>Home</b>')
})

it.each(['', 0, false, null, undefined])(
'displays `Back` for `%s`',
(text) => {
document.body.innerHTML = render('back-link', { context: { text } })
const $component = document.querySelector('.govuk-back-link')

expect($component).toHaveTextContent('Back')
}
)
})

it('does not escape HTML when using the `html` option', () => {
document.body.innerHTML = render('back-link', examples.html)
describe('the `html` option', () => {
it('does not escape HTML', () => {
document.body.innerHTML = render('back-link', examples.html)

const $component = document.querySelector('.govuk-back-link')
expect($component).toContainHTML('<b>Back</b>')
const $component = document.querySelector('.govuk-back-link')
expect($component).toContainHTML('<b>Back</b>')
})

it.each(['', 0, false, null, undefined])(
'displays `Back` for `%s`',
(html) => {
document.body.innerHTML = render('back-link', { context: { html } })
const $component = document.querySelector('.govuk-back-link')

expect($component).toHaveTextContent('Back')
}
)
})

it('sets any additional attributes based on the `attributes` option', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/govuk/components/back-link/template.njk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% from "../../macros/attributes.njk" import govukAttributes -%}

<a href="{{ params.href | default('#') }}" class="govuk-back-link {%- if params.classes %} {{ params.classes }}{% endif %}"
<a href="{{ params.href | default('#', true) }}" class="govuk-back-link {%- if params.classes %} {{ params.classes }}{% endif %}"
{{- govukAttributes(params.attributes) }}>
{{- params.html | safe if params.html else (params.text | default("Back")) -}}
{{- params.html | safe if params.html else (params.text | default("Back", true)) -}}
</a>

0 comments on commit 521b2d8

Please sign in to comment.