react 0.9.0-rc1 → 0.11.0-rc1

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 (168) hide show
  1. package/README.md +3 -0
  2. package/dist/JSXTransformer.js +13445 -0
  3. package/dist/react-with-addons.js +20226 -0
  4. package/dist/react-with-addons.min.js +22 -0
  5. package/dist/react.js +18434 -0
  6. package/dist/react.min.js +21 -0
  7. package/lib/AutoFocusMixin.js +4 -2
  8. package/lib/BeforeInputEventPlugin.js +222 -0
  9. package/lib/CSSCore.js +1 -1
  10. package/lib/CSSProperty.js +1 -1
  11. package/lib/CSSPropertyOperations.js +4 -4
  12. package/lib/{ReactMountReady.js → CallbackQueue.js} +33 -25
  13. package/lib/ChangeEventPlugin.js +2 -2
  14. package/lib/ClientReactRootIndex.js +1 -1
  15. package/lib/CompositionEventPlugin.js +6 -2
  16. package/lib/DOMChildrenOperations.js +34 -21
  17. package/lib/DOMProperty.js +52 -22
  18. package/lib/DOMPropertyOperations.js +29 -17
  19. package/lib/Danger.js +1 -1
  20. package/lib/DefaultEventPluginOrder.js +2 -1
  21. package/lib/EnterLeaveEventPlugin.js +1 -1
  22. package/lib/EventConstants.js +2 -1
  23. package/lib/EventListener.js +5 -2
  24. package/lib/EventPluginHub.js +5 -6
  25. package/lib/EventPluginRegistry.js +27 -4
  26. package/lib/EventPluginUtils.js +12 -2
  27. package/lib/EventPropagators.js +1 -1
  28. package/lib/ExecutionEnvironment.js +9 -3
  29. package/lib/{DefaultDOMPropertyConfig.js → HTMLDOMPropertyConfig.js} +47 -58
  30. package/lib/LinkedStateMixin.js +1 -1
  31. package/lib/LinkedValueUtils.js +27 -29
  32. package/lib/LocalEventTrapMixin.js +52 -0
  33. package/lib/MobileSafariClickEventPlugin.js +1 -1
  34. package/lib/PooledClass.js +1 -1
  35. package/lib/React.js +39 -4
  36. package/lib/ReactBrowserComponentMixin.js +46 -0
  37. package/lib/{ReactEventEmitter.js → ReactBrowserEventEmitter.js} +118 -95
  38. package/lib/ReactCSSTransitionGroup.js +13 -11
  39. package/lib/ReactCSSTransitionGroupChild.js +3 -6
  40. package/lib/ReactChildren.js +35 -14
  41. package/lib/ReactComponent.js +120 -224
  42. package/lib/ReactComponentBrowserEnvironment.js +4 -37
  43. package/lib/ReactComponentWithPureRenderMixin.js +54 -0
  44. package/lib/ReactCompositeComponent.js +250 -288
  45. package/lib/ReactContext.js +1 -1
  46. package/lib/ReactCurrentOwner.js +1 -1
  47. package/lib/ReactDOM.js +26 -24
  48. package/lib/ReactDOMButton.js +3 -2
  49. package/lib/ReactDOMComponent.js +43 -24
  50. package/lib/ReactDOMForm.js +8 -13
  51. package/lib/ReactDOMIDOperations.js +3 -32
  52. package/lib/ReactDOMImg.js +8 -14
  53. package/lib/ReactDOMInput.js +3 -2
  54. package/lib/ReactDOMOption.js +12 -8
  55. package/lib/ReactDOMSelect.js +19 -17
  56. package/lib/ReactDOMSelection.js +36 -11
  57. package/lib/ReactDOMTextarea.js +10 -8
  58. package/lib/ReactDefaultBatchingStrategy.js +4 -4
  59. package/lib/ReactDefaultInjection.js +28 -15
  60. package/lib/ReactDefaultPerf.js +29 -12
  61. package/lib/ReactDefaultPerfAnalysis.js +5 -1
  62. package/lib/ReactDescriptor.js +243 -0
  63. package/lib/ReactDescriptorValidator.js +283 -0
  64. package/lib/ReactEmptyComponent.js +78 -0
  65. package/lib/ReactErrorUtils.js +1 -1
  66. package/lib/ReactEventEmitterMixin.js +2 -4
  67. package/lib/ReactEventListener.js +189 -0
  68. package/lib/ReactInjection.js +9 -3
  69. package/lib/ReactInputSelection.js +3 -2
  70. package/lib/ReactInstanceHandles.js +1 -1
  71. package/lib/ReactLink.js +25 -1
  72. package/lib/ReactMarkupChecksum.js +1 -1
  73. package/lib/ReactMount.js +62 -22
  74. package/lib/ReactMultiChild.js +19 -14
  75. package/lib/ReactMultiChildUpdateTypes.js +1 -1
  76. package/lib/ReactOwner.js +7 -2
  77. package/lib/ReactPerf.js +1 -1
  78. package/lib/ReactPropTransferer.js +45 -30
  79. package/lib/ReactPropTypeLocationNames.js +1 -1
  80. package/lib/ReactPropTypeLocations.js +1 -1
  81. package/lib/ReactPropTypes.js +222 -238
  82. package/lib/ReactPutListenerQueue.js +3 -3
  83. package/lib/ReactReconcileTransaction.js +22 -21
  84. package/lib/ReactRootIndex.js +1 -1
  85. package/lib/ReactServerRendering.js +42 -12
  86. package/lib/ReactServerRenderingTransaction.js +115 -0
  87. package/lib/ReactStateSetters.js +1 -1
  88. package/lib/ReactTestUtils.js +412 -0
  89. package/lib/ReactTextComponent.js +22 -14
  90. package/lib/ReactTransitionChildMapping.js +3 -3
  91. package/lib/ReactTransitionEvents.js +20 -1
  92. package/lib/ReactTransitionGroup.js +10 -7
  93. package/lib/ReactUpdates.js +140 -23
  94. package/lib/ReactWithAddons.js +13 -3
  95. package/lib/SVGDOMPropertyConfig.js +97 -0
  96. package/lib/SelectEventPlugin.js +1 -1
  97. package/lib/ServerReactRootIndex.js +1 -1
  98. package/lib/SimpleEventPlugin.js +8 -2
  99. package/lib/SyntheticClipboardEvent.js +1 -1
  100. package/lib/SyntheticCompositionEvent.js +1 -1
  101. package/lib/SyntheticDragEvent.js +1 -1
  102. package/lib/SyntheticEvent.js +1 -1
  103. package/lib/SyntheticFocusEvent.js +1 -1
  104. package/lib/SyntheticInputEvent.js +52 -0
  105. package/lib/SyntheticKeyboardEvent.js +34 -5
  106. package/lib/SyntheticMouseEvent.js +4 -1
  107. package/lib/SyntheticTouchEvent.js +5 -2
  108. package/lib/SyntheticUIEvent.js +25 -3
  109. package/lib/SyntheticWheelEvent.js +1 -1
  110. package/lib/Transaction.js +1 -33
  111. package/lib/ViewportMetrics.js +1 -1
  112. package/lib/accumulate.js +1 -1
  113. package/lib/adler32.js +1 -1
  114. package/lib/cloneWithProps.js +11 -9
  115. package/lib/containsNode.js +1 -1
  116. package/lib/copyProperties.js +1 -1
  117. package/lib/createArrayFrom.js +1 -1
  118. package/lib/createFullPageComponent.js +2 -2
  119. package/lib/createNodesFromMarkup.js +1 -1
  120. package/lib/cx.js +1 -1
  121. package/lib/dangerousStyleValue.js +12 -6
  122. package/lib/emptyFunction.js +1 -1
  123. package/lib/{ReactComponentEnvironment.js → emptyObject.js} +7 -6
  124. package/lib/escapeTextForBrowser.js +3 -4
  125. package/lib/flattenChildren.js +10 -8
  126. package/lib/focusNode.js +33 -0
  127. package/lib/forEachAccumulated.js +1 -1
  128. package/lib/getActiveElement.js +1 -1
  129. package/lib/getEventKey.js +36 -6
  130. package/lib/getEventModifierState.js +52 -0
  131. package/lib/getEventTarget.js +1 -1
  132. package/lib/getMarkupWrap.js +3 -1
  133. package/lib/getNodeForCharacterOffset.js +1 -1
  134. package/lib/getReactRootElementInContainer.js +1 -1
  135. package/lib/getTextContentAccessor.js +2 -2
  136. package/lib/getUnboundedScrollPosition.js +1 -1
  137. package/lib/hyphenate.js +4 -1
  138. package/lib/hyphenateStyleName.js +46 -0
  139. package/lib/instantiateReactComponent.js +62 -0
  140. package/lib/invariant.js +21 -23
  141. package/lib/isEventSupported.js +1 -1
  142. package/lib/isNode.js +2 -2
  143. package/lib/isTextInputElement.js +1 -1
  144. package/lib/isTextNode.js +1 -1
  145. package/lib/joinClasses.js +1 -1
  146. package/lib/keyMirror.js +1 -1
  147. package/lib/keyOf.js +1 -1
  148. package/lib/{objMap.js → mapObject.js} +9 -4
  149. package/lib/memoizeStringOnly.js +1 -1
  150. package/lib/merge.js +1 -1
  151. package/lib/mergeHelpers.js +12 -1
  152. package/lib/mergeInto.js +4 -3
  153. package/lib/mixInto.js +1 -1
  154. package/lib/monitorCodeUse.js +37 -0
  155. package/lib/onlyChild.js +4 -4
  156. package/lib/performance.js +33 -0
  157. package/lib/performanceNow.js +6 -15
  158. package/lib/setInnerHTML.js +77 -0
  159. package/lib/shallowEqual.js +1 -1
  160. package/lib/shouldUpdateReactComponent.js +15 -29
  161. package/lib/toArray.js +1 -1
  162. package/lib/traverseAllChildren.js +10 -6
  163. package/lib/update.js +171 -0
  164. package/lib/warning.js +19 -11
  165. package/package.json +5 -3
  166. package/lib/ReactEventTopLevelCallback.js +0 -109
  167. package/lib/createObjectFrom.js +0 -61
  168. package/lib/objMapKeyVal.js +0 -47
