Skip to content

Commit

Permalink
Add tests for Button and ButtonLink components
Browse files Browse the repository at this point in the history
A new test file, Button.component.test.tsx, has been added for the Button component to validate if it renders a button and correctly displays the provided children as text. Tests in ButtonLink.component.test.tsx have been reorganized for clarity. The tests now more clearly verify rendering of a link and the display of children as text in the ButtonLink component.
  • Loading branch information
josch87 committed Jun 9, 2024
1 parent aaa54f3 commit 911a03d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
23 changes: 23 additions & 0 deletions frontend/src/components/Button/Button.component.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";
import Button from "./Button.tsx";

test("Button component renders a button", () => {
render(
<Button buttonType="delete" handleOnClick={() => {}}>
Delete
</Button>
);
const button = screen.getByRole("button");
expect(button).toBeInTheDocument();
});

test("Button component renders children as text", () => {
render(
<Button buttonType="delete" handleOnClick={() => {}}>
Delete
</Button>
);
const text = screen.getByText(/delete/i);
expect(text).toBeInTheDocument();
});
16 changes: 8 additions & 8 deletions frontend/src/components/ButtonLink/ButtonLink.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ import "@testing-library/jest-dom";
import ButtonLink from "./ButtonLink.tsx";
import { MemoryRouter } from "react-router-dom";

test("ButtonLink component displays children as text", () => {
test("ButtonLink component renders a link", () => {
render(
<MemoryRouter>
<ButtonLink href="/my-url">Click here</ButtonLink>
</MemoryRouter>
);
const text = screen.getByText(/click here/i);
expect(text).toBeInTheDocument();
const button = screen.getByRole("link");
expect(button).toBeInTheDocument();
});

test("ButtonLink component displays a link", () => {
test("ButtonLink component links to the correct target", () => {
render(
<MemoryRouter>
<ButtonLink href="/my-url">Click here</ButtonLink>
</MemoryRouter>
);
const button = screen.getByRole("link");
expect(button).toBeInTheDocument();
expect(button).toHaveAttribute("href", "/my-url");
});

test("ButtonLink component links to the correct target", () => {
test("ButtonLink component renders children as text", () => {
render(
<MemoryRouter>
<ButtonLink href="/my-url">Click here</ButtonLink>
</MemoryRouter>
);
const button = screen.getByRole("link");
expect(button).toHaveAttribute("href", "/my-url");
const text = screen.getByText(/click here/i);
expect(text).toBeInTheDocument();
});

0 comments on commit 911a03d

Please sign in to comment.