From 2e0e87ea5507a5b4f907f33136e9d2da59535904 Mon Sep 17 00:00:00 2001 From: Sarka Chwastkova Date: Wed, 18 Dec 2024 17:22:46 +0100 Subject: [PATCH] feat(Collapse): set a11y attributes and props --- .../src/Collapse/Collapse.stories.tsx | 6 ++ .../src/Collapse/__tests__/index.test.tsx | 22 ++++--- .../orbit-components/src/Collapse/index.tsx | 59 ++++++++++--------- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/packages/orbit-components/src/Collapse/Collapse.stories.tsx b/packages/orbit-components/src/Collapse/Collapse.stories.tsx index ab49aae7e0..a4513f2ca8 100644 --- a/packages/orbit-components/src/Collapse/Collapse.stories.tsx +++ b/packages/orbit-components/src/Collapse/Collapse.stories.tsx @@ -213,6 +213,12 @@ export const Uncontrolled: Story = { args: { expanded: undefined, }, + + parameters: { + controls: { + exclude: "expanded", + }, + }, }; export const Rtl: Story = { diff --git a/packages/orbit-components/src/Collapse/__tests__/index.test.tsx b/packages/orbit-components/src/Collapse/__tests__/index.test.tsx index 5da88c6b8c..3b3f2fa157 100644 --- a/packages/orbit-components/src/Collapse/__tests__/index.test.tsx +++ b/packages/orbit-components/src/Collapse/__tests__/index.test.tsx @@ -4,10 +4,7 @@ import userEvent from "@testing-library/user-event"; import { render, screen } from "../../test-utils"; import Collapse from ".."; -const toggleButtons = [ - [0, "label"], - [1, "icon button"], -]; +const toggleTriggers = ["div", "button"]; describe("Collapse", () => { const user = userEvent.setup(); @@ -31,27 +28,34 @@ describe("Collapse", () => { expect(screen.getByText("customLabel")).toBeInTheDocument(); }); - it.each(toggleButtons)("should trigger click handler when clicking on %s", async buttonIndex => { + it.each(toggleTriggers)("should trigger click handler when clicking on %s", async trigger => { const onClick = jest.fn(); render(
children
, ); - const toggleButton = screen.getAllByRole("button", { name: "Collapse" })[buttonIndex]; + + const toggleButton = + trigger === "div" ? screen.getByText("Collapse") : screen.getByRole(trigger); + await user.click(toggleButton); expect(onClick).toHaveBeenCalled(); }); describe("uncontrolled", () => { - it.each(toggleButtons)("should expand/collapse when clicking on %s", async buttonIndex => { + it.each(toggleTriggers)("should expand/collapse when clicking on %s", async trigger => { render(
children
, ); - const toggleButton = screen.getAllByRole("button", { name: "Collapse" })[buttonIndex]; - // with ByRole we can test whether the content is visible because of aria-hidden + + const toggleButton = + // We need to distinguish between div and button on the next line + // eslint-disable-next-line jest/no-if + trigger === "div" ? screen.getByText("Collapse") : screen.getByRole("button"); + expect(screen.queryByRole("article")).not.toBeInTheDocument(); await user.click(toggleButton); expect(screen.getByRole("article")).toBeInTheDocument(); diff --git a/packages/orbit-components/src/Collapse/index.tsx b/packages/orbit-components/src/Collapse/index.tsx index d344cde17e..8d72f83950 100644 --- a/packages/orbit-components/src/Collapse/index.tsx +++ b/packages/orbit-components/src/Collapse/index.tsx @@ -64,36 +64,39 @@ const Collapse = ({ data-test={dataTest} id={id} > - {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events */} -
- - {label && !customLabel && {label}} - {customLabel} - - {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */} -
{ - ev.stopPropagation(); - }} - > +
+
{ + if (e.key === "Enter" || e.key === " ") { + handleClick(e); + } + }} + > + + {label && !customLabel && {label}} + {customLabel} + +
+ + {actions && ( +
{actions}
- } - size="small" - type="secondary" - ariaLabelledby={labelID} - ariaExpanded={expanded} - ariaControls={slideID} - /> -
+ )} + } + size="small" + type="secondary" + onClick={handleClick} + ariaLabelledby={labelID} + ariaExpanded={expanded} + ariaControls={slideID} + />