Skip to content

Commit

Permalink
WIP - Refactor InitError to use formatErrorMessage or accept an a…
Browse files Browse the repository at this point in the history
…rbitrary message

This ensures errors thrown when the component is already initialised have the same format
as other errors related to components. Accepting an arbitrary message was the simplest way
I figured to type the arguments of `InitError` (and I think should be a default for all errors)

Tests for each component need updating as the messages have changed
  • Loading branch information
romaricpascal committed Sep 30, 2024
1 parent 6d80ae5 commit d830263
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ describe('/components/accordion', () => {
).rejects.toMatchObject({
name: 'InitError',
message:
'Root element (`$root`) already initialised (`govuk-accordion`)'
'govuk-accordion: Root element (`$root`) already initialised'
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ describe('/components/button', () => {
})
).rejects.toMatchObject({
name: 'InitError',
message: 'Root element (`$root`) already initialised (`govuk-button`)'
message: 'govuk-button: Root element (`$root`) already initialised'
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ describe('Character count', () => {
).rejects.toMatchObject({
name: 'InitError',
message:
'Root element (`$root`) already initialised (`govuk-character-count`)'
'govuk-character-count: Root element (`$root`) already initialised'
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ describe('Checkboxes', () => {
).rejects.toMatchObject({
name: 'InitError',
message:
'Root element (`$root`) already initialised (`govuk-checkboxes`)'
'govuk-checkboxes: Root element (`$root`) already initialised'
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ describe('Error Summary', () => {
).rejects.toMatchObject({
name: 'InitError',
message:
'Root element (`$root`) already initialised (`govuk-error-summary`)'
'govuk-error-summary: Root element (`$root`) already initialised'
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ describe('/components/exit-this-page', () => {
).rejects.toMatchObject({
name: 'InitError',
message:
'Root element (`$root`) already initialised (`govuk-exit-this-page`)'
'govuk-exit-this-page: Root element (`$root`) already initialised'
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('Header navigation', () => {
})
).rejects.toMatchObject({
name: 'InitError',
message: 'Root element (`$root`) already initialised (`govuk-header`)'
message: 'govuk-header: Root element (`$root`) already initialised'
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ describe('Notification banner', () => {
).rejects.toMatchObject({
name: 'InitError',
message:
'Root element (`$root`) already initialised (`govuk-notification-banner`)'
'govuk-notification-banner: Root element (`$root`) already initialised'
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ describe('Radios', () => {
})
).rejects.toMatchObject({
name: 'InitError',
message: 'Root element (`$root`) already initialised (`govuk-radios`)'
message: 'govuk-radios: Root element (`$root`) already initialised'
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ describe('Skip Link', () => {
})
).rejects.toMatchObject({
name: 'InitError',
message:
'Root element (`$root`) already initialised (`govuk-skip-link`)'
message: 'govuk-skip-link: Root element (`$root`) already initialised'
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ describe('/components/tabs', () => {
})
).rejects.toMatchObject({
name: 'InitError',
message: 'Root element (`$root`) already initialised (`govuk-tabs`)'
message: 'govuk-tabs: Root element (`$root`) already initialised'
})
})

Expand Down
16 changes: 6 additions & 10 deletions packages/govuk-frontend/src/govuk/errors/index.jsdom.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,21 @@ describe('errors', () => {

describe('InitError', () => {
it('is an instance of GOVUKFrontendError', () => {
expect(new InitError('govuk-accordion')).toBeInstanceOf(
GOVUKFrontendError
)
expect(new InitError(Accordion)).toBeInstanceOf(GOVUKFrontendError)
})

it('has its own name set', () => {
expect(new InitError('govuk-accordion').name).toBe('InitError')
expect(new InitError(Accordion).name).toBe('InitError')
})

it('provides feedback for modules already initialised', () => {
expect(new InitError('govuk-accordion').message).toBe(
'Root element (`$root`) already initialised (`govuk-accordion`)'
expect(new InitError(Accordion).message).toBe(
'govuk-accordion: Root element (`$root`) already initialised'
)
})

it('provides feedback when no module name is provided', () => {
expect(new InitError(undefined, 'Accordion').message).toBe(
'moduleName not defined in component (`Accordion`)'
)
it('allows a custom message to be provided', () => {
expect(new InitError('custom message').message).toBe('custom message')
})
})

Expand Down
25 changes: 15 additions & 10 deletions packages/govuk-frontend/src/govuk/errors/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,18 @@ export class InitError extends GOVUKFrontendError {

/**
* @internal
* @param {string|undefined} moduleName - name of the component module
* @param {string} [className] - name of the component module
* @param {ComponentWithModuleName | string} componentOrMessage - name of the component module
*/
constructor(moduleName, className) {
let errorText = `moduleName not defined in component (\`${className}\`)`
constructor(componentOrMessage) {
const message =
typeof componentOrMessage === 'string'
? componentOrMessage
: formatErrorMessage(
componentOrMessage,
`Root element (\`$root\`) already initialised`
)

if (typeof moduleName === 'string') {
errorText = `Root element (\`$root\`) already initialised (\`${moduleName}\`)`
}

super(errorText)
super(message)
}
}

Expand All @@ -129,5 +130,9 @@ export class InitError extends GOVUKFrontendError {
* @property {string} identifier - An identifier that'll let the user understand which element has an error. This is whatever makes the most sense
* @property {Element | null} [element] - The element in error
* @property {string} [expectedType] - The type that was expected for the identifier
* @property {import('../common/index.mjs').ComponentWithModuleName} component - Component throwing the error
* @property {ComponentWithModuleName} component - Component throwing the error
*/

/**
* @typedef {import('../common/index.mjs').ComponentWithModuleName} ComponentWithModuleName
*/
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class GOVUKFrontendComponent {
// After this, we'll be sure that `childConstructor` has a `moduleName`
// as expected of the `ChildClassConstructor` we've cast `this.constructor` to.
if (typeof childConstructor.moduleName !== 'string') {
throw new InitError(childConstructor.moduleName)
throw new InitError(`\`moduleName\` not defined in component`)
}

childConstructor.checkSupport()
Expand All @@ -48,11 +48,11 @@ export class GOVUKFrontendComponent {
* @throws {InitError} when component is already initialised
*/
checkInitialised($root) {
const moduleName = /** @type {ChildClassConstructor} */ (this.constructor)
.moduleName
const constructor = /** @type {ChildClassConstructor} */ (this.constructor)
const moduleName = constructor.moduleName

if ($root && moduleName && isInitialised($root, moduleName)) {
throw new InitError(moduleName)
throw new InitError(constructor)
}
}

Expand Down

0 comments on commit d830263

Please sign in to comment.