Description
Do you want to request a feature or report a bug?
I would like to request a new feature.
What is the current behavior?
At the moment there is no way to create an instance of a ReactTestInstance
. I would love to be able to do something like:
const div = document.createElement('div');
const componentRef = ReactDOM.render(<MyComponentUnderTest />, div); // or renderIntoDocument
const testInstance = new ReactTestInstance(componentRef);
// now I can run queries using the API of ReactTestInstance
// against a component tree that has been fully DOM rendered
testInstance.findAll(..)
testInstance.children.forEach(() => {});
testInstance.parent
Why?
Libraries such as Enzyme can be used to test React components by full DOM rendering and providing an API to find components and get information about them.
As far as I know, there is no way of querying the component tree created by ReactDOM.render
without relying on the internals of React nodes. This means that Enzyme, in order to support full DOM rendering and it's querying API, it has to interact with React nodes directly. This reliance causes problems in Enzyme whenever React adds a new node type (forwardRef, ContextProvider/Consumer for example).
I have started this RFC that proposes that Enzyme uses ReactTestWrapper
from react-test-renderer
as a layer on top of React node objects. This allows the library to be decoupled from the internals of React. The solution proposed in the RFC relies on the being able to create a ReactTestInstance
from a ReactDOM.render
component tree.
Please let me know if this is something you would consider 😄