Skip to content

Commit

Permalink
Use error message strings instead of cause
Browse files Browse the repository at this point in the history
  • Loading branch information
domoscargin committed Sep 7, 2023
1 parent c61fb5a commit e44f67f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class SkipLink extends GOVUKFrontendComponent {

/**
* @param {Element} $module - HTML element to use for skip link
* @throws {MissingElementError} If the element with the specified ID is not found
*/
constructor($module) {
super()
Expand All @@ -35,12 +36,11 @@ export class SkipLink extends GOVUKFrontendComponent {
try {
const $linkedElement = this.getLinkedElement()
this.$linkedElement = $linkedElement
} catch (cause) {
} catch (error) {
throw new MissingElementError(
'Skip link: the linked HTML element does not exist',
{
cause: cause instanceof Error ? cause : undefined
}
`Skip link: ${
error instanceof Error ? error.message : 'Linked element not found'
}`
)
}

Expand All @@ -52,22 +52,20 @@ export class SkipLink extends GOVUKFrontendComponent {
*
* @private
* @throws {Error} If the "href" attribute does not contain a hash
* @throws {Error} If the element with the specified ID does not exist
* @throws {TypeError} If the element with the specified ID is not found
* @returns {HTMLElement} $linkedElement - DOM element linked to from the skip link
*/
getLinkedElement() {
const linkedElementId = this.getFragmentFromUrl()
if (!linkedElementId) {
throw new Error(
`Skip link: $module "href" attribute does not contain a hash`
)
throw new Error(`$module "href" attribute does not contain a hash`)
}

const linkedElement = document.getElementById(linkedElementId)

if (!linkedElement) {
throw new Error(
`Skip link: Target selector "#${linkedElementId}" not found`
throw new TypeError(
`Linked element selector "#${linkedElementId}" not found`
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,22 @@ describe('Skip Link', () => {
})
).rejects.toEqual({

Check failure on line 93 in packages/govuk-frontend/src/govuk/components/skip-link/skip-link.test.js

View workflow job for this annotation

GitHub Actions / JavaScript component tests (ubuntu-latest)

Skip Link › errors at instantiation › throws when the linked element is missing

expect(received).rejects.toEqual(expected) // deep equality - Expected - 1 + Received + 1 Object { - "message": "Skip link: Target selector \"#this-element-does-not-exist\" not found", + "message": "Skip link: Linked element selector \"#this-element-does-not-exist\" not found", "name": "MissingElementError", } at Object.toEqual (node_modules/expect/build/index.js:218:22) at Object.<anonymous> (packages/govuk-frontend/src/govuk/components/skip-link/skip-link.test.js:93:17)
name: 'MissingElementError',
message: 'Skip link: the linked HTML element does not exist'
message:
'Skip link: Target selector "#this-element-does-not-exist" not found'
})
})

it('throws when the href does not contain a hash', async () => {
await expect(
renderAndInitialise(page, 'skip-link', {
params: {
text: 'Skip to main content',
href: 'this-element-does-not-exist'
}
})
).rejects.toEqual({
name: 'MissingElementError',
message: 'Skip link: $module "href" attribute does not contain a hash'
})
})
})
Expand Down

0 comments on commit e44f67f

Please sign in to comment.