vendredi 30 décembre 2016

How to render react components that have refs in a testing environment?

I am testing a React component.

My React component has a ref - http://ift.tt/2ePLKpP - that is necessary for some functionality.

My testing code has a line like

    const document = ReactTestUtils.renderIntoDocument(<MyReactComponent></MyReactComponent>);

in a beforeEach function.

Normally, the tests fail with

         addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's `render` method, or you have multiple copies of React loaded .

If I comment out that line from the beforeEach, the tests will fail because document is null - which suggests that it was that line that created the addComponentAsRefTo problem.

If I remove the ref from the element, then the specs will run and most will pass. The failing specs are related to functionality that depends on the ref. This suggests that the problem is related to refs.

Together, I am fairly confident that ReactTestUtils.renderIntoDocument does not work properly with elements that have refs in them.

This was mentioned in http://ift.tt/2hzIUsM

Aucun commentaire:

Enregistrer un commentaire