Better test facilities for stateless components
See original GitHub issueI originally left a comment in #4936, but I felt this might be better as a separate issue.
I completely agree with the rationale behind no backing instances and no refs for stateless components. I want to use pure stateless components as much as possible.
However, I’m running into instances where testing stateless components gets tricky. For the most part, using the shallow renderer to render a stateless component has been fine. I can then make assertions on expected props and children. To reduce the duplication, I abstract out the shallow renderer process to a helper function.
When, I want to test events, though, I have to take a different route. If my component takes an
onClick
prop, then I wrap it in a class component, so I can access the DOM node to simulate my click event. Again, this isn’t a huge deal because I can abstract out the wrap/render process into a helper function.I bring all this up because I feel that it would be nice to have better facilities in
TestUtils
for stateless components. I don’t think the answer is allowingReactDOM.findDOMNode
to work on stateless components because that opens up it to abuse in normal application code. Are you open to allowing extra functions inTestUtils
to simplify the process of testing stateless components? Or am I creating a code smell in my testing?I’d be happy to create a PR for
TestUtils
if you think some more helper functions for stateless components would be beneficial.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:14 (4 by maintainers)
I actually made a component that wraps a stateless component into a regular component (without any wrapper html to be added, the output should be the same)
https://github.com/ngerritsen/react-stateless-wrapper
With this you can just use the normal testing facilities.
Seems like most people are pretty happy with Enzyme. I think we can close this.
If you have specific API proposals for React itself please create an RFC: https://github.com/reactjs/rfcs