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

Add tests for ProductHighlight Component #336

Merged
merged 2 commits into from
Dec 6, 2024
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
34 changes: 34 additions & 0 deletions src/components/ProductHighlight/ProductHighlight.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { render, screen } from "@testing-library/react";
import ProductHighlight from "./ProductHighlight";
import { CHECK_IN, AWARD, CANCELLATION } from "../../constants/constants";
import { describe, it, expect } from "vitest";

describe("ProductHighlight Component", () => {
const mockHighlights = [
{ type: CHECK_IN, text: "Easy Check-In", subText: "Arrive anytime after 3 PM" },
{ type: AWARD, text: "Award-Winning Stay", subText: "Winner of 2023 Travelers' Choice" },
{ type: CANCELLATION, text: "Flexible Cancellation", subText: "Get a full refund if you change your mind." },
{ type: "OTHER", text: "Free Wifi", subText: "Superhosts are experienced, highly rated Hosts." },
];

it("renders all highlights correctly", () => {
render(<ProductHighlight highlights={mockHighlights} />);

// Assert that all text content appears
mockHighlights.forEach((highlight) => {
expect(screen.getByText(highlight.text)).toBeInTheDocument();

Check failure on line 19 in src/components/ProductHighlight/ProductHighlight.test.jsx

View workflow job for this annotation

GitHub Actions / build (20.x)

src/components/ProductHighlight/ProductHighlight.test.jsx > ProductHighlight Component > renders all highlights correctly

Error: Invalid Chai property: toBeInTheDocument ❯ src/components/ProductHighlight/ProductHighlight.test.jsx:19:46 ❯ src/components/ProductHighlight/ProductHighlight.test.jsx:18:20
expect(screen.getByText(highlight.subText)).toBeInTheDocument();
});

// Check the icons are rendered (using the `presentation` role for SVGs)
const icons = screen.getAllByRole("presentation", { hidden: true });
expect(icons).toHaveLength(mockHighlights.length);
});
it("renders no highlights when the list is empty", () => {
render(<ProductHighlight highlights={[]} />);

const listItems = screen.queryAllByRole("listitem");
expect(listItems).toHaveLength(0); // No highlights should be rendered
});

});
Loading