import React from "react";
import { TYPOGRAPHY_SIZE_600 } from "@sproutsocial/seeds-typography";
import { render, screen } from "@sproutsocial/seeds-react-testing-library";
import Link from "../Link";
describe("Racine Link", () => {
it("should render in an anchor tag", () => {
render(
Link
);
expect(screen.getByText("Link")).toBeInTheDocument();
});
it("should render disabled", () => {
render(
<>
Regular Link
Disabled Link
>
);
expect(screen.getByText("Regular Link")).toBeEnabled();
expect(screen.getByText("Disabled Link")).toBeDisabled();
});
it("setting external to prop true should add target blank attribute to anchor", () => {
render(
Link
);
expect(screen.getByText("Link").closest("a")?.target).toEqual("_blank");
});
it("setting external to prop true should add rel='noopener' attribute", () => {
render(
Link
);
expect(screen.getByText("Link").closest("a")?.rel).toEqual(
"noopener noreferrer"
);
});
it("setting a onClick instead of href renders component as a Button", () => {
render(
{
// eslint-disable-next-line no-console
console.log("Button");
}}
>
Am I a button?
);
expect(screen.getByText("Am I a button?").tagName).toEqual("BUTTON");
});
it("setting both href and onClick renders component as an Anchor", () => {
render(
{
// eslint-disable-next-line no-console
console.log("Button");
}}
>
Am I a button?
);
expect(screen.getByText("Am I a button?").tagName).toEqual("A");
});
it("Using the 'as' prop works as expected and retains styles from Text and Link", () => {
render(
Link as span
);
expect(screen.getByText("Link as span").tagName).toEqual("SPAN");
expect(screen.getByText("Link as span")).toHaveStyleRule(
"font-size",
TYPOGRAPHY_SIZE_600.fontSize
);
});
it("Has type attribute as button when rendered as a button element", () => {
render(Link);
expect(screen.getByDataQaLabel({ link: "" }).tagName).toEqual("BUTTON");
});
it("Does not have type attribute when rendered as an anchor element", () => {
render(Link);
expect(screen.getByDataQaLabel({ link: "" })).not.toHaveAttribute("type");
expect(screen.getByDataQaLabel({ link: "" }).tagName).not.toEqual("BUTTON");
});
it("Can render with an underline", () => {
// The Tailwind path renders the link with the `seeds-link-underline`
// class, which sets `text-decoration: underline` in link.css.
render(Link);
expect(screen.getByText("Link")).toHaveClass("seeds-link-underline");
});
});
describe("ref forwarding", () => {
it("should forward ref to anchor element when href is provided", () => {
const ref = React.createRef();
render(
Test Link
);
expect(ref.current).toBeInstanceOf(HTMLAnchorElement);
expect(ref.current?.tagName).toBe("A");
expect(ref.current?.getAttribute("href")).toBe("/test");
});
it("should forward ref to button element when no href is provided", () => {
const ref = React.createRef();
render(
{}} ref={ref}>
Test Button
);
expect(ref.current).toBeInstanceOf(HTMLButtonElement);
expect(ref.current?.tagName).toBe("BUTTON");
});
it("should allow ref access to DOM methods", () => {
const ref = React.createRef();
render(
Test Link
);
// Verify we can call DOM methods
expect(typeof ref.current?.focus).toBe("function");
expect(typeof ref.current?.blur).toBe("function");
expect(typeof ref.current?.getBoundingClientRect).toBe("function");
});
it("should work without ref (backward compatibility)", () => {
// This should not throw or break
expect(() => {
render(Test Link);
}).not.toThrow();
});
it("should handle ref with external links", () => {
const ref = React.createRef();
render(
External
);
expect(ref.current).toBeInstanceOf(HTMLAnchorElement);
expect(ref.current?.getAttribute("target")).toBe("_blank");
expect(ref.current?.getAttribute("rel")).toBe("noopener noreferrer");
});
it("should handle ref with disabled state", () => {
const ref = React.createRef();
render(
Disabled
);
expect(ref.current).toBeInstanceOf(HTMLAnchorElement);
expect(ref.current?.getAttribute("aria-disabled")).toBe("true");
});
it("should handle ref with custom 'as' prop", () => {
const ref = React.createRef();
render(
Custom As
);
expect(ref.current).toBeInstanceOf(HTMLAnchorElement);
});
});