import { render } from "@testing-library/react"; import React from "react"; import { componentDisplayName } from "../utils/componentDisplayName"; import { withForEach } from "./withForEach"; interface ExampleProps { a: number; b: number; i: number; } function Example(props: ExampleProps): JSX.Element { // @ts-expect-error it's okay // eslint-disable-next-line react/prop-types const { children, ...rest } = props; return (
      {"\n"}
      {JSON.stringify(rest)}
      {"\n"}
      {children ? <>children: {children} : ""}
      {!!children && "\n"}
    
); } it("withForEach name", () => { const Component = withForEach(10)(Example); expect(componentDisplayName.get(Component)).toBe("withForEach.10(Example)"); }); it("withForEach number", () => { const Component = withForEach(2)(Example); render(); expect(document.body.children[0].innerHTML).toMatchInlineSnapshot(` "
{"a":1,"b":2,"i":0}
{"a":1,"b":2,"i":1}
" `); }); it("withForEach array of string", () => { const Component = withForEach(["c", "d"])(Example); render(); expect(document.body.children[0].innerHTML).toMatchInlineSnapshot(` "
{"a":1,"b":2,"i":0}
children: c
{"a":1,"b":2,"i":1}
children: d
" `); }); it("withForEach array of objects", () => { const Component = withForEach([{ c: 3 }, { d: 4 }])(Example); render(); expect(document.body.children[0].innerHTML).toMatchInlineSnapshot(` "
{"c":3,"a":1,"b":2,"i":0}
{"d":4,"a":1,"b":2,"i":1}
" `); }); it("withForEach array of elements", () => { const Component = withForEach([
1
,
2
, ])(Example); render(); expect(document.body.children[0].innerHTML).toMatchInlineSnapshot(` "
{"a":1,"b":2,"i":0}
children: 
1
{"a":1,"b":2,"i":1}
children: 
2
" `); }); it("withForEach object", () => { const Component = withForEach({ c: 3 })(Example); render(); expect(document.body.children[0].innerHTML).toMatchInlineSnapshot(` "
{"a":1,"b":2,"i":"c"}
children: 3
" `); });