Skip to content
This repository has been archived by the owner on Jul 8, 2021. It is now read-only.

how to find children when they pass through another component? #11

Open
tdfairbrother opened this issue Jun 3, 2018 · 4 comments
Open

Comments

@tdfairbrother
Copy link

tdfairbrother commented Jun 3, 2018

I have found a difference between the JS and Reason versions of enzyme testing. In the following JS example, I can find ANY of the children of a shallowly rendered component. In the second example, it doesn't matter that I have the headers inside of an intermediate container .

describe('shallow find test', () => {
  it('renders 2 headers', () => {
    const wrapper = shallow(
      <div>
        <div className="header">
          Test
        </div>
        <div className="header">
          Test2
        </div>
      </div>
    );
    expect(wrapper.find('.header').length).toEqual(2);
  });

  it('renders 2 headers', () => {
    const ThisWontBreakTheFindForChildrenOfThis = ({children}) => (
      <div>{children}</div>
    );
    const wrapper = shallow(
      <ThisWontBreakTheFindForChildrenOfThis>
        <div>
          <div className="header">
            Test
          </div>
          <div className="header">
            Test2
          </div>
        </div>
      </ThisWontBreakTheFindForChildrenOfThis>
    );
    expect(wrapper.find('.header').length).toEqual(2);
  });
});

In the following reason-react test, the second test does not behave the same as the JS version. It does not seem to find the header elements that are rendered through the component.

open Jest;

Enzyme.configureEnzyme(Enzyme.react_16_adapter());

module Container {
  let component = ReasonReact.statelessComponent("Example1");
  let make = (_children) => {
    ...component,
    render: _self =>
      <div>
        <div className="header">
          (ReasonReact.string("Test"))
        </div>
        <div className="header">
          (ReasonReact.string("Test2"))
        </div>
      </div>
  };
};

module ThisWillBreakTheFindForChildrenOfThis {
  let component = ReasonReact.statelessComponent("Example2");
  let make = (children) => {
    ...component,
    render: _self =>
     ReasonReact.createDomElement(
       "div",
       ~props={"className": ""},
       children
     )
  };
};

/* TODO -- this container wont allow me to find the inner divs*/
module ContainerWithWrap {
  let component = ReasonReact.statelessComponent("Example3");
  let make = (_children) => {
    ...component,
    render: _self =>
      <ThisWillBreakTheFindForChildrenOfThis>
        <div>
          <div className="header">
            (ReasonReact.string("Test"))
          </div>
          <div className="header">
            (ReasonReact.string("Test2"))
          </div>
        </div>
      </ThisWillBreakTheFindForChildrenOfThis>
  };
};

let setup = () =>
  Enzyme.shallow(<Container />);

let setup2 = () =>
  Enzyme.shallow(<ContainerWithWrap />);

let header = (wrapper) =>
  wrapper
    |> Enzyme.find(".header");

describe("shallow find test", () => {
  open Expect;

  test("renders 2 headers", () => {
    let wrapper = setup(());
    let headerNodes = wrapper |> header;
    expect(Enzyme.length(headerNodes)) |> toBe(2)
  });

  test("DOESNT render 2 headers", () => {
    let wrapper = setup2(());
    let headerNodes = wrapper |> header;
    expect(Enzyme.length(headerNodes)) |> toBe(0)
  });
});

Is there a way to do what I am trying to do?

@erykpiast
Copy link
Contributor

I'm trying to refresh this repository. @tdfairbrother, is your issue still preset? Did you find any workaround for it in the meantime?

@tdfairbrother
Copy link
Author

No, I ended up using a different library.

@erykpiast
Copy link
Contributor

Oh, I see. Which one? You mean different ReasonML bindings or a different testing library?

@tdfairbrother
Copy link
Author

tdfairbrother commented Mar 3, 2020 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants