From 7801f8a56874692154e187d85ec6908ee0b7e2de Mon Sep 17 00:00:00 2001 From: Brett Kyle Date: Tue, 5 Sep 2023 14:22:42 +0100 Subject: [PATCH] Throw MissingElementError if key HTML missing --- src/govuk/components/skip-link/skip-link.mjs | 3 ++- src/govuk/components/skip-link/skip-link.test.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/govuk/components/skip-link/skip-link.mjs b/src/govuk/components/skip-link/skip-link.mjs index 4c15ca2669..cae167bc0f 100644 --- a/src/govuk/components/skip-link/skip-link.mjs +++ b/src/govuk/components/skip-link/skip-link.mjs @@ -1,3 +1,4 @@ +import { MissingElementError } from '../../errors/index.mjs' import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs' /** @@ -33,7 +34,7 @@ export class SkipLink extends GOVUKFrontendComponent { // Check for linked element const $linkedElement = this.getLinkedElement() if (!$linkedElement) { - return + throw new MissingElementError('The linked HTML element does not exist') } this.$linkedElement = $linkedElement diff --git a/src/govuk/components/skip-link/skip-link.test.js b/src/govuk/components/skip-link/skip-link.test.js index ad065b1a65..14f708f74b 100644 --- a/src/govuk/components/skip-link/skip-link.test.js +++ b/src/govuk/components/skip-link/skip-link.test.js @@ -81,5 +81,19 @@ describe('Skip Link', () => { message: 'GOV.UK Frontend is not supported in this browser' }) }) + + it('throws when the linked element is missing', async () => { + await expect( + renderAndInitialise(page, 'skip-link', { + params: { + text: 'Skip to main content', + href: '#this-element-does-not-exist' + } + }) + ).rejects.toEqual({ + name: 'MissingElementError', + message: 'The linked HTML element does not exist' + }) + }) }) })