diff --git a/packages/govuk-frontend/src/govuk/components/accordion/accordion.mjs b/packages/govuk-frontend/src/govuk/components/accordion/accordion.mjs index 98c5445fc0..179e918bf1 100644 --- a/packages/govuk-frontend/src/govuk/components/accordion/accordion.mjs +++ b/packages/govuk-frontend/src/govuk/components/accordion/accordion.mjs @@ -237,8 +237,18 @@ export class Accordion extends GOVUKFrontendComponent { const $heading = $header.querySelector(`.${this.sectionHeadingClass}`) const $summary = $header.querySelector(`.${this.sectionSummaryClass}`) - if (!$span || !$heading) { - return + if (!$heading) { + throw new ElementError({ + componentName: 'Accordion', + identifier: `Section heading (\`.${this.sectionHeadingClass}\`)` + }) + } + + if (!$span) { + throw new ElementError({ + componentName: 'Accordion', + identifier: `Section button (\`.${this.sectionButtonClass}\`)` + }) } // Create a button element that will replace the diff --git a/packages/govuk-frontend/src/govuk/components/accordion/accordion.puppeteer.test.js b/packages/govuk-frontend/src/govuk/components/accordion/accordion.puppeteer.test.js index 4d95160c7a..2c61cda698 100644 --- a/packages/govuk-frontend/src/govuk/components/accordion/accordion.puppeteer.test.js +++ b/packages/govuk-frontend/src/govuk/components/accordion/accordion.puppeteer.test.js @@ -798,6 +798,40 @@ describe('/components/accordion', () => { 'Accordion: Section headers (`.govuk-accordion__section-header`) not found' }) }) + + it('throws when any section heading is missing', async () => { + await expect( + renderAndInitialise(page, 'accordion', examples.default, { + beforeInitialisation($module, { selector }) { + $module.querySelector(selector).remove() + }, + context: { + selector: '.govuk-accordion__section-heading' + } + }) + ).rejects.toEqual({ + name: 'ElementError', + message: + 'Accordion: Section heading (`.govuk-accordion__section-heading`) not found' + }) + }) + + it('throws when any section button span is missing', async () => { + await expect( + renderAndInitialise(page, 'accordion', examples.default, { + beforeInitialisation($module, { selector }) { + $module.querySelector(selector).remove() + }, + context: { + selector: '.govuk-accordion__section-button' + } + }) + ).rejects.toEqual({ + name: 'ElementError', + message: + 'Accordion: Section button (`.govuk-accordion__section-button`) not found' + }) + }) }) }) })