react-shallow-testutils icon indicating copy to clipboard operation
react-shallow-testutils copied to clipboard

findAllWithType is not functioning correctly

Open Will-Sommers opened this issue 10 years ago • 4 comments
trafficstars

Firstly, are you accepting PRs on this project?

Second, consider the following code:

var Test = React.createClass({
  render: function() {
    return <div><TestTwo /></div>
  } 
});

var TestTwo = React.createClass({
  render: function() {
    return <div> 'hi'</div>
  }
})
renderer = new ShallowRenderer.Renderer;
component = renderer.render(Test, {}, {});
ShallowRenderer.findAllWithType(component, 'TestTwo')

Should this return an array with one element representing the TestTwo component? It is currently returning an empty array. I'm trying to verify that I am using this correctly.

It looks as if .type passed to findAll is not returning the correct value.

Will-Sommers avatar Aug 12 '15 03:08 Will-Sommers

Yeah, of course I'll accept PRs.

This should work:

ShallowRenderer.findAllWithType(component, TestTwo)

There's not much documentation on the shallow rendering stuff but in the rendered tree it gives you back each element has a type property. If the element was a composite component like your TestTwo, the type is the Function that is returned from React.createClass (or the class if you're using ES2015 classes). If the element was a DOM element then type will be just a String, e.g, 'div' or 'span'.

Given that, I didn't have separate functions for DOM components and composite components like the normal React TestUtils. However, I can if you think it would simplify the meaning?

sheepsteak avatar Aug 12 '15 08:08 sheepsteak

Thank you/ I'll check that out and maybe submit a PR with some tests/examples.

Will-Sommers avatar Aug 12 '15 14:08 Will-Sommers

How about using displayName when type is a Function?

It also seems to me that Function is sometimes named (possibly in ES6 case), so that could also be checked.

rkovacevic avatar Dec 06 '15 10:12 rkovacevic

Be great to see this working. Currently we use a lot of functional components which don't get found using this method.

Rodeoclash avatar May 23 '16 01:05 Rodeoclash