Skip to content

Commit

Permalink
Add more button tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hetunandu committed Jul 8, 2024
1 parent e26ee7a commit f2d0e9f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from "react";
import SidebarButton, { type SidebarButtonProps } from "./SidebarButton";

import { Condition } from "../../../enums";
import userEvent from "@testing-library/user-event";

const sidebarButtonProps: SidebarButtonProps = {
icon: "down-arrow",
Expand All @@ -24,4 +25,27 @@ describe("SidebarButton", () => {
const svgs = container.querySelectorAll("svg");
expect(svgs).toHaveLength(2);
});

it("should call onClick with urlSuffix", async () => {
const checkOnClick = {
...sidebarButtonProps,
onClick: jest.fn(),
};
const { getByRole } = render(<SidebarButton {...checkOnClick} />);

await userEvent.click(getByRole("button"));
expect(checkOnClick.onClick).toHaveBeenCalledWith(checkOnClick.urlSuffix);
});

it("should not call onClick when button is already selected", async () => {
const withSelected = {
...sidebarButtonProps,
selected: true,
onClick: jest.fn(),
};
const { getByRole } = render(<SidebarButton {...withSelected} />);

await userEvent.click(getByRole("button"));
expect(withSelected.onClick).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function SidebarButton(props: SidebarButtonProps) {
className={`t--sidebar-${title || tooltip}`}
data-selected={selected}
onClick={handleOnClick}
role="button"
selected={selected}
>
<Icon name={icon} size="lg" />
Expand Down

0 comments on commit f2d0e9f

Please sign in to comment.