mardi 5 avril 2016

React Test Tools: Find all components in tree

I have a component which have something like this:

<Component1>
    <Component2>
        {someArray.map((content, index) => {
            <Component3 prop={content} />
        })}
    </Component2>
</Component1>

In my test I have to check the number of Component3 rendered inside my mocked Component1 for that:

_componentTree = ReactTestUtils.renderIntoDocument(
    <Component1 {..._props} />
)

But then, all functions in React Test Utils Seems to find the first component (Component1) and then all children are inside props.children

The only method that seems to search on all the tree is findAllInRenderedTree. In an example I've found, they do like this:

_rendered = ReactTestUtils.findAllInRenderedTree(
  _componentTree,
    (c) => c.tagName === 'tag'
)

I have tried, but replacing tag by Component3 does not work (I assume because it is not the tag name rendered to the dom?). So my question is, Is there a way to search by component name? Something like:

_rendered = ReactTestUtils.findAllInRenderedTree(
  _componentTree,
    (c) => c.componentName === 'Component3'
)

Aucun commentaire:

Enregistrer un commentaire