@@ -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.
@@ -19,7 +19,7 @@
19
19
  "use strict";
20
20
 
21
21
  var PooledClass = require("./PooledClass");
22
- var ReactEventEmitter = require("./ReactEventEmitter");
22
+ var ReactBrowserEventEmitter = require("./ReactBrowserEventEmitter");
23
23
 
24
24
  var mixInto = require("./mixInto");
25
25
 
@@ -39,7 +39,7 @@ mixInto(ReactPutListenerQueue, {
39
39
  putListeners: function() {
40
40
  for (var i = 0; i < this.listenersToPut.length; i++) {
41
41
  var listenerToPut = this.listenersToPut[i];
42
- ReactEventEmitter.putListener(
42
+ ReactBrowserEventEmitter.putListener(
43
43
  listenerToPut.rootNodeID,
44
44
  listenerToPut.propKey,
45
45
  listenerToPut.propValue
@@ -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.
@@ -19,11 +19,10 @@
19
19
 
20
20
  "use strict";
21
21
 
22
- var ExecutionEnvironment = require("./ExecutionEnvironment");
22
+ var CallbackQueue = require("./CallbackQueue");
23
23
  var PooledClass = require("./PooledClass");
24
- var ReactEventEmitter = require("./ReactEventEmitter");
24
+ var ReactBrowserEventEmitter = require("./ReactBrowserEventEmitter");
25
25
  var ReactInputSelection = require("./ReactInputSelection");
26
- var ReactMountReady = require("./ReactMountReady");
27
26
  var ReactPutListenerQueue = require("./ReactPutListenerQueue");
28
27
  var Transaction = require("./Transaction");
29
28
 
@@ -51,27 +50,28 @@ var SELECTION_RESTORATION = {
51
50
  */
52
51
  var EVENT_SUPPRESSION = {
53
52
  /**
54
- * @return {boolean} The enabled status of `ReactEventEmitter` before the
55
- * reconciliation.
53
+ * @return {boolean} The enabled status of `ReactBrowserEventEmitter` before
54
+ * the reconciliation.
56
55
  */
57
56
  initialize: function() {
58
- var currentlyEnabled = ReactEventEmitter.isEnabled();
59
- ReactEventEmitter.setEnabled(false);
57
+ var currentlyEnabled = ReactBrowserEventEmitter.isEnabled();
58
+ ReactBrowserEventEmitter.setEnabled(false);
60
59
  return currentlyEnabled;
61
60
  },
62
61
 
63
62
  /**
64
- * @param {boolean} previouslyEnabled Enabled status of `ReactEventEmitter`
65
- * before the reconciliation occured. `close` restores the previous value.
63
+ * @param {boolean} previouslyEnabled Enabled status of
64
+ * `ReactBrowserEventEmitter` before the reconciliation occured. `close`
65
+ * restores the previous value.
66
66
  */
67
67
  close: function(previouslyEnabled) {
68
- ReactEventEmitter.setEnabled(previouslyEnabled);
68
+ ReactBrowserEventEmitter.setEnabled(previouslyEnabled);
69
69
  }
70
70
  };
71
71
 
72
72
  /**
73
- * Provides a `ReactMountReady` queue for collecting `onDOMReady` callbacks
74
- * during the performing of the transaction.
73
+ * Provides a queue for collecting `componentDidMount` and
74
+ * `componentDidUpdate` callbacks during the the transaction.
75
75
  */
76
76
  var ON_DOM_READY_QUEUEING = {
77
77
  /**
@@ -127,7 +127,13 @@ var TRANSACTION_WRAPPERS = [
127
127
  */
128
128
  function ReactReconcileTransaction() {
129
129
  this.reinitializeTransaction();
130
- this.reactMountReady = ReactMountReady.getPooled(null);
130
+ // Only server-side rendering really needs this option (see
131
+ // `ReactServerRendering`), but server-side uses
132
+ // `ReactServerRenderingTransaction` instead. This option is here so that it's
133
+ // accessible and defaults to false when `ReactDOMComponent` and
134
+ // `ReactTextComponent` checks it in `mountComponent`.`
135
+ this.renderToStaticMarkup = false;
136
+ this.reactMountReady = CallbackQueue.getPooled(null);
131
137
  this.putListenerQueue = ReactPutListenerQueue.getPooled();
132
138
  }
133
139
 
@@ -140,16 +146,11 @@ var Mixin = {
140
146
  * TODO: convert to array<TransactionWrapper>
141
147
  */
142
148
  getTransactionWrappers: function() {
143
- if (ExecutionEnvironment.canUseDOM) {
144
- return TRANSACTION_WRAPPERS;
145
- } else {
146
- return [];
147
- }
149
+ return TRANSACTION_WRAPPERS;
148
150
  },
149
151
 
150
152
  /**
151
153
  * @return {object} The queue to collect `onDOMReady` callbacks with.
152
- * TODO: convert to ReactMountReady
153
154
  */
154
155
  getReactMountReady: function() {
155
156
  return this.reactMountReady;
@@ -164,7 +165,7 @@ var Mixin = {
164
165
  * instance to be resused.
165
166
  */
166
167
  destructor: function() {
167
- ReactMountReady.release(this.reactMountReady);
168
+ CallbackQueue.release(this.reactMountReady);
168
169
  this.reactMountReady = null;
169
170
 
170
171
  ReactPutListenerQueue.release(this.putListenerQueue);
@@ -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.
@@ -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.
@@ -18,22 +18,24 @@
18
18
  */
19
19
  "use strict";
20
20
 
21
- var ReactComponent = require("./ReactComponent");
21
+ var ReactDescriptor = require("./ReactDescriptor");
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
- * @return {string} the markup
32
+ * @return {string} the HTML markup
31
33
  */
32
34
  function renderComponentToString(component) {
33
35
  ("production" !== process.env.NODE_ENV ? invariant(
34
- ReactComponent.isValidComponent(component),
36
+ ReactDescriptor.isValidDescriptor(component),
35
37
  'renderComponentToString(): You must pass a valid ReactComponent.'
36
- ) : invariant(ReactComponent.isValidComponent(component)));
38
+ ) : invariant(ReactDescriptor.isValidDescriptor(component)));
37
39
 
38
40
  ("production" !== process.env.NODE_ENV ? invariant(
39
41
  !(arguments.length === 2 && typeof arguments[1] === 'function'),
@@ -41,19 +43,47 @@ function renderComponentToString(component) {
41
43
  'returns the generated markup. Please remove the second parameter.'
42
44
  ) : invariant(!(arguments.length === 2 && typeof arguments[1] === 'function')));
43
45
 
44
- var id = ReactInstanceHandles.createReactRootID();
45
- var transaction = ReactReconcileTransaction.getPooled();
46
- transaction.reinitializeTransaction();
46
+ var transaction;
47
47
  try {
48
+ var id = ReactInstanceHandles.createReactRootID();
49
+ transaction = ReactServerRenderingTransaction.getPooled(false);
50
+
48
51
  return transaction.perform(function() {
49
- var markup = component.mountComponent(id, transaction, 0);
52
+ var componentInstance = instantiateReactComponent(component);
53
+ var markup = componentInstance.mountComponent(id, transaction, 0);
50
54
  return ReactMarkupChecksum.addChecksumToMarkup(markup);
51
55
  }, null);
52
56
  } finally {
53
- ReactReconcileTransaction.release(transaction);
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
+ ReactDescriptor.isValidDescriptor(component),
69
+ 'renderComponentToStaticMarkup(): You must pass a valid ReactComponent.'
70
+ ) : invariant(ReactDescriptor.isValidDescriptor(component)));
71
+
72
+ var transaction;
73
+ try {
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);
80
+ }, null);
81
+ } finally {
82
+ ReactServerRenderingTransaction.release(transaction);
54
83
  }
55
84
  }
56
85
 
57
86
  module.exports = {
58
- renderComponentToString: renderComponentToString
87
+ renderComponentToString: renderComponentToString,
88
+ renderComponentToStaticMarkup: renderComponentToStaticMarkup
59
89
  };
@@ -0,0 +1,115 @@
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 CallbackQueue = require("./CallbackQueue");
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 `CallbackQueue` 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 = CallbackQueue.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
+ */
88
+ getReactMountReady: function() {
89
+ return this.reactMountReady;
90
+ },
91
+
92
+ getPutListenerQueue: function() {
93
+ return this.putListenerQueue;
94
+ },
95
+
96
+ /**
97
+ * `PooledClass` looks for this, and will invoke this before allowing this
98
+ * instance to be resused.
99
+ */
100
+ destructor: function() {
101
+ CallbackQueue.release(this.reactMountReady);
102
+ this.reactMountReady = null;
103
+
104
+ ReactPutListenerQueue.release(this.putListenerQueue);
105
+ this.putListenerQueue = null;
106
+ }
107
+ };
108
+
109
+
110
+ mixInto(ReactServerRenderingTransaction, Transaction.Mixin);
111
+ mixInto(ReactServerRenderingTransaction, Mixin);
112
+
113
+ PooledClass.addPoolingTo(ReactServerRenderingTransaction);
114
+
115
+ 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,412 @@
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 ReactDescriptor = require("./ReactDescriptor");
26
+ var ReactDOM = require("./ReactDOM");
27
+ var ReactBrowserEventEmitter = require("./ReactBrowserEventEmitter");
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
+ isDescriptor: function(descriptor) {
61
+ return ReactDescriptor.isValidDescriptor(descriptor);
62
+ },
63
+
64
+ isDescriptorOfType: function(inst, convenienceConstructor) {
65
+ return (
66
+ ReactDescriptor.isValidDescriptor(inst) &&
67
+ inst.type === convenienceConstructor.type
68
+ );
69
+ },
70
+
71
+ isDOMComponent: function(inst) {
72
+ return !!(inst && inst.mountComponent && inst.tagName);
73
+ },
74
+
75
+ isDOMComponentDescriptor: function(inst) {
76
+ return !!(inst &&
77
+ ReactDescriptor.isValidDescriptor(inst) &&
78
+ !!inst.tagName);
79
+ },
80
+
81
+ isCompositeComponent: function(inst) {
82
+ return typeof inst.render === 'function' &&
83
+ typeof inst.setState === 'function';
84
+ },
85
+
86
+ isCompositeComponentWithType: function(inst, type) {
87
+ return !!(ReactTestUtils.isCompositeComponent(inst) &&
88
+ (inst.constructor === type.type));
89
+ },
90
+
91
+ isCompositeComponentDescriptor: function(inst) {
92
+ if (!ReactDescriptor.isValidDescriptor(inst)) {
93
+ return false;
94
+ }
95
+ // We check the prototype of the type that will get mounted, not the
96
+ // instance itself. This is a future proof way of duck typing.
97
+ var prototype = inst.type.prototype;
98
+ return (
99
+ typeof prototype.render === 'function' &&
100
+ typeof prototype.setState === 'function'
101
+ );
102
+ },
103
+
104
+ isCompositeComponentDescriptorWithType: function(inst, type) {
105
+ return !!(ReactTestUtils.isCompositeComponentDescriptor(inst) &&
106
+ (inst.constructor === type));
107
+ },
108
+
109
+ isTextComponent: function(inst) {
110
+ return inst instanceof ReactTextComponent.type;
111
+ },
112
+
113
+ findAllInRenderedTree: function(inst, test) {
114
+ if (!inst) {
115
+ return [];
116
+ }
117
+ var ret = test(inst) ? [inst] : [];
118
+ if (ReactTestUtils.isDOMComponent(inst)) {
119
+ var renderedChildren = inst._renderedChildren;
120
+ var key;
121
+ for (key in renderedChildren) {
122
+ if (!renderedChildren.hasOwnProperty(key)) {
123
+ continue;
124
+ }
125
+ ret = ret.concat(
126
+ ReactTestUtils.findAllInRenderedTree(renderedChildren[key], test)
127
+ );
128
+ }
129
+ } else if (ReactTestUtils.isCompositeComponent(inst)) {
130
+ ret = ret.concat(
131
+ ReactTestUtils.findAllInRenderedTree(inst._renderedComponent, test)
132
+ );
133
+ }
134
+ return ret;
135
+ },
136
+
137
+ /**
138
+ * Finds all instance of components in the rendered tree that are DOM
139
+ * components with the class name matching `className`.
140
+ * @return an array of all the matches.
141
+ */
142
+ scryRenderedDOMComponentsWithClass: function(root, className) {
143
+ return ReactTestUtils.findAllInRenderedTree(root, function(inst) {
144
+ var instClassName = inst.props.className;
145
+ return ReactTestUtils.isDOMComponent(inst) && (
146
+ instClassName &&
147
+ (' ' + instClassName + ' ').indexOf(' ' + className + ' ') !== -1
148
+ );
149
+ });
150
+ },
151
+
152
+ /**
153
+ * Like scryRenderedDOMComponentsWithClass but expects there to be one result,
154
+ * and returns that one result, or throws exception if there is any other
155
+ * number of matches besides one.
156
+ * @return {!ReactDOMComponent} The one match.
157
+ */
158
+ findRenderedDOMComponentWithClass: function(root, className) {
159
+ var all =
160
+ ReactTestUtils.scryRenderedDOMComponentsWithClass(root, className);
161
+ if (all.length !== 1) {
162
+ throw new Error('Did not find exactly one match for class:' + className);
163
+ }
164
+ return all[0];
165
+ },
166
+
167
+
168
+ /**
169
+ * Finds all instance of components in the rendered tree that are DOM
170
+ * components with the tag name matching `tagName`.
171
+ * @return an array of all the matches.
172
+ */
173
+ scryRenderedDOMComponentsWithTag: function(root, tagName) {
174
+ return ReactTestUtils.findAllInRenderedTree(root, function(inst) {
175
+ return ReactTestUtils.isDOMComponent(inst) &&
176
+ inst.tagName === tagName.toUpperCase();
177
+ });
178
+ },
179
+
180
+ /**
181
+ * Like scryRenderedDOMComponentsWithTag but expects there to be one result,
182
+ * and returns that one result, or throws exception if there is any other
183
+ * number of matches besides one.
184
+ * @return {!ReactDOMComponent} The one match.
185
+ */
186
+ findRenderedDOMComponentWithTag: function(root, tagName) {
187
+ var all = ReactTestUtils.scryRenderedDOMComponentsWithTag(root, tagName);
188
+ if (all.length !== 1) {
189
+ throw new Error('Did not find exactly one match for tag:' + tagName);
190
+ }
191
+ return all[0];
192
+ },
193
+
194
+
195
+ /**
196
+ * Finds all instances of components with type equal to `componentType`.
197
+ * @return an array of all the matches.
198
+ */
199
+ scryRenderedComponentsWithType: function(root, componentType) {
200
+ return ReactTestUtils.findAllInRenderedTree(root, function(inst) {
201
+ return ReactTestUtils.isCompositeComponentWithType(
202
+ inst,
203
+ componentType
204
+ );
205
+ });
206
+ },
207
+
208
+ /**
209
+ * Same as `scryRenderedComponentsWithType` but expects there to be one result
210
+ * and returns that one result, or throws exception if there is any other
211
+ * number of matches besides one.
212
+ * @return {!ReactComponent} The one match.
213
+ */
214
+ findRenderedComponentWithType: function(root, componentType) {
215
+ var all = ReactTestUtils.scryRenderedComponentsWithType(
216
+ root,
217
+ componentType
218
+ );
219
+ if (all.length !== 1) {
220
+ throw new Error(
221
+ 'Did not find exactly one match for componentType:' + componentType
222
+ );
223
+ }
224
+ return all[0];
225
+ },
226
+
227
+ /**
228
+ * Pass a mocked component module to this method to augment it with
229
+ * useful methods that allow it to be used as a dummy React component.
230
+ * Instead of rendering as usual, the component will become a simple
231
+ * <div> containing any provided children.
232
+ *
233
+ * @param {object} module the mock function object exported from a
234
+ * module that defines the component to be mocked
235
+ * @param {?string} mockTagName optional dummy root tag name to return
236
+ * from render method (overrides
237
+ * module.mockTagName if provided)
238
+ * @return {object} the ReactTestUtils object (for chaining)
239
+ */
240
+ mockComponent: function(module, mockTagName) {
241
+ var ConvenienceConstructor = React.createClass({
242
+ render: function() {
243
+ var mockTagName = mockTagName || module.mockTagName || "div";
244
+ return ReactDOM[mockTagName](null, this.props.children);
245
+ }
246
+ });
247
+
248
+ copyProperties(module, ConvenienceConstructor);
249
+ module.mockImplementation(ConvenienceConstructor);
250
+
251
+ return this;
252
+ },
253
+
254
+ /**
255
+ * Simulates a top level event being dispatched from a raw event that occured
256
+ * on an `Element` node.
257
+ * @param topLevelType {Object} A type from `EventConstants.topLevelTypes`
258
+ * @param {!Element} node The dom to simulate an event occurring on.
259
+ * @param {?Event} fakeNativeEvent Fake native event to use in SyntheticEvent.
260
+ */
261
+ simulateNativeEventOnNode: function(topLevelType, node, fakeNativeEvent) {
262
+ fakeNativeEvent.target = node;
263
+ ReactBrowserEventEmitter.ReactEventListener.dispatchEvent(
264
+ topLevelType,
265
+ fakeNativeEvent
266
+ );
267
+ },
268
+
269
+ /**
270
+ * Simulates a top level event being dispatched from a raw event that occured
271
+ * on the `ReactDOMComponent` `comp`.
272
+ * @param topLevelType {Object} A type from `EventConstants.topLevelTypes`.
273
+ * @param comp {!ReactDOMComponent}
274
+ * @param {?Event} fakeNativeEvent Fake native event to use in SyntheticEvent.
275
+ */
276
+ simulateNativeEventOnDOMComponent: function(
277
+ topLevelType,
278
+ comp,
279
+ fakeNativeEvent) {
280
+ ReactTestUtils.simulateNativeEventOnNode(
281
+ topLevelType,
282
+ comp.getDOMNode(),
283
+ fakeNativeEvent
284
+ );
285
+ },
286
+
287
+ nativeTouchData: function(x, y) {
288
+ return {
289
+ touches: [
290
+ {pageX: x, pageY: y}
291
+ ]
292
+ };
293
+ },
294
+
295
+ Simulate: null,
296
+ SimulateNative: {}
297
+ };
298
+
299
+ /**
300
+ * Exports:
301
+ *
302
+ * - `ReactTestUtils.Simulate.click(Element/ReactDOMComponent)`
303
+ * - `ReactTestUtils.Simulate.mouseMove(Element/ReactDOMComponent)`
304
+ * - `ReactTestUtils.Simulate.change(Element/ReactDOMComponent)`
305
+ * - ... (All keys from event plugin `eventTypes` objects)
306
+ */
307
+ function makeSimulator(eventType) {
308
+ return function(domComponentOrNode, eventData) {
309
+ var node;
310
+ if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
311
+ node = domComponentOrNode.getDOMNode();
312
+ } else if (domComponentOrNode.tagName) {
313
+ node = domComponentOrNode;
314
+ }
315
+
316
+ var fakeNativeEvent = new Event();
317
+ fakeNativeEvent.target = node;
318
+ // We don't use SyntheticEvent.getPooled in order to not have to worry about
319
+ // properly destroying any properties assigned from `eventData` upon release
320
+ var event = new SyntheticEvent(
321
+ ReactBrowserEventEmitter.eventNameDispatchConfigs[eventType],
322
+ ReactMount.getID(node),
323
+ fakeNativeEvent
324
+ );
325
+ mergeInto(event, eventData);
326
+ EventPropagators.accumulateTwoPhaseDispatches(event);
327
+
328
+ ReactUpdates.batchedUpdates(function() {
329
+ EventPluginHub.enqueueEvents(event);
330
+ EventPluginHub.processEventQueue();
331
+ });
332
+ };
333
+ }
334
+
335
+ function buildSimulators() {
336
+ ReactTestUtils.Simulate = {};
337
+
338
+ var eventType;
339
+ for (eventType in ReactBrowserEventEmitter.eventNameDispatchConfigs) {
340
+ /**
341
+ * @param {!Element || ReactDOMComponent} domComponentOrNode
342
+ * @param {?object} eventData Fake event data to use in SyntheticEvent.
343
+ */
344
+ ReactTestUtils.Simulate[eventType] = makeSimulator(eventType);
345
+ }
346
+ }
347
+
348
+ // Rebuild ReactTestUtils.Simulate whenever event plugins are injected
349
+ var oldInjectEventPluginOrder = EventPluginHub.injection.injectEventPluginOrder;
350
+ EventPluginHub.injection.injectEventPluginOrder = function() {
351
+ oldInjectEventPluginOrder.apply(this, arguments);
352
+ buildSimulators();
353
+ };
354
+ var oldInjectEventPlugins = EventPluginHub.injection.injectEventPluginsByName;
355
+ EventPluginHub.injection.injectEventPluginsByName = function() {
356
+ oldInjectEventPlugins.apply(this, arguments);
357
+ buildSimulators();
358
+ };
359
+
360
+ buildSimulators();
361
+
362
+ /**
363
+ * Exports:
364
+ *
365
+ * - `ReactTestUtils.SimulateNative.click(Element/ReactDOMComponent)`
366
+ * - `ReactTestUtils.SimulateNative.mouseMove(Element/ReactDOMComponent)`
367
+ * - `ReactTestUtils.SimulateNative.mouseIn/ReactDOMComponent)`
368
+ * - `ReactTestUtils.SimulateNative.mouseOut(Element/ReactDOMComponent)`
369
+ * - ... (All keys from `EventConstants.topLevelTypes`)
370
+ *
371
+ * Note: Top level event types are a subset of the entire set of handler types
372
+ * (which include a broader set of "synthetic" events). For example, onDragDone
373
+ * is a synthetic event. Except when testing an event plugin or React's event
374
+ * handling code specifically, you probably want to use ReactTestUtils.Simulate
375
+ * to dispatch synthetic events.
376
+ */
377
+
378
+ function makeNativeSimulator(eventType) {
379
+ return function(domComponentOrNode, nativeEventData) {
380
+ var fakeNativeEvent = new Event(eventType);
381
+ mergeInto(fakeNativeEvent, nativeEventData);
382
+ if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
383
+ ReactTestUtils.simulateNativeEventOnDOMComponent(
384
+ eventType,
385
+ domComponentOrNode,
386
+ fakeNativeEvent
387
+ );
388
+ } else if (!!domComponentOrNode.tagName) {
389
+ // Will allow on actual dom nodes.
390
+ ReactTestUtils.simulateNativeEventOnNode(
391
+ eventType,
392
+ domComponentOrNode,
393
+ fakeNativeEvent
394
+ );
395
+ }
396
+ };
397
+ }
398
+
399
+ var eventType;
400
+ for (eventType in topLevelTypes) {
401
+ // Event type is stored as 'topClick' - we transform that to 'click'
402
+ var convenienceName = eventType.indexOf('top') === 0 ?
403
+ eventType.charAt(3).toLowerCase() + eventType.substr(4) : eventType;
404
+ /**
405
+ * @param {!Element || ReactDOMComponent} domComponentOrNode
406
+ * @param {?Event} nativeEventData Fake native event to use in SyntheticEvent.
407
+ */
408
+ ReactTestUtils.SimulateNative[convenienceName] =
409
+ makeNativeSimulator(eventType);
410
+ }
411
+
412
+ module.exports = ReactTestUtils;