1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 106x 106x 106x 106x 112x 79x 79x 33x 33x 33x 118x 79x 39x 1x 1x | /* eslint react/no-deprecated: 0 */ import { REACT013 } from './version'; let TestUtils; let createShallowRenderer; let renderToStaticMarkup; let renderIntoDocument; let findDOMNode; let React; let ReactContext; let childrenToArray; React = require('react'); Iif (REACT013) { renderToStaticMarkup = React.renderToStaticMarkup; /* eslint-disable react/no-deprecated */ findDOMNode = React.findDOMNode; /* eslint-enable react/no-deprecated */ TestUtils = require('react/addons').addons.TestUtils; ReactContext = require('react/lib/ReactContext'); // Shallow rendering in 0.13 did not properly support context. This function provides a shim // around `TestUtils.createRenderer` that instead returns a ShallowRenderer that actually // works with context. See https://github.com/facebook/react/issues/3721 for more details. createShallowRenderer = function createRendererCompatible() { const renderer = TestUtils.createRenderer(); renderer.render = (originalRender => function contextCompatibleRender(node, context = {}) { ReactContext.current = context; originalRender.call(this, React.createElement(node.type, node.props), context); ReactContext.current = {}; return renderer.getRenderOutput(); })(renderer.render); return renderer; }; renderIntoDocument = TestUtils.renderIntoDocument; // this fixes some issues in React 0.13 with setState and jsdom... // see issue: https://github.com/airbnb/enzyme/issues/27 require('react/lib/ExecutionEnvironment').canUseDOM = true; // in 0.13, a Children.toArray function was not exported. Make our own instead. childrenToArray = (children) => { const results = []; if (children !== undefined && children !== null && children !== false) { React.Children.forEach(children, (el) => { if (el !== undefined && el !== null && el !== false) { results.push(el); } }); } return results; }; } else { renderToStaticMarkup = require('react-dom/server').renderToStaticMarkup; findDOMNode = require('react-dom').findDOMNode; // We require the testutils, but they don't come with 0.14 out of the box, so we // require them here through this node module. The bummer is that we are not able // to list this as a dependency in package.json and have 0.13 work properly. // As a result, right now this is basically an implicit dependency. TestUtils = require('react-addons-test-utils'); // Shallow rendering changed from 0.13 => 0.14 in such a way that // 0.14 now does not allow shallow rendering of native DOM elements. // This is mainly because the result of such a call should not realistically // be any different than the JSX you passed in (result of `React.createElement`. // In order to maintain the same behavior across versions, this function // is essentially a replacement for `TestUtils.createRenderer` that doesn't use // shallow rendering when it's just a DOM element. createShallowRenderer = function createRendererCompatible() { const renderer = TestUtils.createRenderer(); let isDOM = false; let _node; return { _instance: renderer._instance, render(node, context) { if (typeof node.type === 'string') { isDOM = true; _node = node; } else { isDOM = false; renderer.render(node, context); this._instance = renderer._instance; } }, getRenderOutput() { if (isDOM) { return _node; } return renderer.getRenderOutput(); }, }; }; renderIntoDocument = TestUtils.renderIntoDocument; childrenToArray = React.Children.toArray; } const { mockComponent, isElement, isElementOfType, isDOMComponent, isCompositeComponent, isCompositeComponentWithType, isCompositeComponentElement, Simulate, findAllInRenderedTree, } = TestUtils; export { createShallowRenderer, renderToStaticMarkup, renderIntoDocument, mockComponent, isElement, isElementOfType, isDOMComponent, isCompositeComponent, isCompositeComponentWithType, isCompositeComponentElement, Simulate, findDOMNode, findAllInRenderedTree, childrenToArray, }; |