import { createRef } from "react";
import { render, screen } from "@testing-library/react";
import { StickyOnMobile } from ".";
const CLASS_NAME = "mobius-sticky-on-mobile";
describe("StickyOnMobile", () => {
it("renders children", () => {
render(Sample text);
expect(screen.getByText("Sample text")).toBeInTheDocument();
});
it("renders a div by default", () => {
render(x);
expect(screen.getByTestId("bar").tagName).toBe("DIV");
});
it("uses the provided elementType", () => {
render(
x
,
);
expect(screen.getByTestId("bar").tagName).toBe("FOOTER");
});
it("does not pass elementType through as a DOM attribute", () => {
render(
x
,
);
expect(screen.getByTestId("bar")).not.toHaveAttribute(
"elementtype",
"footer",
);
});
it("forwards style attributes", () => {
render(
x
,
);
expect(screen.getByTestId("bar")).toHaveStyle({
"background-color": "rgb(255, 0, 0)",
});
});
it("spreads HTML attributes onto the element", () => {
render(
x
,
);
expect(screen.getByTestId("bar")).toHaveAttribute(
"aria-label",
"payment actions",
);
});
describe("class names", () => {
it("applies the mobius base class", () => {
render(x);
expect(screen.getByTestId("bar")).toHaveClass("mobius");
});
it("applies the mobius-sticky-on-mobile class", () => {
render(x);
expect(screen.getByTestId("bar")).toHaveClass(CLASS_NAME);
});
it("merges a custom className", () => {
render(
x
,
);
expect(screen.getByTestId("bar")).toHaveClass("my-class");
expect(screen.getByTestId("bar")).toHaveClass(CLASS_NAME);
});
});
it("forwards ref", () => {
const ref = createRef();
render(x);
expect(ref.current).toBeInstanceOf(HTMLDivElement);
});
describe("breakpoint behaviour", () => {
// Behaviour at the 768px breakpoint is driven by a static @media query in
// StickyOnMobile.css. JSDOM does not evaluate @media rules, so these tests
// verify the *contract*: the class hook is applied unconditionally so the
// CSS can switch positioning based on viewport width.
it("applies the sticky class hook regardless of viewport (CSS handles the switch)", () => {
render(x);
expect(screen.getByTestId("bar")).toHaveClass(CLASS_NAME);
});
});
});