Skip to content

Commit

Permalink
test: added tests for new button
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraghazra committed Jul 14, 2021
1 parent f7ea32d commit 2ab5561
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
85 changes: 85 additions & 0 deletions src/button/__tests__/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,91 @@ describe("Testing Button", () => {
expect(screen.getByRole("button")).toHaveTextContent(/hello world/i);
});

it("should render properly with iconOnly", () => {
render(<Button iconOnly={<p>Icon only</p>}>hello world</Button>);

expect(screen.queryByText(/hello world/i)).not.toBeInTheDocument();
expect(screen.queryByText(/Icon only/i)).toBeInTheDocument();
});

test("prefix, suffix & children should be ignored when iconOnly is set", () => {
render(
<Button
prefix={<p>prefix</p>}
suffix={<p>suffix</p>}
iconOnly={<p>Icon only</p>}
>
hello world
</Button>,
);

expect(screen.queryByText(/hello world/i)).not.toBeInTheDocument();
expect(screen.queryByText("prefix")).not.toBeInTheDocument();
expect(screen.queryByText("suffix")).not.toBeInTheDocument();
expect(screen.queryByText(/Icon only/i)).toBeInTheDocument();
});

it("should render with loading spinner when loading is true", () => {
render(<Button loading>hello world</Button>);

expect(screen.getByTestId("testid-spinner")).toBeInTheDocument();
});

it("should render a custom loading spinner", () => {
render(
<Button loading spinner={<p>spinning</p>}>
hello world
</Button>,
);

expect(screen.queryByText("spinning")).toBeInTheDocument();
});

it("should render with prefix suffix", () => {
render(
<Button prefix={<p>prefix</p>} suffix={<p>suffix</p>}>
hello world
</Button>,
);

expect(screen.queryByText("prefix")).toBeInTheDocument();
expect(screen.queryByText("suffix")).toBeInTheDocument();
});

test("when loading, replace prefix with spinner when prefix is provided", () => {
render(
<Button prefix={<p>prefix</p>} loading>
hello world
</Button>,
);

expect(screen.queryByText("prefix")).not.toBeInTheDocument();
expect(screen.getByTestId("testid-spinner")).toBeInTheDocument();
});

test("when loading, replace suffix with spinner when suffix is provided", () => {
render(
<Button suffix={<p>suffix</p>} loading>
hello world
</Button>,
);

expect(screen.queryByText("suffix")).not.toBeInTheDocument();
expect(screen.getByTestId("testid-spinner")).toBeInTheDocument();
});

test("when loading, replace suffix with spinner when both suffix and prefix are provided", () => {
render(
<Button prefix={<p>prefix</p>} suffix={<p>suffix</p>} loading>
hello world
</Button>,
);

expect(screen.queryByText("prefix")).toBeInTheDocument();
expect(screen.queryByText("suffix")).not.toBeInTheDocument();
expect(screen.getByTestId("testid-spinner")).toBeInTheDocument();
});

it("should not have axe violations", async () => {
testA11y(<Button>Ally</Button>);
});
Expand Down
2 changes: 1 addition & 1 deletion src/button/stories/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
CloseButtonProps,
} from "../index";
import { Spinner } from "../../spinner";
import { CaretRightIcon, ClockIcon } from "../../icons";
import { CaretRightIcon, ClockIcon, SearchIcon } from "../../icons";
import { createControls } from "../../../.storybook/storybookUtils";

export default {
Expand Down
7 changes: 6 additions & 1 deletion src/spinner/Spinner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ export const Spinner = forwardRefWithAs<SpinnerProps, HTMLDivElement, "div">(
);

return (
<Box ref={ref} className={spinnerStyles} {...rest}>
<Box
ref={ref}
className={spinnerStyles}
data-testid="testid-spinner"
{...rest}
>
{label && <div className={theme.spinner.aria}>{label}</div>}
</Box>
);
Expand Down

0 comments on commit 2ab5561

Please sign in to comment.