Mailspring/spec/n1-spec-runner/react-test-utils-extensions.es6
2016-10-17 18:07:35 -07:00

31 lines
1.1 KiB
JavaScript

/* eslint react/no-render-return-value: 0 */
import _ from 'underscore';
import ReactDOM from "react-dom";
import ReactTestUtils from 'react-addons-test-utils';
export function scryRenderedComponentsWithTypeAndProps(root, type, props) {
if (!root) { throw new Error("Must supply a root to scryRenderedComponentsWithTypeAndProps"); }
return _.compact(_.map(ReactTestUtils.scryRenderedComponentsWithType(root, type), (el) => {
if (_.isEqual(_.pick(el.props, Object.keys(props)), props)) {
return el;
}
return false;
}));
}
let ReactElementContainers = [];
// Override ReactTestUtils.renderIntoDocument so that
// we can remove all the created elements after the test completes.
export function renderIntoDocument(element) {
const container = document.createElement('div');
ReactElementContainers.push(container);
return ReactDOM.render(element, container);
}
export function unmountAll() {
for (let i = 0; i < ReactElementContainers.length; i++) {
const container = ReactElementContainers[i];
ReactDOM.unmountComponentAtNode(container);
}
ReactElementContainers = [];
}