import React from "react"; import { render } from "@testing-library/react"; import { CSSProperties } from "react"; import { withHocs } from "../hocs/withHocs"; import { componentDisplayName } from "../utils/componentDisplayName"; import { withStyleObjectStrategy } from "./withStyleObjectStrategy"; function Example(props: { style: CSSProperties }): JSX.Element { return
{JSON.stringify(props)}
; } it("withStyleObjectStrategy name", () => { const Component = withStyleObjectStrategy({ borderColor: "red", })(Example); expect(componentDisplayName.get(Component)).toBe( "withStyleObjectStrategy(Example)", ); }); it("withStyleObjectStrategy name with dependency array", () => { const Component = withStyleObjectStrategy({ borderColor: "red", })(Example); expect(componentDisplayName.get(Component)).toBe( "withStyleObjectStrategy(Example)", ); }); it("withStyleObjectStrategy with value", () => { const Component = withStyleObjectStrategy<{ background?: string }>( ({ background }) => ({ background, }), ["background"], )(Example); render(); expect(componentDisplayName.get(Component)).toBe( "withStyleObjectStrategy.background(Example)", ); }); it("withStyleObjectStrategy with function", () => { const Component = withStyleObjectStrategy<{}>( () => ({ background: "blue" }), [], )(Example); render(); expect(document.body.children[0].innerHTML).toBe( '
{"style":{"background":"blue"}}
', ); }); it("withStyleObjectStrategy a new property in dependencyNames", () => { const Component = withStyleObjectStrategy<{ background: string }>( ({ background }) => ({ background }), ["background"], )(Example); render(); expect(document.body.children[0].innerHTML).toBe( '
{"background":"yellow","style":{"background":"yellow"}}
', ); }); it("withStyleObjectStrategy overridden", () => { const Component = withStyleObjectStrategy({ background: "red" })(Example); render(); expect(document.body.children[0].innerHTML).toBe( '
{"style":{"background":"red","border":"none"}}
', ); }); it("withStyleObjectStrategy rewritten", () => { const Component = withStyleObjectStrategy<{ style: CSSProperties }>( (_props) => ({ width: "100%" }), ["style"], )(Example); render(); expect(document.body.children[0].innerHTML).toBe( '
{"style":{"width":"100%"}}
', ); }); it("withStyleObjectStrategy with style twice", () => { const Component = withHocs([ withStyleObjectStrategy({ background: "black", borderColor: "white", }), withStyleObjectStrategy({ borderColor: "red", display: "block", }), ])(Example); render(); expect(document.body.children[0].innerHTML).toBe( '
{"style":{"borderColor":"white","display":"block","background":"black"}}
', ); });