Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEQ]Yaswanth/FEQ-1974_Added test cases for sidenote component #137

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/components/SideNote/__test__/SideNote.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import { SideNote } from "..";

describe("SideNote Component", () => {
it("renders children and title correctly", () => {
const { getByText } = render(
<SideNote title="Title">
<div>Child content</div>
</SideNote>
);
expect(getByText("Title")).toBeInTheDocument();
expect(getByText("Child content")).toBeInTheDocument();
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certain variables such as title is being repeated and I suggest to define them in a beforeEach and reuse them in different tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't use it in the beforeEach method because I've been using queryByText, getByText, and queryByTestId for specific cases.


it("renders action button and handles click event", () => {
const handleClick = jest.fn();
const { getByText } = render(
<SideNote actionClick={handleClick} actionLabel="Action">
<div>Child content</div>
</SideNote>
);
const actionButton = getByText("Action");
fireEvent.click(actionButton);
expect(handleClick).toHaveBeenCalled();
});

it("applies additional className correctly", () => {
const { container } = render(
<SideNote className="custom-class">
<div>Child content</div>
</SideNote>
);
expect(container.firstChild).toHaveClass("deriv-side-note", "custom-class");
});

it("renders only title when children are not provided", () => {
const { getByText, queryByText } = render(<SideNote title="Title" />);
expect(getByText("Title")).toBeInTheDocument();
expect(queryByText("Child content")).toBeNull();
});

it("renders only children when title is not provided", () => {
const { getByText, queryByText } = render(
<SideNote>
<div>Child content</div>
</SideNote>
);
expect(getByText("Child content")).toBeInTheDocument();
expect(queryByText("Title")).toBeNull();
});

it("renders action button with default label if no actionLabel provided", () => {
const { getByText } = render(
<SideNote actionClick={() => {}}>
<div>Child content</div>
</SideNote>
);
expect(getByText("Learn more")).toBeInTheDocument();
});

});