|
| 1 | +import { fireEvent, render, screen } from "@testing-library/react"; |
| 2 | +import { vi } from "vitest"; |
| 3 | + |
| 4 | +import ElevatedCard from "./"; |
| 5 | + |
| 6 | +describe("ElevatedCard component", () => { |
| 7 | + const title = "Curated Resources"; |
| 8 | + const description = |
| 9 | + "Explore our collection of tools, libraries, and tutorials to enhance your development workflow."; |
| 10 | + |
| 11 | + test("renders correctly - snapshot test", () => { |
| 12 | + const { asFragment } = render( |
| 13 | + <ElevatedCard title={title} description={description} /> |
| 14 | + ); |
| 15 | + |
| 16 | + expect(asFragment()).toMatchSnapshot(); |
| 17 | + }); |
| 18 | + |
| 19 | + test("renders title and description", () => { |
| 20 | + render(<ElevatedCard title={title} description={description} />); |
| 21 | + expect(screen.getByText(title)).toBeInTheDocument(); |
| 22 | + expect(screen.getByText(description)).toBeInTheDocument(); |
| 23 | + }); |
| 24 | + |
| 25 | + test("conditionally renders icon", () => { |
| 26 | + render( |
| 27 | + <ElevatedCard |
| 28 | + title={title} |
| 29 | + description={description} |
| 30 | + icon={{ |
| 31 | + iconName: "IconBook", |
| 32 | + dataTestId: "icon-book" |
| 33 | + }} |
| 34 | + /> |
| 35 | + ); |
| 36 | + expect(screen.getByTestId("icon-book")).toBeInTheDocument(); |
| 37 | + }); |
| 38 | + |
| 39 | + test("renders children when provided", () => { |
| 40 | + const childText = "Child Content"; |
| 41 | + render( |
| 42 | + <ElevatedCard title={title} description={description}> |
| 43 | + <div data-testid='child-content'>{childText}</div> |
| 44 | + </ElevatedCard> |
| 45 | + ); |
| 46 | + expect(screen.getByTestId("child-content")).toBeInTheDocument(); |
| 47 | + expect(screen.getByText(childText)).toBeInTheDocument(); |
| 48 | + }); |
| 49 | + |
| 50 | + test("handles click events", () => { |
| 51 | + const onClick = vi.fn(); |
| 52 | + render( |
| 53 | + <ElevatedCard title={title} description={description} onClick={onClick} /> |
| 54 | + ); |
| 55 | + |
| 56 | + fireEvent.click(screen.getByTestId("elevated-card")); |
| 57 | + expect(onClick).toHaveBeenCalledTimes(1); |
| 58 | + }); |
| 59 | + |
| 60 | + test("applies custom class name", () => { |
| 61 | + const customClass = "custom-card-class"; |
| 62 | + |
| 63 | + render( |
| 64 | + <ElevatedCard |
| 65 | + title={title} |
| 66 | + description={description} |
| 67 | + className={customClass} |
| 68 | + /> |
| 69 | + ); |
| 70 | + |
| 71 | + const cardElement = screen.getByTestId("elevated-card"); |
| 72 | + expect(cardElement).toHaveClass(customClass); |
| 73 | + }); |
| 74 | + |
| 75 | + test("uses custom data-testid when provided", () => { |
| 76 | + const customTestId = "custom-card-test-id"; |
| 77 | + render( |
| 78 | + <ElevatedCard |
| 79 | + title={title} |
| 80 | + description={description} |
| 81 | + dataTestId={customTestId} |
| 82 | + /> |
| 83 | + ); |
| 84 | + |
| 85 | + expect(screen.getByTestId(customTestId)).toBeInTheDocument(); |
| 86 | + }); |
| 87 | +}); |
0 commit comments