Skip to content

Commit

Permalink
Refactor the test for whether $module is missing or wrong type
Browse files Browse the repository at this point in the history
Introduce a new `ElementError` that handles the check to format different message so we do not repeat it in each component
  • Loading branch information
romaricpascal authored and colinrotherham committed Sep 18, 2023
1 parent 7590f72 commit 70e1277
Show file tree
Hide file tree
Showing 24 changed files with 163 additions and 115 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mergeConfigs, extractConfigByNamespace } from '../../common/index.mjs'
import { normaliseDataset } from '../../common/normalise-dataset.mjs'
import { MissingElementError } from '../../errors/index.mjs'
import { ElementError } from '../../errors/index.mjs'
import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'
import { I18n } from '../../i18n.mjs'

Expand Down Expand Up @@ -120,11 +120,11 @@ export class Accordion extends GOVUKFrontendComponent {
super()

if (!($module instanceof HTMLElement)) {
throw !$module
? new MissingElementError('Accordion: $module not found')
: new TypeError(
'Accordion: $module is not an instance of "HTMLElement"'
)
throw new ElementError($module, {
componentName: 'Accordion',
identifier: '$module',
expectedType: HTMLElement
})
}

this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ describe('/components/accordion', () => {
}
})
).rejects.toEqual({
name: 'MissingElementError',
name: 'ElementError',
message: 'Accordion: $module not found'
})
})
Expand All @@ -754,13 +754,11 @@ describe('/components/accordion', () => {
params: examples.default,
beforeInitialisation($module) {
// Replace with an `<svg>` element which is not an `HTMLElement` in the DOM (but an `SVGElement`)
$module.outerHTML = `<svg data-module="${$module.getAttribute(
'data-module'
)}"></svg>`
$module.outerHTML = `<svg data-module="govuk-accordion"></svg>`
}
})
).rejects.toEqual({
name: 'TypeError',
name: 'ElementError',
message: 'Accordion: $module is not an instance of "HTMLElement"'
})
})
Expand Down
10 changes: 6 additions & 4 deletions packages/govuk-frontend/src/govuk/components/button/button.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mergeConfigs } from '../../common/index.mjs'
import { normaliseDataset } from '../../common/normalise-dataset.mjs'
import { MissingElementError } from '../../errors/index.mjs'
import { ElementError } from '../../errors/index.mjs'
import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'

