diff --git a/src/components/Accordion/Accordion.test.tsx b/src/components/Accordion/Accordion.test.tsx index 2dddb7388f..df8c7cb0d2 100644 --- a/src/components/Accordion/Accordion.test.tsx +++ b/src/components/Accordion/Accordion.test.tsx @@ -377,4 +377,26 @@ describe('Accordion component', () => { } ) }) + + describe('with a custom click handler', () => { + const customToggleFunction = vi.fn() + + const customTestItems: AccordionItemProps[] = [ + { + title: 'First Amendment', + content: firstAmendment, + expanded: false, + id: '123', + headingLevel: 'h4', + handleToggle: () => { + customToggleFunction() + }, + }, + ] + it('fires the handler successfully', () => { + const { getByText } = render() + fireEvent.click(getByText(testItems[0].title as string)) + expect(customToggleFunction).toHaveBeenCalledOnce() + }) + }) }) diff --git a/src/components/Accordion/Accordion.tsx b/src/components/Accordion/Accordion.tsx index 9314517076..b89a9c70aa 100644 --- a/src/components/Accordion/Accordion.tsx +++ b/src/components/Accordion/Accordion.tsx @@ -108,7 +108,8 @@ export const Accordion = ({ key={`accordionItem_${i}`} {...item} expanded={openItems.indexOf(item.id) > -1} - handleToggle={(): void => { + handleToggle={(e): void => { + if (item.handleToggle) item.handleToggle(e) toggleItem(item.id) }} />