import React from "react";
import { SelectPlanButton } from "./index";
import { fireEvent, render, screen } from "@testing-library/react";
describe("SelectPlanButton", () => {
const defaultProps = {
onSelect: jest.fn(),
speed: "500",
};
beforeEach(() => {
jest.clearAllMocks();
});
it("renders with default props", () => {
render();
expect(screen.getByText("Select plan")).toBeInTheDocument();
});
it("calls onSelect when clicked", () => {
render();
fireEvent.click(screen.getByRole("button"));
expect(defaultProps.onSelect).toHaveBeenCalledTimes(1);
});
it("applies selected styles when isSelected is true", () => {
render();
const button = screen.getByRole("button");
expect(button.className).toContain("bg-bg-surface");
expect(button.className).toContain("text-text");
});
it("applies default (unselected) styles when isSelected is false", () => {
render();
const button = screen.getByRole("button");
expect(button.className).toContain("bg-bg-fill-brand");
expect(button.className).toContain("text-text-brand-on-bg-fill");
});
it("renders the expand_circle_right icon", () => {
render();
expect(screen.getByText("expand_circle_right")).toBeInTheDocument();
});
it("applies icon className for unselected state", () => {
render(
);
const icon = screen.getByText("expand_circle_right");
expect(icon.className).toContain("text-icon-inverse");
expect(icon.className).toContain("custom-icon");
});
it("applies icon className for selected state", () => {
render(
);
const icon = screen.getByText("expand_circle_right");
expect(icon.className).toContain("text-icon");
expect(icon.className).toContain("custom-icon");
});
it("sets data-track attributes on the button", () => {
render();
const button = screen.getByRole("button");
expect(button).toHaveAttribute(
"data-track-element-name",
"speed_plan_select_button"
);
expect(button).toHaveAttribute(
"data-track-click-text",
"Select plan speed 1000"
);
expect(button).toHaveAttribute(
"data-track-element-clicked",
"speed_plan_card"
);
});
it("calls renderCheckPlans with correct arguments", () => {
const renderCheckPlans = jest.fn(() =>
);
render(
);
expect(renderCheckPlans).toHaveBeenCalledWith({
speedCardConfig: expect.objectContaining({
speed: "300",
techType: "fiber",
isMax: true,
isModalOpen: false,
}),
cta: { label: "Go", url: "/go" },
});
expect(screen.getByTestId("check-plans")).toBeInTheDocument();
});
it("sets isModalOpen to true after click and passes setModalOpen", () => {
let capturedConfig: any;
const renderCheckPlans = jest.fn(args => {
capturedConfig = args;
return null;
});
const { rerender } = render(
);
// Initially modal is closed
expect(renderCheckPlans).toHaveBeenLastCalledWith(
expect.objectContaining({
speedCardConfig: expect.objectContaining({ isModalOpen: false }),
})
);
// Click opens modal
fireEvent.click(screen.getByRole("button"));
// After click, re-render happens with open=true
expect(renderCheckPlans).toHaveBeenLastCalledWith(
expect.objectContaining({
speedCardConfig: expect.objectContaining({ isModalOpen: true }),
})
);
// Call setModalOpen to close
capturedConfig.speedCardConfig.setModalOpen();
// Need rerender to capture new state
rerender(
);
expect(renderCheckPlans).toHaveBeenLastCalledWith(
expect.objectContaining({
speedCardConfig: expect.objectContaining({ isModalOpen: false }),
})
);
});
it("renders without renderCheckPlans (optional)", () => {
render();
expect(screen.getByRole("button")).toBeInTheDocument();
});
it("uses default iconSize of 24", () => {
render();
const icon = screen.getByText("expand_circle_right");
expect(icon.style.fontSize).toBe("24px");
});
it("uses custom iconSize", () => {
render();
const icon = screen.getByText("expand_circle_right");
expect(icon.style.fontSize).toBe("32px");
});
});