Skip to content

Commit

Permalink
Merge pull request #5065 from alphagov/jsdom-warning-text-template-tests
Browse files Browse the repository at this point in the history
Update warning text template tests to use jsdom
  • Loading branch information
romaricpascal authored Jul 18, 2024
2 parents f1f6c6d + e627dc9 commit 7e91160
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const { getExamples, render } = require('@govuk-frontend/lib/components')

describe('Warning text', () => {
let examples

beforeAll(async () => {
examples = await getExamples('warning-text')
})

describe('default example', () => {
beforeAll(() => {
document.body.innerHTML = render('warning-text', examples.default)
})

it('contains the text from the `text` option', () => {
const $component = document.querySelector('.govuk-warning-text')

expect($component).toHaveTextContent(
'You can be fined up to £5,000 if you don’t register.'
)
})

it('includes the default assistive text', () => {
const $assistiveText = document.querySelector('.govuk-visually-hidden')

expect($assistiveText).toHaveTextContent('Warning')
})

it('hides the icon from screen readers using the `aria-hidden` attribute', () => {
const $icon = document.querySelector('.govuk-warning-text__icon')

expect($icon).toHaveAttribute('aria-hidden', 'true')
})
})

it('includes additional classes from the `classes` option', () => {
document.body.innerHTML = render('warning-text', examples.classes)
const $component = document.querySelector('.govuk-warning-text')

expect($component).toHaveClass('govuk-warning-text--custom-class')
})

it('includes custom assistive text based on the `iconFallbackText` option', () => {
document.body.innerHTML = render(
'warning-text',
examples['icon fallback text only']
)
const $assistiveText = document.querySelector('.govuk-visually-hidden')

expect($assistiveText).toHaveTextContent('Some custom fallback text')
})

it('sets any additional attributes based on the `attributes` option', () => {
document.body.innerHTML = render('warning-text', examples.attributes)
const $component = document.querySelector('.govuk-warning-text')

expect($component).toHaveAttribute('data-test', 'attribute')
expect($component).toHaveAttribute('id', 'my-warning-text')
})

it('escapes HTML when using the `text` option', () => {
document.body.innerHTML = render('warning-text', examples['html as text'])
const $component = document.querySelector('.govuk-warning-text')

expect($component).toHaveTextContent(
'<span>Some custom warning text</span>'
)
})

it('does not escape HTML when using the `html` option', () => {
document.body.innerHTML = render('warning-text', examples.html)
const $component = document.querySelector('.govuk-warning-text')

expect($component).toContainHTML('<span>Some custom warning text</span>')
})
})

This file was deleted.

0 comments on commit 7e91160

Please sign in to comment.