Skip to content

Commit

Permalink
Throw error if there are no checkbox items
Browse files Browse the repository at this point in the history
  • Loading branch information
domoscargin committed Sep 20, 2023
1 parent cf41055 commit df9c59a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ export class Checkboxes extends GOVUKFrontendComponent {
/** @satisfies {NodeListOf<HTMLInputElement>} */
const $inputs = $module.querySelectorAll('input[type="checkbox"]')
if (!$inputs.length) {
return this
throw new ElementError(null, {
componentName: 'Checkboxes',
identifier: '<input type="checkbox">',
expectedType: HTMLElement
})
}

this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,24 @@ describe('Checkboxes with multiple groups and a "None" checkbox and conditional
message: 'Checkboxes: $module is not an instance of "HTMLElement"'
})
})

it('throws when the input list is empty', async () => {
await expect(
renderAndInitialise(page, 'checkboxes', {
params: examples.default,
beforeInitialisation($module) {
$module
.querySelectorAll('.govuk-checkboxes__item')
.forEach((item) => {
item.remove()
})
}
})
).rejects.toEqual({
name: 'ElementError',
message: 'Checkboxes: <input type="checkbox"> not found'
})
})
})
})
})

0 comments on commit df9c59a

Please sign in to comment.