import * as React from "react";
import { render, screen } from "@sproutsocial/seeds-react-testing-library";
import styled from "styled-components";
import { VisuallyHidden } from "../VisuallyHidden";
describe("VisuallyHidden", () => {
it("renders without crashing", () => {
render(Hidden Content);
const element = screen.getByText("Hidden Content");
expect(element).toBeInTheDocument();
expect(element.tagName).toBe("SPAN");
});
it("retains visually hidden styles on the styled fallback", () => {
render(Hidden Content);
const element = screen.getByText("Hidden Content");
expect(element).toHaveStyle(`
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
border: 0;
`);
});
it("forwards additional props", () => {
render(Hidden Content);
const element = screen.getByText("Hidden Content");
expect(element).toHaveAttribute("aria-label", "hidden");
});
it("renders a div with role status through the as prop", () => {
render(
Status
);
const element = screen.getByRole("status");
expect(element.tagName).toBe("DIV");
expect(element).toHaveAttribute("data-state", "ready");
expect(element).not.toHaveAttribute("as");
});
it("forwards className, id, aria, and events", () => {
const onClick = jest.fn();
render(
Hidden Content
);
const element = screen.getByText("Hidden Content");
expect(element).toHaveClass("consumer-class");
expect(element).toHaveAttribute("id", "hidden-id");
expect(element).toHaveAttribute("aria-label", "hidden label");
element.click();
expect(onClick).toHaveBeenCalledTimes(1);
});
it("accepts but does not leak the intentionally loose invalid prop", () => {
const error = jest.spyOn(console, "error").mockImplementation(() => {});
render(
Hidden Content
);
expect(screen.getByText("Hidden Content")).not.toHaveAttribute(
"invalidProp"
);
expect(error).not.toHaveBeenCalled();
error.mockRestore();
});
it("preserves the current non-forwarded ref behavior", () => {
const ref = React.createRef();
const originalError = console.error;
const expectedWarning = "Function components cannot be given refs";
const error = jest
.spyOn(console, "error")
.mockImplementation((...args: Parameters) => {
if (typeof args[0] === "string" && args[0].includes(expectedWarning)) {
return;
}
originalError(...args);
});
render(Hidden Content);
expect(ref.current).toBeNull();
expect(
error.mock.calls.filter(
([message]) =>
typeof message === "string" && message.includes(expectedWarning)
)
).toHaveLength(1);
expect(
error.mock.calls.filter(
([message]) =>
typeof message !== "string" || !message.includes(expectedWarning)
)
).toHaveLength(0);
error.mockRestore();
});
it("passes arbitrary props and className to a custom component through as", () => {
const CustomComponent = ({
arbitraryProp,
className,
children,
}: {
arbitraryProp?: string;
className?: string;
children?: React.ReactNode;
}) => (
{children}
);
render(
Hidden Content
);
const element = screen.getByText("Hidden Content");
expect(element).toHaveAttribute("data-arbitrary-prop", "preserved");
expect(element).toHaveClass("consumer-class");
});
it("uses the Tailwind path by default", () => {
render(Hidden Content);
expect(screen.getByText("Hidden Content")).toHaveClass(
"seeds-visually-hidden"
);
});
it("merges a plain consumer className on the Tailwind path", () => {
render(Hidden Content);
expect(screen.getByText("Hidden Content")).toHaveClass(
"seeds-visually-hidden",
"static"
);
});
it("routes styled-system props to the styled-components fallback", () => {
render(Hidden Content);
expect(screen.getByText("Hidden Content")).not.toHaveClass(
"seeds-visually-hidden"
);
});
it("routes a styled extension to the styled-components fallback", () => {
const Extended = styled(VisuallyHidden)`
opacity: 0.5;
`;
render(Hidden Content);
const element = screen.getByText("Hidden Content");
expect(element).not.toHaveClass("seeds-visually-hidden");
expect(element).toHaveStyleRule("opacity", "0.5");
});
});