const KEY_SPACE = 32
Expand Down Expand Up @@ -35,9 +35,11 @@ export class Button extends GOVUKFrontendComponent {
super()

if (!($module instanceof HTMLElement)) {
throw !$module
? new MissingElementError('Button: $module not found')
: new TypeError('Button: $module is not an instance of "HTMLElement"')
throw new ElementError($module, {
componentName: 'Button',
identifier: '$module',
expectedType: HTMLElement
})
}

this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ describe('/components/button', () => {
}
})
).rejects.toEqual({
name: 'MissingElementError',
name: 'ElementError',
message: 'Button: $module not found'
})
})
Expand All @@ -359,13 +359,11 @@ describe('/components/button', () => {
params: examples.default,
beforeInitialisation($module) {
// Replace with an `<svg>` element which is not an `HTMLElement` in the DOM (but an `SVGElement`)
$module.outerHTML = `<svg data-module="${$module.getAttribute(
'data-module'
)}"></svg>`
$module.outerHTML = `<svg data-module="govuk-button"></svg>`
}
})
).rejects.toEqual({
name: 'TypeError',
name: 'ElementError',
message: 'Button: $module is not an instance of "HTMLElement"'
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
validateConfig
} from '../../common/index.mjs'
import { normaliseDataset } from '../../common/normalise-dataset.mjs'
import { ConfigError, MissingElementError } from '../../errors/index.mjs'
import { ConfigError, ElementError } from '../../errors/index.mjs'
import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'
import { I18n } from '../../i18n.mjs'

Expand Down Expand Up @@ -75,11 +75,11 @@ export class CharacterCount extends GOVUKFrontendComponent {
super()

if (!($module instanceof HTMLElement)) {
throw !$module
? new MissingElementError('Character count: $module not found')
: new TypeError(
'Character count: $module is not an instance of "HTMLElement"'
)
throw new ElementError($module, {
componentName: 'Character count',
identifier: '$module',
expectedType: HTMLElement
})
}

const $textarea = $module.querySelector('.govuk-js-character-count')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ describe('Character count', () => {
}
})
).rejects.toEqual({
name: 'MissingElementError',
name: 'ElementError',
message: 'Character count: $module not found'
})
})
Expand All @@ -805,13 +805,11 @@ describe('Character count', () => {
params: examples.default,
beforeInitialisation($module) {
// Replace with an `<svg>` element which is not an `HTMLElement` in the DOM (but an `SVGElement`)
$module.outerHTML = `<svg data-module="${$module.getAttribute(
'data-module'
)}"></svg>`
$module.outerHTML = `<svg data-module="govuk-character-count"></svg>`
}
})
).rejects.toEqual({
name: 'TypeError',
name: 'ElementError',
message:
'Character count: $module is not an instance of "HTMLElement"'
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MissingElementError } from '../../errors/index.mjs'
import { ElementError } from '../../errors/index.mjs'
import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'

/**
Expand Down Expand Up @@ -31,11 +31,11 @@ export class Checkboxes extends GOVUKFrontendComponent {
super()

if (!($module instanceof HTMLElement)) {
throw !$module
? new MissingElementError('Checkboxes: $module not found')
: new TypeError(
'Checkboxes: $module is not an instance of "HTMLElement"'
)
throw new ElementError($module, {
componentName: 'Checkboxes',
identifier: '$module',
expectedType: HTMLElement
})
}

/** @satisfies {NodeListOf<HTMLInputElement>} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ describe('Checkboxes with multiple groups and a "None" checkbox and conditional
}
})
).rejects.toEqual({
name: 'MissingElementError',
name: 'ElementError',
message: 'Checkboxes: $module not found'
})
})
Expand All @@ -360,13 +360,11 @@ describe('Checkboxes with multiple groups and a "None" checkbox and conditional
params: examples.default,
beforeInitialisation($module) {
// Replace with an `<svg>` element which is not an `HTMLElement` in the DOM (but an `SVGElement`)
$module.outerHTML = `<svg data-module="${$module.getAttribute(
'data-module'
)}"></svg>`
$module.outerHTML = `<svg data-module="govuk-checkboxes"></svg>`
}
})
).rejects.toEqual({
name: 'TypeError',
name: 'ElementError',
message: 'Checkboxes: $module is not an instance of "HTMLElement"'
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mergeConfigs } from '../../common/index.mjs'
import { normaliseDataset } from '../../common/normalise-dataset.mjs'
import { MissingElementError } from '../../errors/index.mjs'
import { ElementError } from '../../errors/index.mjs'
import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'

/**
Expand Down Expand Up @@ -28,11 +28,11 @@ export class ErrorSummary extends GOVUKFrontendComponent {
super()

if (!($module instanceof HTMLElement)) {
throw !$module
? new MissingElementError('Error summary: $module not found')
: new TypeError(
'Error summary: $module is not an instance of "HTMLElement"'
)
throw new ElementError($module, {
componentName: 'Error summary',
identifier: '$module',
expectedType: HTMLElement
})
}

this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ describe('Error Summary', () => {
}
})
).rejects.toEqual({
name: 'MissingElementError',
name: 'ElementError',
message: 'Error summary: $module not found'
})
})
Expand All @@ -251,13 +251,11 @@ describe('Error Summary', () => {
params: examples.default,
beforeInitialisation($module) {
// Replace with an `<svg>` element which is not an `HTMLElement` in the DOM (but an `SVGElement`)
$module.outerHTML = `<svg data-module="${$module.getAttribute(
'data-module'
)}"></svg>`
$module.outerHTML = `<svg data-module="govuk-error-summary"></svg>`
}
})
).rejects.toEqual({
name: 'TypeError',
name: 'ElementError',
message: 'Error summary: $module is not an instance of "HTMLElement"'
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mergeConfigs, extractConfigByNamespace } from '../../common/index.mjs'
import { normaliseDataset } from '../../common/normalise-dataset.mjs'
import { MissingElementError } from '../../errors/index.mjs'
import { ElementError } from '../../errors/index.mjs'
import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'
import { I18n } from '../../i18n.mjs'

Expand Down Expand Up @@ -82,11 +82,11 @@ export class ExitThisPage extends GOVUKFrontendComponent {
super()

if (!($module instanceof HTMLElement)) {
throw !$module
? new MissingElementError('Exit this page: $module not found')
: new TypeError(
'Exit this page: $module is not an instance of "HTMLElement"'
)
throw new ElementError($module, {
componentName: 'Exit this page',
identifier: '$module',
expectedType: HTMLElement
})
}

const $button = $module.querySelector('.govuk-exit-this-page__button')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ describe('/components/exit-this-page', () => {
}
})
).rejects.toEqual({
name: 'MissingElementError',
name: 'ElementError',
message: 'Exit this page: $module not found'
})
})
Expand All @@ -229,13 +229,11 @@ describe('/components/exit-this-page', () => {
params: examples.default,
beforeInitialisation($module) {
// Replace with an `<svg>` element which is not an `HTMLElement` in the DOM (but an `SVGElement`)
$module.outerHTML = `<svg data-module="${$module.getAttribute(
'data-module'
)}"></svg>`
$module.outerHTML = `<svg data-module="govuk-exit-this-page"></svg>`
}
})
).rejects.toEqual({
name: 'TypeError',
name: 'ElementError',
message: 'Exit this page: $module is not an instance of "HTMLElement"'
})
})
Expand Down
10 changes: 6 additions & 4 deletions packages/govuk-frontend/src/govuk/components/header/header.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MissingElementError } from '../../errors/index.mjs'
import { ElementError } from '../../errors/index.mjs'
import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'

/**
Expand Down Expand Up @@ -45,9 +45,11 @@ export class Header extends GOVUKFrontendComponent {
super()

if (!($module instanceof HTMLElement)) {
throw !$module
? new MissingElementError('Header: $module not found')
: new TypeError('Header: $module is not an instance of "HTMLElement"')
throw new ElementError($module, {
componentName: 'Header',
identifier: '$module',
expectedType: HTMLElement
})
}

this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ describe('Header navigation', () => {
}
})
).rejects.toEqual({
name: 'MissingElementError',
name: 'ElementError',
message: 'Header: $module not found'
})
})
Expand All @@ -211,13 +211,11 @@ describe('Header navigation', () => {
params: examples.default,
beforeInitialisation($module) {
// Replace with an `<svg>` element which is not an `HTMLElement` in the DOM (but an `SVGElement`)
$module.outerHTML = `<svg data-module="${$module.getAttribute(
'data-module'
)}"></svg>`
$module.outerHTML = `<svg data-module="govuk-header"></svg>`
}
})
).rejects.toEqual({
name: 'TypeError',
name: 'ElementError',
message: 'Header: $module is not an instance of "HTMLElement"'
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mergeConfigs } from '../../common/index.mjs'
import { normaliseDataset } from '../../common/normalise-dataset.mjs'
import { MissingElementError } from '../../errors/index.mjs'
import { ElementError } from '../../errors/index.mjs'
import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'

/**
Expand All @@ -26,11 +26,11 @@ export class NotificationBanner extends GOVUKFrontendComponent {
super()

if (!($module instanceof HTMLElement)) {
throw !$module
? new MissingElementError('Notification banner: $module not found')
: new TypeError(
'Notification banner: $module is not an instance of "HTMLElement"'
)
throw new ElementError($module, {
componentName: 'Notification banner',
identifier: '$module',
expectedType: HTMLElement
})
}

this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ describe('Notification banner', () => {
}
})
).rejects.toEqual({
name: 'MissingElementError',
name: 'ElementError',
message: 'Notification banner: $module not found'
})
})
Expand All @@ -239,13 +239,11 @@ describe('Notification banner', () => {
params: examples.default,
beforeInitialisation($module) {
// Replace with an `<svg>` element which is not an `HTMLElement` in the DOM (but an `SVGElement`)
$module.outerHTML = `<svg data-module="${$module.getAttribute(
'data-module'
)}"></svg>`
$module.outerHTML = `<svg data-module="govuk-notification-banner"></svg>`
}
})
).rejects.toEqual({
name: 'TypeError',
name: 'ElementError',
message:
'Notification banner: $module is not an instance of "HTMLElement"'
})
Expand Down
Loading

0 comments on commit 70e1277

Please sign in to comment.