import React from "react";
import { BlogCard } from "./index";
import { render, screen } from "@testing-library/react";
// Mock next/link
jest.mock("next/link", () => {
const MockLink = ({ children, href, className, ...rest }: any) => (
{children}
);
MockLink.displayName = "MockNextLink";
return MockLink;
});
jest.mock("@shared/components/text", () => ({
Text: ({ children, className, ...rest }: any) => (
{children}
),
}));
describe("BlogCard", () => {
const defaultProps = {
href: "/blog/test-post",
title: "Test Blog Post",
description: "A description of the post",
date: "Jan 15, 2026",
category: "Technology",
};
describe("Rendering", () => {
it("renders the article element", () => {
const { container } = render();
expect(container.querySelector("article")).toBeInTheDocument();
});
it("renders title as a link", () => {
render();
expect(screen.getByText("Test Blog Post")).toBeInTheDocument();
expect(screen.getByText("Test Blog Post").closest("a")).toHaveAttribute(
"href",
"/blog/test-post"
);
});
it("renders description", () => {
render();
expect(screen.getByText("A description of the post")).toBeInTheDocument();
});
it("renders category", () => {
render();
expect(screen.getByText("Technology")).toBeInTheDocument();
});
it("renders date", () => {
render();
expect(screen.getByText("Jan 15, 2026")).toBeInTheDocument();
});
it("renders dot separator between category and date", () => {
render();
expect(screen.getByText("•")).toBeInTheDocument();
});
it("does not render date separator when date is not provided", () => {
render();
expect(screen.queryByText("•")).not.toBeInTheDocument();
});
});
describe("Image", () => {
it("renders img element when image is provided", () => {
const image = {
src: "/test.jpg",
alt: "Test image",
width: 400,
height: 300,
};
const { container } = render(
);
const img = container.querySelector("img");
expect(img).toBeInTheDocument();
expect(img).toHaveAttribute("src", "/test.jpg");
expect(img).toHaveAttribute("alt", "Test image");
});
it("renders custom imageComponent when provided", () => {
const image = {
src: "/test.jpg",
alt: "Custom image",
width: 400,
height: 300,
};
const CustomImage = (props: any) => (
);
render(
);
expect(screen.getByTestId("custom-image")).toBeInTheDocument();
});
it("renders placeholder when no image is provided", () => {
const { container } = render();
const placeholder = container.querySelector("[aria-hidden='true']");
expect(placeholder).toBeInTheDocument();
});
});
describe("Layout", () => {
it("applies grid layout by default", () => {
const { container } = render();
const article = container.querySelector("article");
expect(article?.className).toContain("flex h-full flex-col");
});
it("applies non-grid layout when asGrid is false", () => {
const { container } = render(
);
const article = container.querySelector("article");
expect(article?.className).toContain("callout-card");
});
it("applies h-full in non-grid mode for equal height cards", () => {
const { container } = render(
);
const article = container.querySelector("article");
expect(article?.className).toContain("h-full");
expect(article?.className).toContain("flex-col");
});
it("applies data-section-type attribute", () => {
const { container } = render();
const article = container.querySelector("article");
expect(article).toHaveAttribute("data-section-type", "blog-card");
});
it("applies data-section-index attribute", () => {
const { container } = render();
const article = container.querySelector("article");
expect(article).toHaveAttribute("data-section-index", "2");
});
});
describe("Navigation", () => {
it("renders a link to href when title is missing", () => {
const { container } = render(
);
const links = container.querySelectorAll("a");
expect(links).toHaveLength(1);
expect(links[0]).toHaveAttribute("href", "/blog/test-post");
});
it("renders exactly one link when title is present", () => {
const { container } = render();
const links = container.querySelectorAll("a");
expect(links).toHaveLength(1);
expect(links[0]).toHaveAttribute("href", "/blog/test-post");
});
});
describe("Optional props", () => {
it("renders without title", () => {
const { container } = render(
);
expect(container.querySelector("article")).toBeInTheDocument();
});
it("renders without description", () => {
const { container } = render(
);
expect(container.querySelector("article")).toBeInTheDocument();
});
});
});