import React from "react";
import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import Flex from "./flex";
describe("Flex", () => {
describe("Rendering", () => {
it("should render flex container with children", () => {
render(
Item 1
Item 2
);
const flex = screen.getByTestId("flex");
expect(flex).toBeInTheDocument();
expect(flex).toHaveClass("flex");
});
it("should render as div by default", () => {
render(Content);
const flex = screen.getByTestId("flex");
expect(flex.tagName).toBe("DIV");
});
it("should render as custom element with 'as' prop", () => {
render(
Content
);
const flex = screen.getByTestId("flex");
expect(flex.tagName).toBe("SECTION");
});
it("should apply inline flex class when inline prop is true", () => {
render(
Content
);
const flex = screen.getByTestId("flex");
expect(flex).toHaveClass("flex-inline");
expect(flex).not.toHaveClass("flex");
});
it("should merge custom className with generated classes", () => {
render(
Content
);
const flex = screen.getByTestId("flex");
expect(flex).toHaveClass("flex", "flex-row", "custom-class");
});
it("should apply custom styles", () => {
render(
Content
);
const flex = screen.getByTestId("flex");
expect(flex).toHaveStyle({ "--flex-gap": "2rem" });
});
});
describe("Direction Props", () => {
it("should apply flex-row class for row direction", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("flex-row");
});
it("should apply flex-col class for column direction", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("flex-col");
});
it("should apply flex-row-reverse class for row-reverse direction", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("flex-row-reverse");
});
it("should apply flex-col-reverse class for column-reverse direction", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("flex-col-reverse");
});
});
describe("Wrap Props", () => {
it("should apply flex-wrap class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("flex-wrap");
});
it("should apply flex-nowrap class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("flex-nowrap");
});
it("should apply flex-wrap-reverse class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("flex-wrap-reverse");
});
});
describe("Gap Props", () => {
it("should apply gap-md class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("gap-md");
});
it("should apply row-gap class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("row-gap-lg");
});
it("should apply col-gap class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("col-gap-sm");
});
});
describe("Justify Props", () => {
it("should apply justify-start class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("justify-start");
});
it("should apply justify-center class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("justify-center");
});
it("should apply justify-between class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("justify-between");
});
it("should apply justify-around class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("justify-around");
});
it("should apply justify-evenly class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("justify-evenly");
});
});
describe("Align Props", () => {
it("should apply items-start class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("items-start");
});
it("should apply items-center class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("items-center");
});
it("should apply items-stretch class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("items-stretch");
});
it("should apply items-baseline class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("items-baseline");
});
});
describe("Align Content Props", () => {
it("should apply content-start class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("content-start");
});
it("should apply content-between class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("content-between");
});
it("should apply content-stretch class", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("content-stretch");
});
});
describe("Preset Variants", () => {
it("should apply flex-center class for center variant", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("flex-center");
});
it("should apply flex-between class for between variant", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("flex-between");
});
it("should apply flex-around class for around variant", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("flex-around");
});
it("should apply flex-stack class for stack variant", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("flex-stack");
});
it("should apply flex-spread class for spread variant", () => {
render(
Content
);
expect(screen.getByTestId("flex")).toHaveClass("flex-spread");
});
});
describe("Responsive Props", () => {
it("should apply sm responsive classes", () => {
render(
Content
);
const flex = screen.getByTestId("flex");
expect(flex).toHaveClass("sm:flex-row", "sm:gap-md");
});
it("should apply md responsive classes", () => {
render(
Content
);
const flex = screen.getByTestId("flex");
expect(flex).toHaveClass("md:justify-between", "md:items-center");
});
it("should apply lg responsive classes", () => {
render(
Content
);
const flex = screen.getByTestId("flex");
expect(flex).toHaveClass("lg:flex-col", "lg:flex-wrap");
});
it("should apply xl responsive classes", () => {
render(
Content
);
const flex = screen.getByTestId("flex");
expect(flex).toHaveClass("xl:gap-xl", "xl:justify-evenly");
});
it("should combine base and responsive classes", () => {
render(
Content
);
const flex = screen.getByTestId("flex");
expect(flex).toHaveClass(
"flex",
"flex-col",
"gap-sm",
"md:flex-row",
"md:gap-lg"
);
});
});
describe("Flex.Item", () => {
it("should render Flex.Item component", () => {
render(Item content);
const item = screen.getByTestId("flex-item");
expect(item).toBeInTheDocument();
expect(item.tagName).toBe("DIV");
});
it("should apply flex-1 class", () => {
render(
Item
);
expect(screen.getByTestId("flex-item")).toHaveClass("flex-1");
});
it("should apply flex-auto class", () => {
render(
Item
);
expect(screen.getByTestId("flex-item")).toHaveClass("flex-auto");
});
it("should apply flex-none class", () => {
render(
Item
);
expect(screen.getByTestId("flex-item")).toHaveClass("flex-none");
});
it("should apply flex-initial class", () => {
render(
Item
);
expect(screen.getByTestId("flex-item")).toHaveClass("flex-initial");
});
it("should apply flex-grow class", () => {
render(
Item
);
expect(screen.getByTestId("flex-item")).toHaveClass("flex-grow-1");
});
it("should apply flex-shrink class", () => {
render(
Item
);
expect(screen.getByTestId("flex-item")).toHaveClass("flex-shrink-0");
});
it("should apply flex-basis class", () => {
render(
Item
);
expect(screen.getByTestId("flex-item")).toHaveClass("flex-basis-auto");
});
it("should apply align-self class", () => {
render(
Item
);
expect(screen.getByTestId("flex-item")).toHaveClass("self-end");
});
it("should apply order class", () => {
render(
Item
);
expect(screen.getByTestId("flex-item")).toHaveClass("order-first");
});
it("should apply responsive flex classes", () => {
render(
Item
);
const item = screen.getByTestId("flex-item");
expect(item).toHaveClass("flex-none", "md:flex-1", "lg:flex-auto");
});
it("should render as custom element", () => {
render(
Item
);
const item = screen.getByTestId("flex-item");
expect(item.tagName).toBe("ARTICLE");
});
});
describe("Flex.Spacer", () => {
it("should render Flex.Spacer component", () => {
render();
const spacer = screen.getByTestId("flex-spacer");
expect(spacer).toBeInTheDocument();
expect(spacer.tagName).toBe("DIV");
});
it("should apply flex-1 class by default", () => {
render();
expect(screen.getByTestId("flex-spacer")).toHaveClass("flex-1");
});
it("should render as custom element", () => {
render();
const spacer = screen.getByTestId("flex-spacer");
expect(spacer.tagName).toBe("SECTION");
});
it("should merge custom className", () => {
render();
const spacer = screen.getByTestId("flex-spacer");
expect(spacer).toHaveClass("flex-1", "custom-spacer");
});
});
describe("Composition", () => {
it("should render Flex with Flex.Item children", () => {
render(
Item 1
Item 2
);
expect(screen.getByTestId("flex")).toBeInTheDocument();
expect(screen.getByTestId("item-1")).toBeInTheDocument();
expect(screen.getByTestId("item-2")).toBeInTheDocument();
});
it("should render Flex with Flex.Spacer", () => {
render(
Left
Right
);
expect(screen.getByTestId("flex")).toBeInTheDocument();
expect(screen.getByTestId("spacer")).toBeInTheDocument();
expect(screen.getByTestId("left")).toBeInTheDocument();
expect(screen.getByTestId("right")).toBeInTheDocument();
});
it("should support nested Flex containers", () => {
render(
Nested content
);
expect(screen.getByTestId("outer")).toBeInTheDocument();
expect(screen.getByTestId("inner")).toBeInTheDocument();
});
});
describe("Accessibility", () => {
it("should forward ARIA attributes", () => {
render(
Content
);
const flex = screen.getByTestId("flex");
expect(flex).toHaveAttribute("aria-label", "Main navigation");
expect(flex).toHaveAttribute("role", "navigation");
});
it("should forward data attributes", () => {
render(
Content
);
const flex = screen.getByTestId("flex");
expect(flex).toHaveAttribute("data-custom", "value");
});
it("should support ref forwarding", () => {
const ref = React.createRef();
render(
Content
);
expect(ref.current).toBe(screen.getByTestId("flex"));
});
it("should support Flex.Item ref forwarding", () => {
const ref = React.createRef();
render(
Item
);
expect(ref.current).toBe(screen.getByTestId("flex-item"));
});
it("should support Flex.Spacer ref forwarding", () => {
const ref = React.createRef();
render();
expect(ref.current).toBe(screen.getByTestId("flex-spacer"));
});
});
});