You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TestUtils.renderIntoDocument doesn't function as expected in all browsers #3789
varInput=React.createClass({render: function(){return(<input{...this.props}/>);}});varTestUtils=React.addons.TestUtils;varinputInstance=TestUtils.renderIntoDocument(<Inputvalue='abc'/>);varinputDOMNode=React.findDOMNode(inputInstance);inputDOMNode.focus();// Place cursor at the end.inputDOMNode.setSelectionRange(3,3);// Should log 3 twice.console.log(inputDOMNode.selectionStart);console.log(inputDOMNode.selectionEnd);
Problem: 0 is logged twice in Chrome and Firefox. What I expected: 3 is logged twice. We see this in Safari.
The interesting part is if we use React.render(..., document.body) instead of TestUtils.renderIntoDocument the program works as expected.
setSelectionRange doesn't seem to work if the element isn't on the page. That's why rendering into document.body works as expected. I suppose it's a fluke the program ran correctly in Safari... Though that requirement isn't documented anywhere (?).
Appending to the document was commented out in this commit: ce95c3d. I guess the bigger issue is this method is a misnomer, as it doesn't actually render into the document. It certainly fooled me.
@spicyj@zpao does this change our thoughts on renderIntoDocument? Maybe we should attach to the dom to avoid cases like this? What cleanup were we trying to avoid when we removed that logic? Might the better solution be to improve test isolation?
This will help avoid some cleanup effort between tests in the open-source version of React (see calls to removeNextSiblings in react/src/test/index.html).
This is definitely a problem for accessibility testing. We need components to be attached to test them fully, as the current method makes the tree appear to be hidden. It would be really helpful to have blessed methods for doing this, as appending to the body and then removing messes up my other tests with "The DOM was unexpectedly mutated" errors.
Reacted by Solomon Hawk, Nate Hunzaker and Trevor Pierce
@marcysutton Thanks, that's useful feedback. If you do need to append to the document then the most straightforward and flexible solution is to call ReactDOM.render manually:
var div = document.createElement('div');
document.body.appendChild(div);
ReactDOM.render(<Foo />, div);
That's also exactly what the implementation of any test helper we might provide would do.
@sophiebits Thank you for this suggestion to use ReactDOM.render(). I had an issue similar to the one @marcysutton asked about. I was using a lot of vanilla JS traversal methods that returned null values when I used ReactTestUtils.renderIntoDocument(). Test lights are all green now.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!
Activity
ezequiel commentedon Apr 30, 2015
I get it now.
setSelectionRangedoesn't seem to work if the element isn't on the page. That's why rendering intodocument.bodyworks as expected. I suppose it's a fluke the program ran correctly in Safari... Though that requirement isn't documented anywhere (?).Appending to the document was commented out in this commit: ce95c3d. I guess the bigger issue is this method is a misnomer, as it doesn't actually render into the document. It certainly fooled me.
jimfb commentedon May 1, 2015
@spicyj @zpao does this change our thoughts on renderIntoDocument? Maybe we should attach to the dom to avoid cases like this? What cleanup were we trying to avoid when we removed that logic? Might the better solution be to improve test isolation?
sophiebits commentedon May 1, 2015
I don't feel strongly.
ezequiel commentedon May 2, 2015
I suppose we have two options:
TestUtils.renderIntoDocument.TestUtils.render(does anyone have a better name?) which is simply a renamed version ofrenderIntoDocument.or,
TestUtils.renderIntoDocumentto actually render into the document.Option 1 seems like the best option at this point, as
renderIntoDocument's current behavior hasn't received any complaints (besides mine) thus far.Mock out input in button tests
rupurt commentedon Nov 6, 2015
@ezequiel how do you feel about the following
ReactTestUtils.renderIntoDocumentReactTestUtils.renderwhich uses the functionality fromReactTestUtils.renderIntoDocumentReactTestUtils.renderInto(component, element)- which actually renders into the DOM as a child ofelementReactTestUtils.cleanup(component, element)- which cleans up the given child component fromelementWe're running into problems when trying to test the dimensions of a component i.e.
element.offsetWidth,element.offsetHeightrupurt commentedon Nov 6, 2015
We could even make
renderIntotake a method that automatically does the cleanup for us e.g.marcysutton commentedon Aug 16, 2016
This is definitely a problem for accessibility testing. We need components to be attached to test them fully, as the current method makes the tree appear to be hidden. It would be really helpful to have blessed methods for doing this, as appending to the body and then removing messes up my other tests with "The DOM was unexpectedly mutated" errors.
sophiebits commentedon Aug 17, 2016
@marcysutton Thanks, that's useful feedback. If you do need to append to the document then the most straightforward and flexible solution is to call ReactDOM.render manually:
That's also exactly what the implementation of any test helper we might provide would do.
1Copenut commentedon Apr 12, 2018
@sophiebits Thank you for this suggestion to use
ReactDOM.render(). I had an issue similar to the one @marcysutton asked about. I was using a lot of vanilla JS traversal methods that returned null values when I usedReactTestUtils.renderIntoDocument(). Test lights are all green now.stale commentedon Jan 10, 2020
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.
stale commentedon Jan 19, 2020
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!