UNPKG

enzyme

Version:

JavaScript Testing utilities for React

174 lines (156 loc) 7.43 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.unmountComponentAtNode = exports.renderWithOptions = exports.childrenToArray = exports.findAllInRenderedTree = exports.findDOMNode = exports.Simulate = exports.isCompositeComponentElement = exports.isCompositeComponentWithType = exports.isCompositeComponent = exports.isDOMComponent = exports.isElementOfType = exports.isElement = exports.mockComponent = exports.renderIntoDocument = exports.renderToStaticMarkup = exports.createShallowRenderer = undefined; var _version = require('./version'); var TestUtils = undefined; /* eslint react/no-deprecated: 0 */ var createShallowRenderer = undefined; var renderToStaticMarkup = undefined; var renderIntoDocument = undefined; var findDOMNode = undefined; var React = undefined; var ReactContext = undefined; var childrenToArray = undefined; var renderWithOptions = undefined; var unmountComponentAtNode = undefined; React = require('react'); if (_version.REACT013) { exports.renderToStaticMarkup = renderToStaticMarkup = React.renderToStaticMarkup; /* eslint-disable react/no-deprecated */ exports.findDOMNode = findDOMNode = React.findDOMNode; exports.unmountComponentAtNode = unmountComponentAtNode = React.unmountComponentAtNode; /* 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. exports.createShallowRenderer = createShallowRenderer = function () { function createRendererCompatible() { var renderer = TestUtils.createRenderer(); renderer.render = function (originalRender) { return function () { function contextCompatibleRender(node) { var context = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; ReactContext.current = context; originalRender.call(this, React.createElement(node.type, node.props), context); ReactContext.current = {}; return renderer.getRenderOutput(); } return contextCompatibleRender; }(); }(renderer.render); return renderer; } return createRendererCompatible; }(); exports.renderIntoDocument = 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. exports.childrenToArray = childrenToArray = function childrenToArray(children) { var results = []; if (children !== undefined && children !== null && children !== false) { React.Children.forEach(children, function (el) { if (el !== undefined && el !== null && el !== false) { results.push(el); } }); } return results; }; exports.renderWithOptions = renderWithOptions = function renderWithOptions(node, options) { if (options.attachTo) { return React.render(node, options.attachTo); } return TestUtils.renderIntoDocument(node); }; } else { (function () { var ReactDOM = require('react-dom'); exports.renderToStaticMarkup = renderToStaticMarkup = require('react-dom/server').renderToStaticMarkup; exports.findDOMNode = findDOMNode = ReactDOM.findDOMNode; exports.unmountComponentAtNode = unmountComponentAtNode = ReactDOM.unmountComponentAtNode; // 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. exports.createShallowRenderer = createShallowRenderer = function () { function createRendererCompatible() { var renderer = TestUtils.createRenderer(); var isDOM = false; var _node = undefined; return { _instance: renderer._instance, render: function () { function render(node, context) { if (typeof node.type === 'string') { isDOM = true; _node = node; } else { isDOM = false; renderer.render(node, context); this._instance = renderer._instance; } } return render; }(), getRenderOutput: function () { function getRenderOutput() { if (isDOM) { return _node; } return renderer.getRenderOutput(); } return getRenderOutput; }() }; } return createRendererCompatible; }(); exports.renderIntoDocument = renderIntoDocument = TestUtils.renderIntoDocument; exports.childrenToArray = childrenToArray = React.Children.toArray; exports.renderWithOptions = renderWithOptions = function renderWithOptions(node, options) { if (options.attachTo) { return ReactDOM.render(node, options.attachTo); } return TestUtils.renderIntoDocument(node); }; })(); } var _TestUtils = TestUtils; var mockComponent = _TestUtils.mockComponent; var isElement = _TestUtils.isElement; var isElementOfType = _TestUtils.isElementOfType; var isDOMComponent = _TestUtils.isDOMComponent; var isCompositeComponent = _TestUtils.isCompositeComponent; var isCompositeComponentWithType = _TestUtils.isCompositeComponentWithType; var isCompositeComponentElement = _TestUtils.isCompositeComponentElement; var Simulate = _TestUtils.Simulate; var findAllInRenderedTree = _TestUtils.findAllInRenderedTree; exports.createShallowRenderer = createShallowRenderer; exports.renderToStaticMarkup = renderToStaticMarkup; exports.renderIntoDocument = renderIntoDocument; exports.mockComponent = mockComponent; exports.isElement = isElement; exports.isElementOfType = isElementOfType; exports.isDOMComponent = isDOMComponent; exports.isCompositeComponent = isCompositeComponent; exports.isCompositeComponentWithType = isCompositeComponentWithType; exports.isCompositeComponentElement = isCompositeComponentElement; exports.Simulate = Simulate; exports.findDOMNode = findDOMNode; exports.findAllInRenderedTree = findAllInRenderedTree; exports.childrenToArray = childrenToArray; exports.renderWithOptions = renderWithOptions; exports.unmountComponentAtNode = unmountComponentAtNode;