import { render } from "@testing-library/react"; import React from "react"; import { componentDisplayName } from "../utils/componentDisplayName"; import { withIf } from "./withIf"; interface ExampleProps { a: number; } function Example(props: ExampleProps): JSX.Element { return
{JSON.stringify(props)};
}
it("withIf name with propName", () => {
const Component = withIf("b")(Example);
expect(componentDisplayName.get(Component)).toBe("withIf.b(Example)");
});
it("withIf name with function", () => {
const Component = withIf{"a":1,"enabled":true}',
);
});
it("withIf with function", () => {
const Component = withIf{"a":1}');
});
it("withIf with function with new dependency", () => {
const Component = withIf<{ i: number }>(({ i }) => i > 0, {
dependencyNames: ["i"],
Else: (_props: { b: number }) => <>null>,
})(Example);
const { rerender } = render({"a":1,"i":1}');
});
it("withIf with function and no dependencyNames", () => {
let calls = 0;
const Component = withIf<{}>(
() => {
calls++;
return false;
},
{
dependencyNames: [],
},
)(Example);
const { rerender } = render({JSON.stringify(props)},
})(Example);
const { rerender } = render({"a":0,"condition":false}',
);
rerender({"a":1,"condition":true}',
);
});