Skip to content

Commit

Permalink
feat(Button): add aria-hidden prop (#1936)
Browse files Browse the repository at this point in the history
  • Loading branch information
YossiSaadi committed Feb 14, 2024
1 parent 816038e commit 9d5ed1e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
11 changes: 9 additions & 2 deletions packages/core/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export interface ButtonProps extends VibeComponentProps {
/** aria controls - receives id for the controlled region */
ariaControls?: string;
"aria-describedby"?: AriaAttributes["aria-describedby"];
/**
* aria to be used for screen reader to know if the button is hidden
*/
"aria-hidden"?: AriaAttributes["aria-hidden"];
/** On Button Focus callback */
onFocus?: (event: React.FocusEvent<HTMLButtonElement>) => void;
/** On Button Blur callback */
Expand Down Expand Up @@ -136,6 +140,7 @@ const Button: VibeComponent<ButtonProps, unknown> & {
ariaExpanded,
ariaControls,
"aria-describedby": ariaDescribedBy,
"aria-hidden": ariaHidden,
blurOnMouseUp,
dataTestId: backwardCompatabilityDataTestId,
"data-testid": dataTestId,
Expand Down Expand Up @@ -260,7 +265,8 @@ const Button: VibeComponent<ButtonProps, unknown> & {
"aria-haspopup": ariaHasPopup,
"aria-expanded": ariaExpanded,
"aria-controls": ariaControls,
"aria-describedby": ariaDescribedBy
"aria-describedby": ariaDescribedBy,
"aria-hidden": ariaHidden
};
return props;
}, [
Expand All @@ -284,7 +290,8 @@ const Button: VibeComponent<ButtonProps, unknown> & {
ariaHasPopup,
ariaExpanded,
ariaControls,
ariaDescribedBy
ariaDescribedBy,
ariaHidden
]);

const leftIconSize = useMemo(() => {
Expand Down
78 changes: 45 additions & 33 deletions packages/core/src/components/Button/__tests__/button.jest.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import React from "react";
import { render, fireEvent, cleanup, waitFor } from "@testing-library/react";
import "@testing-library/jest-dom";
import Button from "../Button";

const text = "Click Me!";
const className = "test-class";

function renderComponent(props) {
return render(<Button {...props}>{text}</Button>);
}

describe("<Buttoon />", () => {
let clickActionStub;
let onMouseDownStub;
let buttonComponent;

beforeEach(() => {
clickActionStub = jest.fn();
onMouseDownStub = jest.fn();
buttonComponent = render(
<Button className={className} onClick={clickActionStub} onMouseDown={onMouseDownStub}>
{text}
</Button>
);
});

describe("<Button />", () => {
afterEach(() => {
cleanup();
});

describe("click", () => {
let clickActionStub;
let onMouseDownStub;
let buttonComponent;

beforeEach(() => {
clickActionStub = jest.fn();
onMouseDownStub = jest.fn();
buttonComponent = render(
<Button onClick={clickActionStub} onMouseDown={onMouseDownStub}>
{text}
</Button>
);
});

it("should call the click callback when clicked", () => {
const { getByText } = buttonComponent;
fireEvent.click(getByText(text));
Expand Down Expand Up @@ -99,17 +96,19 @@ describe("<Buttoon />", () => {

it("should call on blur", () => {
const onBlur = jest.fn();
cleanup();
const { getByText } = renderComponent({ onBlur });
const { getByText } = render(<Button onBlur={onBlur}>{text}</Button>);
const button = getByText(text);
fireEvent.blur(button);
expect(onBlur.mock.calls.length).toEqual(1);
});

it("should call do blur on mouseup", () => {
const onBlur = jest.fn();
cleanup();
const { getByText } = renderComponent({ onBlur, blurOnMouseUp: false });
const { getByText } = render(
<Button onBlur={onBlur} blurOnMouseUp={false}>
{text}
</Button>
);
const button = getByText(text);
fireEvent.focus(button);
fireEvent.mouseUp(button);
Expand All @@ -118,43 +117,56 @@ describe("<Buttoon />", () => {

it("should call on focus", () => {
const onFocus = jest.fn();
cleanup();
const { getByText } = renderComponent({ onFocus });
const { getByText } = render(<Button onFocus={onFocus}>{text}</Button>);
const button = getByText(text);
fireEvent.focus(button);
expect(onFocus.mock.calls.length).toEqual(1);
});

describe("mouse down", () => {
const onClick = jest.fn();
it("should call the click callback when clicked", () => {
const { getByText } = buttonComponent;
const { getByText } = render(<Button onClick={onClick}>{text}</Button>);
fireEvent.mouseDown(getByText(text));
expect(clickActionStub.mock.calls.length).toEqual(0);
expect(onClick.mock.calls.length).toEqual(0);
});

it("should call mouse down callback", () => {
const { getByText, rerender } = buttonComponent;
const onMouseDown = jest.fn();
rerender(
<Button onClick={clickActionStub} onMouseDown={onMouseDown}>
const { getByText } = render(
<Button onClick={onClick} onMouseDown={onMouseDown}>
{text}
</Button>
);
fireEvent.mouseDown(getByText(text));
expect(onMouseDown.mock.calls.length).toEqual(1);
});
});

describe("a11y", () => {
it("Aria label should be connected to the button", () => {
const ariaLabel = "Icon Name";
const onClick = jest.fn();
const { getByLabelText } = render(
<Button ariaLabel={ariaLabel} className={className} onClick={clickActionStub} onMouseDown={onMouseDownStub}>
<Button ariaLabel={ariaLabel} onClick={onClick}>
{text}
</Button>
);
const buttonElement = getByLabelText(ariaLabel);
fireEvent.click(buttonElement);
expect(clickActionStub.mock.calls.length).toEqual(1);
expect(onClick.mock.calls.length).toEqual(1);
});

it("should apply aria-hidden attribute to button", () => {
const { getByRole } = render(<Button aria-hidden>{text}</Button>);
const buttonElement = getByRole("button", { hidden: true });
expect(buttonElement).toHaveAttribute("aria-hidden", "true");
});

it("should not apply aria-hidden attribute to button", () => {
const { getByRole } = render(<Button>{text}</Button>);
const buttonElement = getByRole("button");
expect(buttonElement).not.toHaveAttribute("aria-hidden");
});
});
});

0 comments on commit 9d5ed1e

Please sign in to comment.