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 all commits
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
84 changes: 84 additions & 0 deletions src/components/SideNote/__test__/SideNote.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import { SideNote } from "..";

describe("SideNote Component", () => {

it("renders properly with default props", () => {
const { getByText, queryByTestId, container } = render(
<SideNote>
<div>Child content</div>
</SideNote>,
);
const title = queryByTestId("dt_deriv-side-note-title");
const content = getByText("Child content");
const actionButton = container.getElementsByClassName('deriv-side-note__action');
expect(title).not.toBeInTheDocument();
expect(content).toBeInTheDocument();
expect(actionButton).toHaveLength(0);
});

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

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" />);
const title = getByText("Title");
const child = queryByText("Child content");
expect(title).toBeInTheDocument();
expect(child).toBeNull();
});

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

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

});
2 changes: 1 addition & 1 deletion src/components/SideNote/index.tsx
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ export const SideNote = ({
}: PropsWithChildren<SideNoteProps>) => (
<div className={clsx("deriv-side-note", className)} {...props}>
{title && (
<Text size={titleSize} align="left" weight="bold">
<Text data-testid="dt_deriv-side-note-title" size={titleSize} align="left" weight="bold">
{title}
</Text>
)}
7 changes: 4 additions & 3 deletions src/components/Text/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { CSSProperties, ElementType, ReactNode } from "react";
import React, { CSSProperties,ComponentProps, ElementType, ReactNode } from "react";
import clsx from "clsx";
import "./Text.scss";

@@ -29,7 +29,7 @@ type TGenericSizes =
| "xl"
| "xs";

export interface TextProps {
export interface TextProps extends ComponentProps<ElementType> {
align?: CSSProperties["textAlign"];
as?: ElementType;
children: ReactNode;
@@ -51,6 +51,7 @@ export const Text = ({
size = "md",
weight = "normal",
className,
...rest
}: TextProps) => {
const textClassNames = clsx(
"deriv-text",
@@ -65,5 +66,5 @@ export const Text = ({

const Tag = as;

return <Tag className={textClassNames}>{children}</Tag>;
return <Tag className={textClassNames} {...rest}>{children}</Tag>;
};