react 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/README.md +0 -8
  2. package/addons.js +0 -3
  3. package/lib/AutoFocusMixin.js +32 -0
  4. package/lib/CSSCore.js +23 -22
  5. package/lib/CSSProperty.js +32 -1
  6. package/lib/CSSPropertyOperations.js +1 -1
  7. package/lib/ChangeEventPlugin.js +27 -5
  8. package/lib/ClientReactRootIndex.js +30 -0
  9. package/lib/CompositionEventPlugin.js +58 -10
  10. package/lib/DOMChildrenOperations.js +39 -3
  11. package/lib/DOMProperty.js +9 -5
  12. package/lib/DOMPropertyOperations.js +21 -8
  13. package/lib/Danger.js +9 -8
  14. package/lib/DefaultDOMPropertyConfig.js +23 -14
  15. package/lib/DefaultEventPluginOrder.js +1 -1
  16. package/lib/EnterLeaveEventPlugin.js +38 -5
  17. package/lib/EventConstants.js +4 -1
  18. package/lib/EventListener.js +42 -34
  19. package/lib/EventPluginHub.js +118 -13
  20. package/lib/EventPluginRegistry.js +62 -18
  21. package/lib/EventPluginUtils.js +33 -4
  22. package/lib/EventPropagators.js +7 -43
  23. package/lib/ExecutionEnvironment.js +4 -1
  24. package/lib/LinkedStateMixin.js +1 -1
  25. package/lib/LinkedValueUtils.js +160 -0
  26. package/lib/MobileSafariClickEventPlugin.js +1 -1
  27. package/lib/PooledClass.js +7 -1
  28. package/lib/React.js +30 -4
  29. package/lib/ReactBrowserComponentMixin.js +42 -0
  30. package/lib/ReactCSSTransitionGroup.js +65 -0
  31. package/lib/{ReactTransitionableChild.js → ReactCSSTransitionGroupChild.js} +22 -36
  32. package/lib/ReactChildren.js +4 -4
  33. package/lib/ReactComponent.js +163 -83
  34. package/lib/ReactComponentBrowserEnvironment.js +55 -71
  35. package/lib/ReactCompositeComponent.js +686 -119
  36. package/lib/ReactContext.js +67 -0
  37. package/lib/ReactCurrentOwner.js +1 -1
  38. package/lib/ReactDOM.js +19 -6
  39. package/lib/ReactDOMButton.js +6 -1
  40. package/lib/ReactDOMComponent.js +66 -24
  41. package/lib/ReactDOMForm.js +13 -3
  42. package/lib/ReactDOMIDOperations.js +106 -61
  43. package/lib/ReactDOMImg.js +61 -0
  44. package/lib/ReactDOMInput.js +28 -15
  45. package/lib/ReactDOMOption.js +13 -8
  46. package/lib/ReactDOMSelect.js +38 -18
  47. package/lib/ReactDOMSelection.js +1 -1
  48. package/lib/ReactDOMTextarea.js +19 -11
  49. package/lib/ReactDefaultBatchingStrategy.js +1 -1
  50. package/lib/ReactDefaultInjection.js +60 -26
  51. package/lib/ReactDefaultPerf.js +208 -371
  52. package/lib/ReactDefaultPerfAnalysis.js +199 -0
  53. package/lib/ReactErrorUtils.js +6 -15
  54. package/lib/ReactEventEmitter.js +144 -146
  55. package/lib/ReactEventEmitterMixin.js +1 -33
  56. package/lib/ReactEventTopLevelCallback.js +75 -15
  57. package/lib/ReactInjection.js +43 -0
  58. package/lib/ReactInputSelection.js +3 -2
  59. package/lib/ReactInstanceHandles.js +36 -20
  60. package/lib/ReactLink.js +2 -2
  61. package/lib/ReactMarkupChecksum.js +1 -1
  62. package/lib/ReactMount.js +136 -104
  63. package/lib/ReactMountReady.js +2 -2
  64. package/lib/ReactMultiChild.js +40 -49
  65. package/lib/ReactMultiChildUpdateTypes.js +3 -1
  66. package/lib/ReactOwner.js +17 -4
  67. package/lib/ReactPerf.js +6 -9
  68. package/lib/ReactPropTransferer.js +41 -22
  69. package/lib/ReactPropTypeLocationNames.js +31 -0
  70. package/lib/{ReactComponentEnvironment.js → ReactPropTypeLocations.js} +11 -6
  71. package/lib/ReactPropTypes.js +249 -48
  72. package/lib/ReactPutListenerQueue.js +61 -0
  73. package/lib/ReactReconcileTransaction.js +28 -7
  74. package/lib/ReactRootIndex.js +36 -0
  75. package/lib/ReactServerRendering.js +46 -19
  76. package/lib/ReactServerRenderingTransaction.js +116 -0
  77. package/lib/ReactStateSetters.js +1 -1
  78. package/lib/ReactTestUtils.js +394 -0
  79. package/lib/ReactTextComponent.js +33 -6
  80. package/lib/{ReactTransitionKeySet.js → ReactTransitionChildMapping.js} +43 -48
  81. package/lib/ReactTransitionEvents.js +1 -1
  82. package/lib/ReactTransitionGroup.js +133 -58
  83. package/lib/ReactUpdates.js +15 -12
  84. package/lib/ReactWithAddons.js +15 -3
  85. package/lib/SelectEventPlugin.js +23 -40
  86. package/lib/ServerReactRootIndex.js +36 -0
  87. package/lib/SimpleEventPlugin.js +55 -7
  88. package/lib/SyntheticClipboardEvent.js +8 -2
  89. package/lib/SyntheticCompositionEvent.js +1 -1
  90. package/lib/SyntheticDragEvent.js +44 -0
  91. package/lib/SyntheticEvent.js +3 -2
  92. package/lib/SyntheticFocusEvent.js +1 -1
  93. package/lib/SyntheticKeyboardEvent.js +5 -3
  94. package/lib/SyntheticMouseEvent.js +1 -1
  95. package/lib/SyntheticTouchEvent.js +1 -1
  96. package/lib/SyntheticUIEvent.js +1 -1
  97. package/lib/SyntheticWheelEvent.js +11 -8
  98. package/lib/Transaction.js +62 -37
  99. package/lib/ViewportMetrics.js +1 -1
  100. package/lib/accumulate.js +1 -1
  101. package/lib/adler32.js +1 -1
  102. package/lib/cloneWithProps.js +59 -0
  103. package/lib/containsNode.js +1 -1
  104. package/lib/copyProperties.js +1 -1
  105. package/lib/createArrayFrom.js +11 -14
  106. package/lib/createFullPageComponent.js +63 -0
  107. package/lib/createNodesFromMarkup.js +1 -1
  108. package/lib/createObjectFrom.js +1 -1
  109. package/lib/cx.js +3 -3
  110. package/lib/dangerousStyleValue.js +1 -1
  111. package/lib/emptyFunction.js +1 -1
  112. package/lib/emptyObject.js +27 -0
  113. package/lib/escapeTextForBrowser.js +1 -1
  114. package/lib/flattenChildren.js +6 -3
  115. package/lib/focusNode.js +33 -0
  116. package/lib/forEachAccumulated.js +1 -1
  117. package/lib/getActiveElement.js +5 -4
  118. package/lib/getEventKey.js +85 -0
  119. package/lib/getEventTarget.js +1 -1
  120. package/lib/getMarkupWrap.js +11 -1
  121. package/lib/getNodeForCharacterOffset.js +1 -1
  122. package/lib/getReactRootElementInContainer.js +1 -1
  123. package/lib/getTextContentAccessor.js +6 -4
  124. package/lib/getUnboundedScrollPosition.js +3 -3
  125. package/lib/hyphenate.js +1 -1
  126. package/lib/instantiateReactComponent.js +70 -0
  127. package/lib/invariant.js +20 -12
  128. package/lib/isEventSupported.js +8 -12
  129. package/lib/isNode.js +2 -2
  130. package/lib/isTextInputElement.js +1 -1
  131. package/lib/isTextNode.js +1 -1
  132. package/lib/joinClasses.js +1 -1
  133. package/lib/keyMirror.js +1 -1
  134. package/lib/keyOf.js +1 -1
  135. package/lib/memoizeStringOnly.js +1 -1
  136. package/lib/merge.js +1 -1
  137. package/lib/mergeHelpers.js +6 -7
  138. package/lib/mergeInto.js +1 -1
  139. package/lib/mixInto.js +1 -1
  140. package/lib/monitorCodeUse.js +37 -0
  141. package/lib/objMap.js +1 -1
  142. package/lib/objMapKeyVal.js +1 -1
  143. package/lib/onlyChild.js +43 -0
  144. package/lib/performanceNow.js +1 -1
  145. package/lib/shallowEqual.js +1 -1
  146. package/lib/shouldUpdateReactComponent.js +61 -0
  147. package/lib/toArray.js +75 -0
  148. package/lib/traverseAllChildren.js +72 -9
  149. package/lib/update.js +159 -0
  150. package/lib/warning.js +48 -0
  151. package/package.json +3 -3
  152. package/react.js +0 -3
  153. package/ReactJSErrors.js +0 -40
  154. package/lib/$.js +0 -46
  155. package/lib/CallbackRegistry.js +0 -91
  156. package/lib/LinkedValueMixin.js +0 -68
  157. package/lib/ex.js +0 -49
  158. package/lib/filterAttributes.js +0 -45
  159. package/lib/ge.js +0 -76
  160. package/lib/mutateHTMLNodeWithMarkup.js +0 -100
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Copyright 2013-2014 Facebook, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ * @providesModule ReactRootIndex
17
+ * @typechecks
18
+ */
19
+
20
+ "use strict";
21
+
22
+ var ReactRootIndexInjection = {
23
+ /**
24
+ * @param {function} _createReactRootIndex
25
+ */
26
+ injectCreateReactRootIndex: function(_createReactRootIndex) {
27
+ ReactRootIndex.createReactRootIndex = _createReactRootIndex;
28
+ }
29
+ };
30
+
31
+ var ReactRootIndex = {
32
+ createReactRootIndex: null,
33
+ injection: ReactRootIndexInjection
34
+ };
35
+
36
+ module.exports = ReactRootIndex;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2013 Facebook, Inc.
2
+ * Copyright 2013-2014 Facebook, Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -21,42 +21,69 @@
21
21
  var ReactComponent = require("./ReactComponent");
