To TestUtil or not to TestUtil
In unit and functional tests for React, the Yahoo Mail team uses the React test utilities. The test utilities have some very helpful methods to simulate events, find rendered components, and isolate tests from each other.
I refactored the List tests to fully use these utilities. One of the lines I changed looked like this:
React.render(<List />, document.body);
which I changed to use the React test utilities:
ReactTestUtils.renderIntoDocument(<List />);
Generally, when testing React components, we use ReactTestUtils.renderIntoDocument() because it protects against test contamination by creating a detached div to put our components into. Since the div is detached from the body, we’re never in danger of one test getting another test’s markup.
However, for List, the test failed when I used renderIntoDocument. Our List has a dependency on the InfiniteScroll component. Inside the InfiniteScroll component, we use offsetHeight to determine the number of conversations present on the page. The InfiniteScroll component is never attached to the DOM because renderIntoDocument creates a detached DOM node. Because the node is detached, offsetHeight is not computed. As a result, the InfiniteScroll component thinks there is nothing to render, when in reality, there is. So, our test fails.
Since we can’t use renderIntoDocument in our List tests, we need another way to prevent test contamination. If we render directly into document.body, we create a mess that must be cleaned up after every test case. If we don’t clean up, we compromise the integrity of the next test case by leaving old elements in the DOM.
To avoid the risk, we render the List in an iframe.
beforeEach(function() {
this.iframe = document.createElement(
Yahoo Engineering