See also Enzyme, a testing library specifically for React, that allows you to easy assert on both deep and shallow renders of the React DOM.<p><a href="https://github.com/airbnb/enzyme#basic-usage" rel="nofollow">https://github.com/airbnb/enzyme#basic-usage</a>
I've found the best testing pattern is to test the interface of the component, not the implementation.<p>This means treating the component as an opaque function with inputs (props) and outputs (the rendered result). Your tests just need to that verify that different prop values result in the DOM changes or callbacks that you expect.
From what Dan Abramov has been posting on Twitter recently, findDOMNode() is in the process of being deprecated, so rather than writing<p><pre><code> const component = ReactDOM.findDOMNode(TestUtils.renderIntoDocument(
<MyComponent {...props} />
));
</code></pre>
we should be writing<p><pre><code> let node;
const component = TestUtils.renderIntoDocument(
<MyComponent {...props} ref={n => node = n}/>
);
</code></pre>
<a href="https://gist.github.com/gaearon/7f0e03d3028016bfabfad641720d3de1" rel="nofollow">https://gist.github.com/gaearon/7f0e03d3028016bfabfad641720d...</a>
I looked through the patterns and I wonder what makes them 'idiomatic'?<p>I've found it useful to use wrapper libraries like Enzyme [0] and Teaspoon [1] when testing React components and interactions.<p>[0] <a href="https://github.com/airbnb/enzyme" rel="nofollow">https://github.com/airbnb/enzyme</a><p>[1] <a href="https://github.com/jquense/teaspoon" rel="nofollow">https://github.com/jquense/teaspoon</a>
Cool post but I'll play devil's advocate and ask what's so idiomatic about this. Can someone comment if this is what most the community is going? Not blaming the author at all, but skeptical of anything called "idiomatic" nowadays.
It can be very helpful to define all the different states (as props) for the set of components you're building. The data can be static or the result of dynamic calls to API endpoints (both approaches have benefits and trade-offs). For instance, if you're building a page with a list of items and at most 10 should be shown at a time, you could define the empty state (0 items), the half-full (1-10 items) and the overloaded state (11+ items, requiring pagination). Then you can implement the actual UI.<p>When this is done (and you're happy with the way it looks), you can make your test suite iterate over each of the different states and capture a screenshot of the UI. When something changes, you will know (the test suite should diff it). This approach is obviously mostly for presentation rather than functionality.<p>The benefits are not limited to testing, this is also very helpful in development, since you could switch between different states very quickly without having to constantly change URLs (for different API results). One method is using a dropdown overlay (only present in the development build). It's also very helpful during code review.
In my experience, as long as you have a well tested data model and use proptypes/Flow/TypeScript, testing the components themselves becomes almost entirely irrelevant.<p>However, I can see the utility of component testing if I were wrapping something like Ace editor or a jQuery plugin or something of that nature.
Do others agree with the `getComponent` pattern described there?<p>I do like it but I'm wary of moving away from the `beforeEach` pattern. The `beforeEach` pattern is much more common and is a consistent interface that engineers can easily pickup and won't need to recreate in every single test file.