22
22
  var ReactInstanceHandles = require("./ReactInstanceHandles");
23
23
  var ReactMarkupChecksum = require("./ReactMarkupChecksum");
24
- var ReactReconcileTransaction = require("./ReactReconcileTransaction");
24
+ var ReactServerRenderingTransaction =
25
+ require("./ReactServerRenderingTransaction");
25
26
 
27
+ var instantiateReactComponent = require("./instantiateReactComponent");
26
28
  var invariant = require("./invariant");
27
29
 
28
30
  /**
29
31
  * @param {ReactComponent} component
30
- * @param {function} callback
32
+ * @return {string} the HTML markup
31
33
  */
32
- function renderComponentToString(component, callback) {
33
- // We use a callback API to keep the API async in case in the future we ever
34
- // need it, but in reality this is a synchronous operation.
35
-
34
+ function renderComponentToString(component) {
36
35
  ("production" !== process.env.NODE_ENV ? invariant(
37
36
  ReactComponent.isValidComponent(component),
38
37
  'renderComponentToString(): You must pass a valid ReactComponent.'
39
38
  ) : invariant(ReactComponent.isValidComponent(component)));
40
39
 
41
40
  ("production" !== process.env.NODE_ENV ? invariant(
42
- typeof callback === 'function',
43
- 'renderComponentToString(): You must pass a function as a callback.'
44
- ) : invariant(typeof callback === 'function'));
41
+ !(arguments.length === 2 && typeof arguments[1] === 'function'),
42
+ 'renderComponentToString(): This function became synchronous and now ' +
43
+ 'returns the generated markup. Please remove the second parameter.'
44
+ ) : invariant(!(arguments.length === 2 && typeof arguments[1] === 'function')));
45
+
46
+ var transaction;
47
+ try {
48
+ var id = ReactInstanceHandles.createReactRootID();
49
+ transaction = ReactServerRenderingTransaction.getPooled(false);
50
+
51
+ return transaction.perform(function() {
52
+ var componentInstance = instantiateReactComponent(component);
53
+ var markup = componentInstance.mountComponent(id, transaction, 0);
54
+ return ReactMarkupChecksum.addChecksumToMarkup(markup);
55
+ }, null);
56
+ } finally {
57
+ ReactServerRenderingTransaction.release(transaction);
58
+ }
59
+ }
60
+
61
+ /**
62
+ * @param {ReactComponent} component
63
+ * @return {string} the HTML markup, without the extra React ID and checksum
64
+ * (for generating static pages)
65
+ */
66
+ function renderComponentToStaticMarkup(component) {
67
+ ("production" !== process.env.NODE_ENV ? invariant(
68
+ ReactComponent.isValidComponent(component),
69
+ 'renderComponentToStaticMarkup(): You must pass a valid ReactComponent.'
70
+ ) : invariant(ReactComponent.isValidComponent(component)));
45
71
 
46
- var id = ReactInstanceHandles.createReactRootID();
47
- var transaction = ReactReconcileTransaction.getPooled();
48
- transaction.reinitializeTransaction();
72
+ var transaction;
49
73
  try {
50
- transaction.perform(function() {
51
- var markup = component.mountComponent(id, transaction, 0);
52
- markup = ReactMarkupChecksum.addChecksumToMarkup(markup);
53
- callback(markup);
74
+ var id = ReactInstanceHandles.createReactRootID();
75
+ transaction = ReactServerRenderingTransaction.getPooled(true);
76
+
77
+ return transaction.perform(function() {
78
+ var componentInstance = instantiateReactComponent(component);
79
+ return componentInstance.mountComponent(id, transaction, 0);
54
80
  }, null);
55
81
  } finally {
56
- ReactReconcileTransaction.release(transaction);
82
+ ReactServerRenderingTransaction.release(transaction);
57
83
  }
58
84
  }
59
85
 
60
86
  module.exports = {
61
- renderComponentToString: renderComponentToString
87
+ renderComponentToString: renderComponentToString,
88
+ renderComponentToStaticMarkup: renderComponentToStaticMarkup
62
89
  };
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Copyright 2014 Facebook, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ * @providesModule ReactServerRenderingTransaction
17
+ * @typechecks
18
+ */
19
+
20
+ "use strict";
21
+
22
+ var PooledClass = require("./PooledClass");
23
+ var ReactMountReady = require("./ReactMountReady");
24
+ var ReactPutListenerQueue = require("./ReactPutListenerQueue");
25
+ var Transaction = require("./Transaction");
26
+
27
+ var emptyFunction = require("./emptyFunction");
28
+ var mixInto = require("./mixInto");
29
+
30
+ /**
31
+ * Provides a `ReactMountReady` queue for collecting `onDOMReady` callbacks
32
+ * during the performing of the transaction.
33
+ */
34
+ var ON_DOM_READY_QUEUEING = {
35
+ /**
36
+ * Initializes the internal `onDOMReady` queue.
37
+ */
38
+ initialize: function() {
39
+ this.reactMountReady.reset();
40
+ },
41
+
42
+ close: emptyFunction
43
+ };
44
+
45
+ var PUT_LISTENER_QUEUEING = {
46
+ initialize: function() {
47
+ this.putListenerQueue.reset();
48
+ },
49
+
50
+ close: emptyFunction
51
+ };
52
+
53
+ /**
54
+ * Executed within the scope of the `Transaction` instance. Consider these as
55
+ * being member methods, but with an implied ordering while being isolated from
56
+ * each other.
57
+ */
58
+ var TRANSACTION_WRAPPERS = [
59
+ PUT_LISTENER_QUEUEING,
60
+ ON_DOM_READY_QUEUEING
61
+ ];
62
+
63
+ /**
64
+ * @class ReactServerRenderingTransaction
65
+ * @param {boolean} renderToStaticMarkup
66
+ */
67
+ function ReactServerRenderingTransaction(renderToStaticMarkup) {
68
+ this.reinitializeTransaction();
69
+ this.renderToStaticMarkup = renderToStaticMarkup;
70
+ this.reactMountReady = ReactMountReady.getPooled(null);
71
+ this.putListenerQueue = ReactPutListenerQueue.getPooled();
72
+ }
73
+
74
+ var Mixin = {
75
+ /**
76
+ * @see Transaction
77
+ * @abstract
78
+ * @final
79
+ * @return {array} Empty list of operation wrap proceedures.
80
+ */
81
+ getTransactionWrappers: function() {
82
+ return TRANSACTION_WRAPPERS;
83
+ },
84
+
85
+ /**
86
+ * @return {object} The queue to collect `onDOMReady` callbacks with.
87
+ * TODO: convert to ReactMountReady
88
+ */
89
+ getReactMountReady: function() {
90
+ return this.reactMountReady;
91
+ },
92
+
93
+ getPutListenerQueue: function() {
94
+ return this.putListenerQueue;
95
+ },
96
+
97
+ /**
98
+ * `PooledClass` looks for this, and will invoke this before allowing this
99
+ * instance to be resused.
100
+ */
101
+ destructor: function() {
102
+ ReactMountReady.release(this.reactMountReady);
103
+ this.reactMountReady = null;
104
+
105
+ ReactPutListenerQueue.release(this.putListenerQueue);
106
+ this.putListenerQueue = null;
107
+ }
108
+ };
109
+
110
+
111
+ mixInto(ReactServerRenderingTransaction, Transaction.Mixin);
112
+ mixInto(ReactServerRenderingTransaction, Mixin);
113
+
114
+ PooledClass.addPoolingTo(ReactServerRenderingTransaction);
115
+
116
+ module.exports = ReactServerRenderingTransaction;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2013 Facebook, Inc.
2
+ * Copyright 2013-2014 Facebook, Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -0,0 +1,394 @@
1
+ /**
2
+ * Copyright 2013-2014 Facebook, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ * @providesModule ReactTestUtils
17
+ */
18
+
19
+ "use strict";
20
+
21
+ var EventConstants = require("./EventConstants");
22
+ var EventPluginHub = require("./EventPluginHub");
23
+ var EventPropagators = require("./EventPropagators");
24
+ var React = require("./React");
25
+ var ReactComponent = require("./ReactComponent");
26
+ var ReactDOM = require("./ReactDOM");
27
+ var ReactEventEmitter = require("./ReactEventEmitter");
28
+ var ReactMount = require("./ReactMount");
29
+ var ReactTextComponent = require("./ReactTextComponent");
30
+ var ReactUpdates = require("./ReactUpdates");
31
+ var SyntheticEvent = require("./SyntheticEvent");
32
+
33
+ var mergeInto = require("./mergeInto");
34
+ var copyProperties = require("./copyProperties");
35
+
36
+ var topLevelTypes = EventConstants.topLevelTypes;
37
+
38
+ function Event(suffix) {}
39
+
40
+ /**
41
+ * @class ReactTestUtils
42
+ */
43
+
44
+ /**
45
+ * Todo: Support the entire DOM.scry query syntax. For now, these simple
46
+ * utilities will suffice for testing purposes.
47
+ * @lends ReactTestUtils
48
+ */
49
+ var ReactTestUtils = {
50
+ renderIntoDocument: function(instance) {
51
+ var div = document.createElement('div');
52
+ // None of our tests actually require attaching the container to the
53
+ // DOM, and doing so creates a mess that we rely on test isolation to
54
+ // clean up, so we're going to stop honoring the name of this method
55
+ // (and probably rename it eventually) if no problems arise.
56
+ // document.documentElement.appendChild(div);
57
+ return React.renderComponent(instance, div);
58
+ },
59
+
60
+ isComponentOfType: function(inst, convenienceConstructor) {
61
+ return (
62
+ ReactComponent.isValidComponent(inst) &&
63
+ inst.type === convenienceConstructor.type
64
+ );
65
+ },
66
+
67
+ isDOMComponent: function(inst) {
68
+ return !!(inst &&
69
+ ReactComponent.isValidComponent(inst) &&
70
+ !!inst.tagName);
71
+ },
72
+
73
+ isCompositeComponent: function(inst) {
74
+ if (!ReactComponent.isValidComponent(inst)) {
75
+ return false;
76
+ }
77
+ // We check the prototype of the type that will get mounted, not the
78
+ // instance itself. This is a future proof way of duck typing.
79
+ var prototype = inst.type.prototype;
80
+ return (
81
+ typeof prototype.render === 'function' &&
82
+ typeof prototype.setState === 'function' &&
83
+ typeof prototype.updateComponent === 'function'
84
+ );
85
+ },
86
+
87
+ isCompositeComponentWithType: function(inst, type) {
88
+ return !!(ReactTestUtils.isCompositeComponent(inst) &&
89
+ (inst.constructor === type.componentConstructor ||
90
+ inst.constructor === type));
91
+ },
92
+
93
+ isTextComponent: function(inst) {
94
+ return inst instanceof ReactTextComponent;
95
+ },
96
+
97
+ findAllInRenderedTree: function(inst, test) {
98
+ if (!inst) {
99
+ return [];
100
+ }
101
+ var ret = test(inst) ? [inst] : [];
102
+ if (ReactTestUtils.isDOMComponent(inst)) {
103
+ var renderedChildren = inst._renderedChildren;
104
+ var key;
105
+ for (key in renderedChildren) {
106
+ if (!renderedChildren.hasOwnProperty(key)) {
107
+ continue;
108
+ }
109
+ ret = ret.concat(
110
+ ReactTestUtils.findAllInRenderedTree(renderedChildren[key], test)
111
+ );
112
+ }
113
+ } else if (ReactTestUtils.isCompositeComponent(inst)) {
114
+ ret = ret.concat(
115
+ ReactTestUtils.findAllInRenderedTree(inst._renderedComponent, test)
116
+ );
117
+ }
118
+ return ret;
119
+ },
120
+
121
+ /**
122
+ * Finds all instance of components in the rendered tree that are DOM
123
+ * components with the class name matching `className`.
124
+ * @return an array of all the matches.
125
+ */
126
+ scryRenderedDOMComponentsWithClass: function(root, className) {
127
+ return ReactTestUtils.findAllInRenderedTree(root, function(inst) {
128
+ var instClassName = inst.props.className;
129
+ return ReactTestUtils.isDOMComponent(inst) && (
130
+ instClassName &&
131
+ (' ' + instClassName + ' ').indexOf(' ' + className + ' ') !== -1
132
+ );
133
+ });
134
+ },
135
+
136
+ /**
137
+ * Like scryRenderedDOMComponentsWithClass but expects there to be one result,
138
+ * and returns that one result, or throws exception if there is any other
139
+ * number of matches besides one.
140
+ * @return {!ReactDOMComponent} The one match.
141
+ */
142
+ findRenderedDOMComponentWithClass: function(root, className) {
143
+ var all =
144
+ ReactTestUtils.scryRenderedDOMComponentsWithClass(root, className);
145
+ if (all.length !== 1) {
146
+ throw new Error('Did not find exactly one match for class:' + className);
147
+ }
148
+ return all[0];
149
+ },
150
+
151
+
152
+ /**
153
+ * Finds all instance of components in the rendered tree that are DOM
154
+ * components with the tag name matching `tagName`.
155
+ * @return an array of all the matches.
156
+ */
157
+ scryRenderedDOMComponentsWithTag: function(root, tagName) {
158
+ return ReactTestUtils.findAllInRenderedTree(root, function(inst) {
159
+ return ReactTestUtils.isDOMComponent(inst) &&
160
+ inst.tagName === tagName.toUpperCase();
161
+ });
162
+ },
163
+
164
+ /**
165
+ * Like scryRenderedDOMComponentsWithTag but expects there to be one result,
166
+ * and returns that one result, or throws exception if there is any other
167
+ * number of matches besides one.
168
+ * @return {!ReactDOMComponent} The one match.
169
+ */
170
+ findRenderedDOMComponentWithTag: function(root, tagName) {
171
+ var all = ReactTestUtils.scryRenderedDOMComponentsWithTag(root, tagName);
172
+ if (all.length !== 1) {
173
+ throw new Error('Did not find exactly one match for tag:' + tagName);
174
+ }
175
+ return all[0];
176
+ },
177
+
178
+
179
+ /**
180
+ * Finds all instances of components with type equal to `componentType`.
181
+ * @return an array of all the matches.
182
+ */
183
+ scryRenderedComponentsWithType: function(root, componentType) {
184
+ return ReactTestUtils.findAllInRenderedTree(root, function(inst) {
185
+ return ReactTestUtils.isCompositeComponentWithType(inst, componentType);
186
+ });
187
+ },
188
+
189
+ /**
190
+ * Same as `scryRenderedComponentsWithType` but expects there to be one result
191
+ * and returns that one result, or throws exception if there is any other
192
+ * number of matches besides one.
193
+ * @return {!ReactComponent} The one match.
194
+ */
195
+ findRenderedComponentWithType: function(root, componentType) {
196
+ var all = ReactTestUtils.scryRenderedComponentsWithType(
197
+ root,
198
+ componentType
199
+ );
200
+ if (all.length !== 1) {
201
+ throw new Error(
202
+ 'Did not find exactly one match for componentType:' + componentType
203
+ );
204
+ }
205
+ return all[0];
206
+ },
207
+
208
+ /**
209
+ * Pass a mocked component module to this method to augment it with
210
+ * useful methods that allow it to be used as a dummy React component.
211
+ * Instead of rendering as usual, the component will become a simple
212
+ * <div> containing any provided children.
213
+ *
214
+ * @param {object} module the mock function object exported from a
215
+ * module that defines the component to be mocked
216
+ * @param {?string} mockTagName optional dummy root tag name to return
217
+ * from render method (overrides
218
+ * module.mockTagName if provided)
219
+ * @return {object} the ReactTestUtils object (for chaining)
220
+ */
221
+ mockComponent: function(module, mockTagName) {
222
+ var ConvenienceConstructor = React.createClass({
223
+ render: function() {
224
+ var mockTagName = mockTagName || module.mockTagName || "div";
225
+ return ReactDOM[mockTagName](null, this.props.children);
226
+ }
227
+ });
228
+
229
+ copyProperties(module, ConvenienceConstructor);
230
+ module.mockImplementation(ConvenienceConstructor);
231
+
232
+ return this;
233
+ },
234
+
235
+ /**
236
+ * Simulates a top level event being dispatched from a raw event that occured
237
+ * on an `Element` node.
238
+ * @param topLevelType {Object} A type from `EventConstants.topLevelTypes`
239
+ * @param {!Element} node The dom to simulate an event occurring on.
240
+ * @param {?Event} fakeNativeEvent Fake native event to use in SyntheticEvent.
241
+ */
242
+ simulateNativeEventOnNode: function(topLevelType, node, fakeNativeEvent) {
243
+ var virtualHandler =
244
+ ReactEventEmitter.TopLevelCallbackCreator.createTopLevelCallback(
245
+ topLevelType
246
+ );
247
+ fakeNativeEvent.target = node;
248
+ virtualHandler(fakeNativeEvent);
249
+ },
250
+
251
+ /**
252
+ * Simulates a top level event being dispatched from a raw event that occured
253
+ * on the `ReactDOMComponent` `comp`.
254
+ * @param topLevelType {Object} A type from `EventConstants.topLevelTypes`.
255
+ * @param comp {!ReactDOMComponent}
256
+ * @param {?Event} fakeNativeEvent Fake native event to use in SyntheticEvent.
257
+ */
258
+ simulateNativeEventOnDOMComponent: function(
259
+ topLevelType,
260
+ comp,
261
+ fakeNativeEvent) {
262
+ ReactTestUtils.simulateNativeEventOnNode(
263
+ topLevelType,
264
+ comp.getDOMNode(),
265
+ fakeNativeEvent
266
+ );
267
+ },
268
+
269
+ nativeTouchData: function(x, y) {
270
+ return {
271
+ touches: [
272
+ {pageX: x, pageY: y}
273
+ ]
274
+ };
275
+ },
276
+
277
+ Simulate: null,
278
+ SimulateNative: {}
279
+ };
280
+
281
+ /**
282
+ * Exports:
283
+ *
284
+ * - `ReactTestUtils.Simulate.click(Element/ReactDOMComponent)`
285
+ * - `ReactTestUtils.Simulate.mouseMove(Element/ReactDOMComponent)`
286
+ * - `ReactTestUtils.Simulate.change(Element/ReactDOMComponent)`
287
+ * - ... (All keys from event plugin `eventTypes` objects)
288
+ */
289
+ function makeSimulator(eventType) {
290
+ return function(domComponentOrNode, eventData) {
291
+ var node;
292
+ if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
293
+ node = domComponentOrNode.getDOMNode();
294
+ } else if (domComponentOrNode.tagName) {
295
+ node = domComponentOrNode;
296
+ }
297
+
298
+ var fakeNativeEvent = new Event();
299
+ fakeNativeEvent.target = node;
300
+ // We don't use SyntheticEvent.getPooled in order to not have to worry about
301
+ // properly destroying any properties assigned from `eventData` upon release
302
+ var event = new SyntheticEvent(
303
+ ReactEventEmitter.eventNameDispatchConfigs[eventType],
304
+ ReactMount.getID(node),
305
+ fakeNativeEvent
306
+ );
307
+ mergeInto(event, eventData);
308
+ EventPropagators.accumulateTwoPhaseDispatches(event);
309
+
310
+ ReactUpdates.batchedUpdates(function() {
311
+ EventPluginHub.enqueueEvents(event);
312
+ EventPluginHub.processEventQueue();
313
+ });
314
+ };
315
+ }
316
+
317
+ function buildSimulators() {
318
+ ReactTestUtils.Simulate = {};
319
+
320
+ var eventType;
321
+ for (eventType in ReactEventEmitter.eventNameDispatchConfigs) {
322
+ /**
323
+ * @param {!Element || ReactDOMComponent} domComponentOrNode
324
+ * @param {?object} eventData Fake event data to use in SyntheticEvent.
325
+ */
326
+ ReactTestUtils.Simulate[eventType] = makeSimulator(eventType);
327
+ }
328
+ }
329
+
330
+ // Rebuild ReactTestUtils.Simulate whenever event plugins are injected
331
+ var oldInjectEventPluginOrder = EventPluginHub.injection.injectEventPluginOrder;
332
+ EventPluginHub.injection.injectEventPluginOrder = function() {
333
+ oldInjectEventPluginOrder.apply(this, arguments);
334
+ buildSimulators();
335
+ };
336
+ var oldInjectEventPlugins = EventPluginHub.injection.injectEventPluginsByName;
337
+ EventPluginHub.injection.injectEventPluginsByName = function() {
338
+ oldInjectEventPlugins.apply(this, arguments);
339
+ buildSimulators();
340
+ };
341
+
342
+ buildSimulators();
343
+
344
+ /**
345
+ * Exports:
346
+ *
347
+ * - `ReactTestUtils.SimulateNative.click(Element/ReactDOMComponent)`
348
+ * - `ReactTestUtils.SimulateNative.mouseMove(Element/ReactDOMComponent)`
349
+ * - `ReactTestUtils.SimulateNative.mouseIn/ReactDOMComponent)`
350
+ * - `ReactTestUtils.SimulateNative.mouseOut(Element/ReactDOMComponent)`
351
+ * - ... (All keys from `EventConstants.topLevelTypes`)
352
+ *
353
+ * Note: Top level event types are a subset of the entire set of handler types
354
+ * (which include a broader set of "synthetic" events). For example, onDragDone
355
+ * is a synthetic event. Except when testing an event plugin or React's event
356
+ * handling code specifically, you probably want to use ReactTestUtils.Simulate
357
+ * to dispatch synthetic events.
358
+ */
359
+
360
+ function makeNativeSimulator(eventType) {
361
+ return function(domComponentOrNode, nativeEventData) {
362
+ var fakeNativeEvent = new Event(eventType);
363
+ mergeInto(fakeNativeEvent, nativeEventData);
364
+ if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
365
+ ReactTestUtils.simulateNativeEventOnDOMComponent(
366
+ eventType,
367
+ domComponentOrNode,
368
+ fakeNativeEvent
369
+ );
370
+ } else if (!!domComponentOrNode.tagName) {
371
+ // Will allow on actual dom nodes.
372
+ ReactTestUtils.simulateNativeEventOnNode(
373
+ eventType,
374
+ domComponentOrNode,
375
+ fakeNativeEvent
376
+ );
377
+ }
378
+ };
379
+ }
380
+
381
+ var eventType;
382
+ for (eventType in topLevelTypes) {
383
+ // Event type is stored as 'topClick' - we transform that to 'click'
384
+ var convenienceName = eventType.indexOf('top') === 0 ?
385
+ eventType.charAt(3).toLowerCase() + eventType.substr(4) : eventType;
386
+ /**
387
+ * @param {!Element || ReactDOMComponent} domComponentOrNode
388
+ * @param {?Event} nativeEventData Fake native event to use in SyntheticEvent.
389
+ */
390
+ ReactTestUtils.SimulateNative[convenienceName] =
391
+ makeNativeSimulator(eventType);
392
+ }
393
+
394
+ module.exports = ReactTestUtils;