UNPKG

5.81 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9'use strict';
10
11var _prodInvariant = require('./reactProdInvariant'),
12 _assign = require('object-assign');
13
14function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
15
16var React = require('react/lib/React');
17var ReactCompositeComponent = require('./ReactCompositeComponent');
18var ReactDefaultBatchingStrategy = require('./ReactDefaultBatchingStrategy');
19var ReactReconciler = require('./ReactReconciler');
20var ReactReconcileTransaction = require('./ReactReconcileTransaction');
21var ReactUpdates = require('./ReactUpdates');
22
23var emptyObject = require('fbjs/lib/emptyObject');
24var getNextDebugID = require('react/lib/getNextDebugID');
25var invariant = require('fbjs/lib/invariant');
26
27function injectDefaults() {
28 ReactUpdates.injection.injectReconcileTransaction(ReactReconcileTransaction);
29 ReactUpdates.injection.injectBatchingStrategy(ReactDefaultBatchingStrategy);
30}
31
32var NoopInternalComponent = function () {
33 function NoopInternalComponent(element) {
34 _classCallCheck(this, NoopInternalComponent);
35
36 this._renderedOutput = element;
37 this._currentElement = element;
38
39 if (process.env.NODE_ENV !== 'production') {
40 this._debugID = getNextDebugID();
41 }
42 }
43
44 NoopInternalComponent.prototype.mountComponent = function mountComponent() {};
45
46 NoopInternalComponent.prototype.receiveComponent = function receiveComponent(element) {
47 this._renderedOutput = element;
48 this._currentElement = element;
49 };
50
51 NoopInternalComponent.prototype.unmountComponent = function unmountComponent() {};
52
53 NoopInternalComponent.prototype.getHostNode = function getHostNode() {
54 return undefined;
55 };
56
57 NoopInternalComponent.prototype.getPublicInstance = function getPublicInstance() {
58 return null;
59 };
60
61 return NoopInternalComponent;
62}();
63
64var ShallowComponentWrapper = function (element) {
65 // TODO: Consolidate with instantiateReactComponent
66 if (process.env.NODE_ENV !== 'production') {
67 this._debugID = getNextDebugID();
68 }
69
70 this.construct(element);
71};
72_assign(ShallowComponentWrapper.prototype, ReactCompositeComponent, {
73 _constructComponent: ReactCompositeComponent._constructComponentWithoutOwner,
74 _instantiateReactComponent: function (element) {
75 return new NoopInternalComponent(element);
76 },
77 _replaceNodeWithMarkup: function () {},
78 _renderValidatedComponent: ReactCompositeComponent._renderValidatedComponentWithoutOwnerOrContext
79});
80
81function _batchedRender(renderer, element, context) {
82 var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(true);
83 renderer._render(element, transaction, context);
84 ReactUpdates.ReactReconcileTransaction.release(transaction);
85}
86
87var ReactShallowRenderer = function () {
88 function ReactShallowRenderer() {
89 _classCallCheck(this, ReactShallowRenderer);
90
91 this._instance = null;
92 }
93
94 ReactShallowRenderer.prototype.getMountedInstance = function getMountedInstance() {
95 return this._instance ? this._instance._instance : null;
96 };
97
98 ReactShallowRenderer.prototype.render = function render(element, context) {
99 // Ensure we've done the default injections. This might not be true in the
100 // case of a simple test that only requires React and the TestUtils in
101 // conjunction with an inline-requires transform.
102 injectDefaults();
103
104 !React.isValidElement(element) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactShallowRenderer render(): Invalid component element.%s', typeof element === 'function' ? ' Instead of passing a component class, make sure to instantiate ' + 'it by passing it to React.createElement.' : '') : _prodInvariant('12', typeof element === 'function' ? ' Instead of passing a component class, make sure to instantiate ' + 'it by passing it to React.createElement.' : '') : void 0;
105 !(typeof element.type !== 'string') ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactShallowRenderer render(): Shallow rendering works only with custom components, not primitives (%s). Instead of calling `.render(el)` and inspecting the rendered output, look at `el.props` directly instead.', element.type) : _prodInvariant('13', element.type) : void 0;
106
107 if (!context) {
108 context = emptyObject;
109 }
110 ReactUpdates.batchedUpdates(_batchedRender, this, element, context);
111
112 return this.getRenderOutput();
113 };
114
115 ReactShallowRenderer.prototype.getRenderOutput = function getRenderOutput() {
116 return this._instance && this._instance._renderedComponent && this._instance._renderedComponent._renderedOutput || null;
117 };
118
119 ReactShallowRenderer.prototype.unmount = function unmount() {
120 if (this._instance) {
121 ReactReconciler.unmountComponent(this._instance, false);
122 }
123 };
124
125 ReactShallowRenderer.prototype.unstable_batchedUpdates = function unstable_batchedUpdates(callback, bookkeeping) {
126 // This is used by Enzyme for fake-simulating events in shallow mode.
127 injectDefaults();
128 return ReactUpdates.batchedUpdates(callback, bookkeeping);
129 };
130
131 ReactShallowRenderer.prototype._render = function _render(element, transaction, context) {
132 if (this._instance) {
133 ReactReconciler.receiveComponent(this._instance, element, transaction, context);
134 } else {
135 var instance = new ShallowComponentWrapper(element);
136 ReactReconciler.mountComponent(instance, transaction, null, null, context, 0);
137 this._instance = instance;
138 }
139 };
140
141 return ReactShallowRenderer;
142}();
143
144ReactShallowRenderer.createRenderer = function () {
145 return new ReactShallowRenderer();
146};
147
148module.exports = ReactShallowRenderer;
\No newline at end of file