From bb0a0f2d2b059a419d88d04e7f447ad37c2f0a8c 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 | 32 +++++------ .../orbit-components/src/Collapse/index.tsx | 57 +++++++++---------- 3 files changed, 49 insertions(+), 46 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..2b138effc3 100644 --- a/packages/orbit-components/src/Collapse/__tests__/index.test.tsx +++ b/packages/orbit-components/src/Collapse/__tests__/index.test.tsx @@ -4,11 +4,6 @@ import userEvent from "@testing-library/user-event"; import { render, screen } from "../../test-utils"; import Collapse from ".."; -const toggleButtons = [ - [0, "label"], - [1, "icon button"], -]; - describe("Collapse", () => { const user = userEvent.setup(); @@ -31,32 +26,35 @@ describe("Collapse", () => { expect(screen.getByText("customLabel")).toBeInTheDocument(); }); - it.each(toggleButtons)("should trigger click handler when clicking on %s", async buttonIndex => { + it("should trigger click handler when clicking on button", async () => { const onClick = jest.fn(); render(
children
, ); - const toggleButton = screen.getAllByRole("button", { name: "Collapse" })[buttonIndex]; - await user.click(toggleButton); - expect(onClick).toHaveBeenCalled(); + + screen.getAllByRole("button").forEach(async toggleButton => { + await user.click(toggleButton); + expect(onClick).toHaveBeenCalled(); + }); }); describe("uncontrolled", () => { - it.each(toggleButtons)("should expand/collapse when clicking on %s", async buttonIndex => { + it("should expand/collapse when clicking on button", async () => { render(
children
, ); - const toggleButton = screen.getAllByRole("button", { name: "Collapse" })[buttonIndex]; - // with ByRole we can test whether the content is visible because of aria-hidden - expect(screen.queryByRole("article")).not.toBeInTheDocument(); - await user.click(toggleButton); - expect(screen.getByRole("article")).toBeInTheDocument(); - await user.click(toggleButton); - expect(screen.queryByRole("article")).not.toBeInTheDocument(); + + await screen.getAllByRole("button").forEach(async toggleButton => { + expect(screen.queryByRole("article")).not.toBeInTheDocument(); + await user.click(toggleButton); + expect(screen.getByRole("article")).toBeInTheDocument(); + await user.click(toggleButton); + expect(screen.queryByRole("article")).not.toBeInTheDocument(); + }); }); }); diff --git a/packages/orbit-components/src/Collapse/index.tsx b/packages/orbit-components/src/Collapse/index.tsx index d344cde17e..e0cd4ba66a 100644 --- a/packages/orbit-components/src/Collapse/index.tsx +++ b/packages/orbit-components/src/Collapse/index.tsx @@ -64,36 +64,35 @@ 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(); - }} - > - {actions} -
- } - size="small" - type="secondary" - ariaLabelledby={labelID} - ariaExpanded={expanded} - ariaControls={slideID} - /> +
+
{ + if (e.key === "Enter" || e.key === " ") { + handleClick(e); + } + }} + > + + {label && !customLabel && {label}} + {customLabel} +
+ + {actions &&
{actions}
} + } + size="small" + type="secondary" + onClick={handleClick} + ariaLabelledby={labelID} + ariaExpanded={expanded} + ariaControls={slideID} + />