|
| 1 | +import React from "react"; |
| 2 | +import { render, fireEvent } from "@testing-library/react"; |
| 3 | +import { SideNote } from ".."; |
| 4 | + |
| 5 | +describe("SideNote Component", () => { |
| 6 | + |
| 7 | + it("renders properly with default props", () => { |
| 8 | + const { getByText, queryByTestId, container } = render( |
| 9 | + <SideNote> |
| 10 | + <div>Child content</div> |
| 11 | + </SideNote>, |
| 12 | + ); |
| 13 | + const title = queryByTestId("dt_deriv-side-note-title"); |
| 14 | + const content = getByText("Child content"); |
| 15 | + const actionButton = container.getElementsByClassName('deriv-side-note__action'); |
| 16 | + expect(title).not.toBeInTheDocument(); |
| 17 | + expect(content).toBeInTheDocument(); |
| 18 | + expect(actionButton).toHaveLength(0); |
| 19 | + }); |
| 20 | + |
| 21 | + it("renders children and title correctly", () => { |
| 22 | + const { getByText } = render( |
| 23 | + <SideNote title="Title"> |
| 24 | + <div>Child content</div> |
| 25 | + </SideNote> |
| 26 | + ); |
| 27 | + const title =getByText("Title"); |
| 28 | + const child=getByText("Child content") |
| 29 | + expect(title).toBeInTheDocument(); |
| 30 | + expect(child).toBeInTheDocument(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("renders action button and handles click event", () => { |
| 34 | + const handleClick = jest.fn(); |
| 35 | + const { getByText } = render( |
| 36 | + <SideNote actionClick={handleClick} actionLabel="Action"> |
| 37 | + <div>Child content</div> |
| 38 | + </SideNote> |
| 39 | + ); |
| 40 | + const actionButton = getByText("Action"); |
| 41 | + fireEvent.click(actionButton); |
| 42 | + expect(handleClick).toHaveBeenCalled(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("applies additional className correctly", () => { |
| 46 | + const { container } = render( |
| 47 | + <SideNote className="custom-class"> |
| 48 | + <div>Child content</div> |
| 49 | + </SideNote> |
| 50 | + ); |
| 51 | + expect(container.firstChild).toHaveClass("deriv-side-note", "custom-class"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("renders only title when children are not provided", () => { |
| 55 | + const { getByText, queryByText } = render(<SideNote title="Title" />); |
| 56 | + const title = getByText("Title"); |
| 57 | + const child = queryByText("Child content"); |
| 58 | + expect(title).toBeInTheDocument(); |
| 59 | + expect(child).toBeNull(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("renders only children when title is not provided", () => { |
| 63 | + const { getByText, queryByText } = render( |
| 64 | + <SideNote> |
| 65 | + <div>Child content</div> |
| 66 | + </SideNote> |
| 67 | + ); |
| 68 | + const title = queryByText("Title"); |
| 69 | + const child = getByText("Child content"); |
| 70 | + expect(child).toBeInTheDocument(); |
| 71 | + expect(title).toBeNull(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("renders action button with default label if no actionLabel provided", () => { |
| 75 | + const { getByText } = render( |
| 76 | + <SideNote actionClick={() => { }}> |
| 77 | + <div>Child content</div> |
| 78 | + </SideNote> |
| 79 | + ); |
| 80 | + const action = getByText("Learn more") |
| 81 | + expect(action).toBeInTheDocument(); |
| 82 | + }); |
| 83 | + |
| 84 | +}); |
0 commit comments