import { expect } from 'chai'; import sinon from 'sinon'; import React from 'react'; import ReactDOM from 'react-dom'; import TestUtils from 'react-addons-test-utils'; import MyComponent from './MyComponent'; /* * To be used when testing a component that is _NOT_ pure (uses this.state) OR when testing lifecycle hooks * (eg. componentDidUpdate). This is accomplished by re-rendering the the component manually and then asserting * expectations. */ describe('MyComponent', () => { const getComponent = (props = {}) => { props = Object.assign({ onChange: sinon.spy(), }, props); const node = document.createElement('div'); // Notice the different rendering method here const component = ReactDOM.render( ), node); return Object.assign(props, { component, node }); }; context('when clicked', () => { it('adds some-class', () => { const props = getComponent(); // Do some action that changes internal state TestUtils.Simulate.click(props.component); // Re-render (you can also change props here) ReactDOM.render(, props.node); expect(component.className).includes('some-class'); }); }); });