Skip to content

Commit

Permalink
Throw error when section content is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
domoscargin committed Oct 19, 2023
1 parent 0bc19c0 commit 53a2a6a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,22 @@ export class Accordion extends GOVUKFrontendComponent {
const $button = $section.querySelector(`.${this.sectionButtonClass}`)
const $content = $section.querySelector(`.${this.sectionContentClass}`)

if (!$showHideIcon || !$showHideText || !$button || !$content) {
if (!$button) {
throw new ElementError({
componentName: 'Accordion',
identifier: `Section button (\`.${this.sectionButtonClass}\`)`
})
}

if (!$content) {
throw new ElementError({
componentName: 'Accordion',
identifier: `Section content (\`.${this.sectionContentClass}\`)`
})
}

if (!$showHideIcon || !$showHideText) {
// Return early for elements we create
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,23 @@ describe('/components/accordion', () => {
'Accordion: Section button (`.govuk-accordion__section-button`) not found'
})
})

it('throws when any section content is missing', async () => {
await expect(
renderAndInitialise(page, 'accordion', examples.default, {
beforeInitialisation($module, { selector }) {
$module.querySelector(selector).remove()
},
context: {
selector: '.govuk-accordion__section-content'
}
})
).rejects.toEqual({
name: 'ElementError',
message:
'Accordion: Section content (`.govuk-accordion__section-content`) not found'
})
})
})
})
})
Expand Down

0 comments on commit 53a2a6a

Please sign in to comment.