77 lines
57 KiB
JavaScript
77 lines
57 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.createMockReact = createMockReact;
|
|
|
|
var _babylon = require("babylon");
|
|
|
|
var _index = require("../../values/index.js");
|
|
|
|
var _index2 = require("../../methods/index.js");
|
|
|
|
var _singletons = require("../../singletons.js");
|
|
|
|
var _utils = require("../../react/utils.js");
|
|
|
|
var _invariant = require("../../invariant");
|
|
|
|
var _invariant2 = _interopRequireDefault(_invariant);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
// most of the code here was taken from https://github.com/facebook/react/blob/master/packages/react/src/ReactElement.js
|
|
var reactCode = "\n function createReact(REACT_ELEMENT_TYPE, REACT_SYMBOL_TYPE, ReactCurrentOwner) {\n var hasOwnProperty = Object.prototype.hasOwnProperty;\n var RESERVED_PROPS = {\n key: true,\n ref: true,\n __self: true,\n __source: true,\n };\n\n var ReactElement = function(type, key, ref, self, source, owner, props) {\n return {\n // This tag allow us to uniquely identify this as a React Element\n $$typeof: REACT_ELEMENT_TYPE,\n \n // Built-in properties that belong on the element\n type: type,\n key: key,\n ref: ref,\n props: props,\n \n // Record the component responsible for creating this element.\n _owner: owner,\n };\n };\n\n function hasValidRef(config) {\n return config.ref !== undefined;\n }\n \n function hasValidKey(config) {\n return config.key !== undefined;\n }\n\n class Component {\n constructor(props, context) {\n // stub, this constructor is never evaluated\n throw new Error(\"React.Component constructor should never evaluate\");\n }\n getChildContext() {}\n }\n\n Component.prototype.isReactComponent = {};\n\n class PureComponent {\n constructor(props, context) {\n // stub, this constructor is never evaluated\n throw new Error(\"React.PureComponent constructor should never evaluate\");\n }\n }\n\n PureComponent.prototype.isReactComponent = {};\n PureComponent.prototype.isPureReactComponent = true;\n\n function forEachChildren() {\n throw new Error(\"TODO: React.Children.forEach is not yet supported\");\n }\n\n function mapChildren() {\n throw new Error(\"TODO: React.Children.map is not yet supported\");\n }\n\n function countChildren() {\n throw new Error(\"TODO: React.Children.count is not yet supported\");\n }\n\n function onlyChild() {\n throw new Error(\"TODO: React.Children.only is not yet supported\");\n }\n\n function toArray() {\n throw new Error(\"TODO: React.Children.toArray is not yet supported\");\n }\n\n function createElement(type, config, children) {\n var propName;\n\n // Reserved names are extracted\n var props = {};\n\n var key = null;\n var ref = null;\n var self = null;\n var source = null;\n\n if (config != null) {\n if (hasValidRef(config)) {\n ref = config.ref;\n }\n if (hasValidKey(config)) {\n key = '' + config.key;\n }\n \n self = config.__self === undefined ? null : config.__self;\n source = config.__source === undefined ? null : config.__source;\n // Remaining properties are added to a new props object\n for (propName in config) {\n if (\n hasOwnProperty.call(config, propName) &&\n !RESERVED_PROPS.hasOwnProperty(propName)\n ) {\n props[propName] = config[propName];\n }\n }\n }\n\n // Children can be more than one argument, and those are transferred onto\n // the newly allocated props object.\n var childrenLength = arguments.length - 2;\n if (childrenLength === 1) {\n props.children = children;\n } else if (childrenLength > 1) {\n var childArray = Array(childrenLength);\n for (var i = 0; i < childrenLength; i++) {\n childArray[i] = arguments[i + 2];\n }\n props.children = childArray;\n }\n \n // Resolve default props\n if (type && type.defaultProps) {\n var defaultProps = type.defaultProps;\n for (propName in defaultProps) {\n if (props[propName] === undefined) {\n props[propName] = defaultProps[propName];\n }\n }\n }\n \n return ReactElement(\n type,\n key,\n ref,\n self,\n source,\n ReactCurrentOwner.current,\n props,\n );\n }\n\n function cloneElement(element, config, children) {\n var propName;\n \n // Original props are copied\n var props = Object.assign({}, element.props);\n \n // Reserved names are extracted\n var key = element.key;\n var ref = element.ref;\n // Self is preserved since the owner is preserved.\n var self = element._self;\n // Source is preserved since cloneElement is unlikely to be targeted by a\n // transpiler, and the original source is probably a better indicator of the\n // true owner.\n var source = element._source;\n \n // Owner will be preserved, unless ref is overridden\n var owner = element._owner;\n \n if (config != null) {\n if (hasValidRef(config)) {\n // Silently steal the ref from the parent.\n ref = config.ref;\n owner = ReactCurrentOwner.current;\n }\n if (hasValidKey(config)) {\n key = '' + config.key;\n }\n \n // Remaining properties override existing props\n var defaultProps;\n if (element.type && element.type.defaultProps) {\n defaultProps = element.type.defaultProps;\n }\n for (propName in config) {\n if (\n hasOwnProperty.call(config, propName) &&\n !RESERVED_PROPS.hasOwnProperty(propName)\n ) {\n if (config[propName] === undefined && defaultProps !== undefined) {\n // Resolve default props\n props[propName] = defaultProps[propName];\n } else {\n props[propName] = config[propName];\n }\n }\n }\n }\n \n // Children can be more than one argument, and those are transferred onto\n // the newly allocated props object.\n var childrenLength = arguments.length - 2;\n if (childrenLength === 1) {\n props.children = children;\n } else if (childrenLength > 1) {\n var childArray = Array(childrenLength);\n for (var i = 0; i < childrenLength; i++) {\n childArray[i] = arguments[i + 2];\n }\n props.children = childArray;\n }\n \n return ReactElement(element.type, key, ref, self, source, owner, props);\n }\n\n function isValidElement(object) {\n return (\n typeof object === 'object' &&\n object !== null &&\n object.$$typeof === REACT_ELEMENT_TYPE\n );\n }\n\n return {\n Children: {\n forEach: forEachChildren,\n map: mapChildren,\n count: countChildren,\n only: onlyChild,\n toArray,\n },\n Component,\n PureComponent,\n Fragment: REACT_SYMBOL_TYPE,\n createElement,\n cloneElement,\n isValidElement,\n version: \"16.2.0\",\n };\n }\n"; /**
|
|
* Copyright (c) 2017-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
var reactAst = (0, _babylon.parseExpression)(reactCode, { plugins: ["flow"] });
|
|
|
|
function createMockReact(realm) {
|
|
var reactFactory = _singletons.Environment.GetValue(realm, realm.$GlobalEnv.evaluate(reactAst, false));
|
|
(0, _invariant2.default)(reactFactory instanceof _index.ECMAScriptSourceFunctionValue);
|
|
|
|
var currentOwner = realm.react.currentOwner = new _index.ObjectValue(realm, realm.intrinsics.ObjectPrototype, "currentOwner");
|
|
// this is to get around Flow getting confused
|
|
var factory = reactFactory.$Call;
|
|
(0, _invariant2.default)(factory !== undefined);
|
|
var reactValue = factory(realm.intrinsics.undefined, [(0, _utils.getReactSymbol)("react.element", realm), (0, _utils.getReactSymbol)("react.symbol", realm), currentOwner]);
|
|
reactValue.intrinsicName = "require(\"react\")";
|
|
(0, _invariant2.default)(reactValue instanceof _index.ObjectValue);
|
|
|
|
var reactComponentValue = (0, _index2.Get)(realm, reactValue, "Component");
|
|
reactComponentValue.intrinsicName = "require(\"react\").Component";
|
|
(0, _invariant2.default)(reactComponentValue instanceof _index.ECMAScriptFunctionValue);
|
|
var reactPureComponentValue = (0, _index2.Get)(realm, reactValue, "PureComponent");
|
|
reactPureComponentValue.intrinsicName = "require(\"react\").PureComponent";
|
|
(0, _invariant2.default)(reactPureComponentValue instanceof _index.ECMAScriptFunctionValue);
|
|
reactComponentValue.$FunctionKind = "normal";
|
|
(0, _invariant2.default)(reactComponentValue instanceof _index.ObjectValue);
|
|
|
|
var reactComponentPrototypeValue = (0, _index2.Get)(realm, reactComponentValue, "prototype");
|
|
reactComponentPrototypeValue.intrinsicName = "require(\"react\").Component.prototype";
|
|
|
|
var reactPureComponentPrototypeValue = (0, _index2.Get)(realm, reactPureComponentValue, "prototype");
|
|
reactPureComponentPrototypeValue.intrinsicName = "require(\"react\").PureComponent.prototype";
|
|
|
|
var reactCloneElementValue = (0, _index2.Get)(realm, reactValue, "cloneElement");
|
|
reactCloneElementValue.intrinsicName = "require(\"react\").cloneElement";
|
|
|
|
var reactCreateElementValue = (0, _index2.Get)(realm, reactValue, "createElement");
|
|
reactCreateElementValue.intrinsicName = "require(\"react\").createElement";
|
|
|
|
var reactIsValidElementValue = (0, _index2.Get)(realm, reactValue, "isValidElement");
|
|
reactIsValidElementValue.intrinsicName = "require(\"react\").isValidElement";
|
|
|
|
var reactChildrenValue = (0, _index2.Get)(realm, reactValue, "Children");
|
|
reactChildrenValue.intrinsicName = "require(\"react\").Children";
|
|
|
|
return reactValue;
|
|
}
|
|
//# sourceMappingURL=mocks.js.map
|