diff --git a/src/components/SegmentedControl/SegmentedControl.stories.tsx b/src/components/SegmentedControl/SegmentedControl.stories.tsx index f0720f2c..1b63a82b 100644 --- a/src/components/SegmentedControl/SegmentedControl.stories.tsx +++ b/src/components/SegmentedControl/SegmentedControl.stories.tsx @@ -26,6 +26,7 @@ export const Default: Story = { sharable, reusable, tiny applications called Charmed Operators.
), + onClick: () => console.log("OLM clicked"), }, { label: "SDK", @@ -35,6 +36,7 @@ export const Default: Story = { them as Charms. ), + onClick: () => console.log("SDK clicked"), }, { label: "Charmhub", diff --git a/src/components/SegmentedControl/SegmentedControl.test.tsx b/src/components/SegmentedControl/SegmentedControl.test.tsx index ecdf40d9..0de7762b 100644 --- a/src/components/SegmentedControl/SegmentedControl.test.tsx +++ b/src/components/SegmentedControl/SegmentedControl.test.tsx @@ -62,6 +62,42 @@ describe("SegmentedControl", () => { ); }); + it("calls a segment onClick handler when clicked", async () => { + const user = userEvent.setup(); + let currentTarget: HTMLButtonElement | null = null; + const onClick = jest.fn((event: React.MouseEventcontent2
, + onClick, + }, + ]} + />, + ); + + await user.click(screen.getByRole("tab", { name: "label2" })); + + expect(onClick).toHaveBeenCalledTimes(1); + expect(currentTarget).toBe(screen.getByRole("tab", { name: "label2" })); + expect(screen.getByRole("tab", { name: "label2" })).toHaveAttribute( + "aria-selected", + "true", + ); + expect(screen.getByRole("tabpanel", { name: "label2" })).toHaveTextContent( + "content2", + ); + }); + it("uses a roving tab index", () => { render(