import React from "react"; import { render } from "@testing-library/react"; import { componentDisplayName } from "../utils/componentDisplayName"; import { withClassName } from "./withClassName"; function Example({ className }: { className: string }): JSX.Element { return ( <>
{JSON.stringify({ className })}
>
);
}
it("withClassName name with no args", () => {
const Component = withClassName()(Example);
expect(componentDisplayName.get(Component)).toBe("withClassName(Example)");
});
it("withClassName name with string", () => {
const Component = withClassName("someClassName")(Example);
expect(componentDisplayName.get(Component)).toBe(
"withClassName.someClassName(Example)",
);
});
it("withClassName name with function", () => {
const Component = withClassName(({ x }: { x: boolean }) =>
x ? "someClassName" : "",
)(Example);
expect(componentDisplayName.get(Component)).toBe(
"withClassName.[unnamed function](Example)",
);
});
it("withClassName name with array of strings", () => {
const Component = withClassName(["someClassName1", "someClassName2"])(
Example,
);
expect(componentDisplayName.get(Component)).toBe(
"withClassName.someClassName1.someClassName2(Example)",
);
});
it("withClassName name with array mixed", () => {
const Component = withClassName([
"someClassName1",
({ x }: { x: boolean }): string => (x ? "someClassName" : ""),
])(Example);
expect(componentDisplayName.get(Component)).toBe(
"withClassName.someClassName1.[unnamed function](Example)",
);
});
describe.each([undefined, "someClass", ["someClass1", "someClass2"]])(
"when className={%s}",
(className) => {
it("with no args", () => {
const Component = withClassName()(Example);
render(