Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix character count behaviour tests instantiating twice #4781

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { getExamples, render } from '@govuk-frontend/lib/components'
import { outdent } from 'outdent'

import { CharacterCount } from './character-count.mjs'

describe('CharacterCount', () => {
let html
let examples

beforeAll(async () => {
const examples = await getExamples('character-count')
html = render('character-count', examples['to configure in JavaScript'])
examples = await getExamples('character-count')
})

beforeEach(async () => {
// Some tests add attributes to `document.body` so we need
// to reset it alongside the component's markup
document.body.outerHTML = `<body class="govuk-frontend-supported">${html}</body>`
beforeEach(() => {
const example = examples['to configure in JavaScript']

// Some tests add attributes to `document.body` so we need to reset it
// alongside both character count renders (for maxlength and maxwords)
document.body.outerHTML = outdent`
<body class="govuk-frontend-supported">
${render('character-count', example)}
${render('character-count', example)}
</body>
`
})

describe('formatCountMessage', () => {
Expand All @@ -22,9 +29,15 @@ describe('CharacterCount', () => {
let componentWithMaxWords

beforeEach(() => {
const $div = document.querySelector('[data-module]')
componentWithMaxLength = new CharacterCount($div, { maxlength: 100 })
componentWithMaxWords = new CharacterCount($div, { maxwords: 100 })
const $divs = document.querySelectorAll('[data-module]')

componentWithMaxLength = new CharacterCount($divs[0], {
maxlength: 100
})

componentWithMaxWords = new CharacterCount($divs[1], {
maxwords: 100
})
})

const cases = [
Expand Down Expand Up @@ -108,14 +121,16 @@ describe('CharacterCount', () => {
})

it('uses specific keys for when limit is reached', () => {
const $div = document.querySelector('[data-module]')
const componentWithMaxLength = new CharacterCount($div, {
const $divs = document.querySelectorAll('[data-module]')

const componentWithMaxLength = new CharacterCount($divs[0], {
maxlength: 100,
i18n: {
charactersAtLimit: 'Custom text.'
}
})
const componentWithMaxWords = new CharacterCount($div, {

const componentWithMaxWords = new CharacterCount($divs[1], {
maxwords: 100,
i18n: {
wordsAtLimit: 'Different custom text.'
Expand Down
4 changes: 2 additions & 2 deletions packages/govuk-frontend/src/govuk/errors/index.jsdom.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ describe('errors', () => {
).toBe('Component name: variableName not found')
})
it('formats the message when the element is not the right type', () => {
const element = document.createElement('div')
const $element = document.createElement('div')

expect(
new ElementError({
componentName: 'Component name',
element,
element: $element,
expectedType: 'HTMLAnchorElement',
identifier: 'variableName'
}).message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class GOVUKFrontendComponent {
* Validates whether GOV.UK Frontend is supported
*
* @private
* @throws {SupportError} when GOV.UK Frontend is not supported
*/
checkSupport() {
if (!isSupported()) {
Expand Down