import React from "react";
import { MaterialIcon } from "./index";
import { fireEvent, render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";
describe("MaterialIcon", () => {
describe("Rendering", () => {
it("renders the icon name as text content", () => {
render();
expect(screen.getByText("home")).toBeInTheDocument();
});
it("has correct displayName", () => {
expect(MaterialIcon.displayName).toBe("MaterialIcon");
});
it("renders as a span element", () => {
render();
expect(screen.getByText("search").tagName).toBe("SPAN");
});
it("applies material-symbols-rounded class", () => {
render();
expect(screen.getByText("settings").className).toContain(
"material-symbols-rounded"
);
});
});
describe("Font variation settings", () => {
it("sets default font variation settings", () => {
render();
const el = screen.getByText("home");
expect(el.style.fontVariationSettings).toContain("'FILL' 0");
expect(el.style.fontVariationSettings).toContain("'wght' 200");
expect(el.style.fontVariationSettings).toContain("'GRAD' 145");
expect(el.style.fontVariationSettings).toContain("'opsz' 48");
});
it("sets fill when provided", () => {
render();
const el = screen.getByText("star");
expect(el.style.fontVariationSettings).toContain("'FILL' 1");
});
it("sets custom weight", () => {
render();
const el = screen.getByText("star");
expect(el.style.fontVariationSettings).toContain("'wght' 400");
});
it("sets custom optical size", () => {
render();
const el = screen.getByText("star");
expect(el.style.fontVariationSettings).toContain("'opsz' 24");
});
it("sets custom emphasis (GRAD)", () => {
render();
const el = screen.getByText("star");
expect(el.style.fontVariationSettings).toContain("'GRAD' 200");
});
});
describe("Size", () => {
it("applies default font size of 24px", () => {
render();
const el = screen.getByText("home");
expect(el.style.fontSize).toBe("24px");
});
it("applies custom size", () => {
render();
const el = screen.getByText("home");
expect(el.style.fontSize).toBe("48px");
});
});
describe("Color", () => {
it("uses currentColor by default", () => {
render();
const el = screen.getByText("home");
expect(el.style.color).toBe("currentColor");
});
it("applies custom color", () => {
render();
const el = screen.getByText("home");
expect(el.style.color).toBe("red");
});
});
describe("Custom props", () => {
it("applies custom className", () => {
render();
const el = screen.getByText("home");
expect(el.className).toContain("custom-icon");
});
it("applies custom inline style", () => {
render();
const el = screen.getByText("home");
expect(el.style.opacity).toBe("0.5");
});
it("applies data-testid", () => {
render();
expect(screen.getByTestId("my-icon")).toBeInTheDocument();
});
});
describe("Event handling", () => {
it("calls onClick when clicked", () => {
const onClick = jest.fn();
render();
fireEvent.click(screen.getByText("home"));
expect(onClick).toHaveBeenCalledTimes(1);
});
});
describe("Font family", () => {
it("sets Material Symbols Rounded font family", () => {
render();
const el = screen.getByText("home");
expect(el.style.fontFamily).toContain("Material Symbols Rounded");
});
});
});