question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TestUtils.renderIntoDocument doesn't function as expected in all browsers

See original GitHub issue

Hi,

Here is my program (jsfiddle):

var Input = React.createClass({
    render: function() {
        return (
            <input 
                {...this.props}
            />
        );
    }
});

var TestUtils = React.addons.TestUtils;
var inputInstance = 
        TestUtils.renderIntoDocument(
            <Input
                value='abc'
            />
        );

var inputDOMNode = 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.

I am using:

  • Chrome 42.0.2311.135
  • Firefox 37.0.2
  • Safari 8.0.5 (10600.5.17)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
marcysuttoncommented, 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.

2reactions
sophiebitscommented, 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:

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test with reactjs renderIntoDocument keep failed due to ...
tl;dr; You need jsdom + Mocha . According to spec ReactTestUtils#renderIntoDocument ,. renderIntoDocument() requires a DOM: You will need to have window, ...
Read more >
Test Utilities - React
renderIntoDocument ()​​ Render a React element into a detached DOM node in the document. This function requires a DOM.
Read more >
Day 2: Testing React Applications - 12 Devs of Xmas
I've written the entire application in ES2015 using Babel. I've installed all our dependencies through npm as Node modules and webpack converts ...
Read more >
unexpected-react
Checking a simple render. var todoList = TestUtils.renderIntoDocument( ; Triggering an event on a button inside a subcomponent (using the eventTarget prop to ......
Read more >
Testing and Performance - Piazza
SWE 432, Fall 2016. Design and Implementation of Software for the Web ... For interaction with DOM/browser. ... Only write code if it's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found