Skip to content

Commit

Permalink
Throw errors if section heading or button missing
Browse files Browse the repository at this point in the history
  • Loading branch information
domoscargin committed Oct 19, 2023
1 parent 48d8ff3 commit 0bc19c0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
})
})
})
})
Expand Down

0 comments on commit 0bc19c0

Please sign in to comment.