1 |
3a515b92
|
cagy
|
/** @license React v16.13.1
|
2 |
|
|
* react-dom-test-utils.development.js
|
3 |
|
|
*
|
4 |
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
5 |
|
|
*
|
6 |
|
|
* This source code is licensed under the MIT license found in the
|
7 |
|
|
* LICENSE file in the root directory of this source tree.
|
8 |
|
|
*/
|
9 |
|
|
|
10 |
|
|
'use strict';
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
if (process.env.NODE_ENV !== "production") {
|
15 |
|
|
(function() {
|
16 |
|
|
'use strict';
|
17 |
|
|
|
18 |
|
|
var _assign = require('object-assign');
|
19 |
|
|
var React = require('react');
|
20 |
|
|
var ReactDOM = require('react-dom');
|
21 |
|
|
var Scheduler = require('scheduler');
|
22 |
|
|
|
23 |
|
|
var ReactSharedInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; // Prevent newer renderers from RTE when used with older react package versions.
|
24 |
|
|
// Current owner and dispatcher used to share the same ref,
|
25 |
|
|
// but PR #14548 split them out to better support the react-debug-tools package.
|
26 |
|
|
|
27 |
|
|
if (!ReactSharedInternals.hasOwnProperty('ReactCurrentDispatcher')) {
|
28 |
|
|
ReactSharedInternals.ReactCurrentDispatcher = {
|
29 |
|
|
current: null
|
30 |
|
|
};
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
if (!ReactSharedInternals.hasOwnProperty('ReactCurrentBatchConfig')) {
|
34 |
|
|
ReactSharedInternals.ReactCurrentBatchConfig = {
|
35 |
|
|
suspense: null
|
36 |
|
|
};
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
// by calls to these methods by a Babel plugin.
|
40 |
|
|
//
|
41 |
|
|
// In PROD (or in packages without access to React internals),
|
42 |
|
|
// they are left as they are instead.
|
43 |
|
|
|
44 |
|
|
function warn(format) {
|
45 |
|
|
{
|
46 |
|
|
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
47 |
|
|
args[_key - 1] = arguments[_key];
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
printWarning('warn', format, args);
|
51 |
|
|
}
|
52 |
|
|
}
|
53 |
|
|
function error(format) {
|
54 |
|
|
{
|
55 |
|
|
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
56 |
|
|
args[_key2 - 1] = arguments[_key2];
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
printWarning('error', format, args);
|
60 |
|
|
}
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
function printWarning(level, format, args) {
|
64 |
|
|
// When changing this logic, you might want to also
|
65 |
|
|
// update consoleWithStackDev.www.js as well.
|
66 |
|
|
{
|
67 |
|
|
var hasExistingStack = args.length > 0 && typeof args[args.length - 1] === 'string' && args[args.length - 1].indexOf('\n in') === 0;
|
68 |
|
|
|
69 |
|
|
if (!hasExistingStack) {
|
70 |
|
|
var ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
|
71 |
|
|
var stack = ReactDebugCurrentFrame.getStackAddendum();
|
72 |
|
|
|
73 |
|
|
if (stack !== '') {
|
74 |
|
|
format += '%s';
|
75 |
|
|
args = args.concat([stack]);
|
76 |
|
|
}
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
var argsWithFormat = args.map(function (item) {
|
80 |
|
|
return '' + item;
|
81 |
|
|
}); // Careful: RN currently depends on this prefix
|
82 |
|
|
|
83 |
|
|
argsWithFormat.unshift('Warning: ' + format); // We intentionally don't use spread (or .apply) directly because it
|
84 |
|
|
// breaks IE9: https://github.com/facebook/react/issues/13610
|
85 |
|
|
// eslint-disable-next-line react-internal/no-production-logging
|
86 |
|
|
|
87 |
|
|
Function.prototype.apply.call(console[level], console, argsWithFormat);
|
88 |
|
|
|
89 |
|
|
try {
|
90 |
|
|
// --- Welcome to debugging React ---
|
91 |
|
|
// This error was thrown as a convenience so that you can use this stack
|
92 |
|
|
// to find the callsite that caused this warning to fire.
|
93 |
|
|
var argIndex = 0;
|
94 |
|
|
var message = 'Warning: ' + format.replace(/%s/g, function () {
|
95 |
|
|
return args[argIndex++];
|
96 |
|
|
});
|
97 |
|
|
throw new Error(message);
|
98 |
|
|
} catch (x) {}
|
99 |
|
|
}
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
/**
|
103 |
|
|
* `ReactInstanceMap` maintains a mapping from a public facing stateful
|
104 |
|
|
* instance (key) and the internal representation (value). This allows public
|
105 |
|
|
* methods to accept the user facing instance as an argument and map them back
|
106 |
|
|
* to internal methods.
|
107 |
|
|
*
|
108 |
|
|
* Note that this module is currently shared and assumed to be stateless.
|
109 |
|
|
* If this becomes an actual Map, that will break.
|
110 |
|
|
*/
|
111 |
|
|
function get(key) {
|
112 |
|
|
return key._reactInternalFiber;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
var FunctionComponent = 0;
|
116 |
|
|
var ClassComponent = 1;
|
117 |
|
|
|
118 |
|
|
var HostRoot = 3; // Root of a host tree. Could be nested inside another node.
|
119 |
|
|
|
120 |
|
|
var HostComponent = 5;
|
121 |
|
|
var HostText = 6;
|
122 |
|
|
|
123 |
|
|
// Don't change these two values. They're used by React Dev Tools.
|
124 |
|
|
var NoEffect =
|
125 |
|
|
/* */
|
126 |
|
|
0;
|
127 |
|
|
|
128 |
|
|
var Placement =
|
129 |
|
|
/* */
|
130 |
|
|
2;
|
131 |
|
|
var Hydrating =
|
132 |
|
|
/* */
|
133 |
|
|
1024;
|
134 |
|
|
|
135 |
|
|
var ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
|
136 |
|
|
function getNearestMountedFiber(fiber) {
|
137 |
|
|
var node = fiber;
|
138 |
|
|
var nearestMounted = fiber;
|
139 |
|
|
|
140 |
|
|
if (!fiber.alternate) {
|
141 |
|
|
// If there is no alternate, this might be a new tree that isn't inserted
|
142 |
|
|
// yet. If it is, then it will have a pending insertion effect on it.
|
143 |
|
|
var nextNode = node;
|
144 |
|
|
|
145 |
|
|
do {
|
146 |
|
|
node = nextNode;
|
147 |
|
|
|
148 |
|
|
if ((node.effectTag & (Placement | Hydrating)) !== NoEffect) {
|
149 |
|
|
// This is an insertion or in-progress hydration. The nearest possible
|
150 |
|
|
// mounted fiber is the parent but we need to continue to figure out
|
151 |
|
|
// if that one is still mounted.
|
152 |
|
|
nearestMounted = node.return;
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
nextNode = node.return;
|
156 |
|
|
} while (nextNode);
|
157 |
|
|
} else {
|
158 |
|
|
while (node.return) {
|
159 |
|
|
node = node.return;
|
160 |
|
|
}
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
if (node.tag === HostRoot) {
|
164 |
|
|
// TODO: Check if this was a nested HostRoot when used with
|
165 |
|
|
// renderContainerIntoSubtree.
|
166 |
|
|
return nearestMounted;
|
167 |
|
|
} // If we didn't hit the root, that means that we're in an disconnected tree
|
168 |
|
|
// that has been unmounted.
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
return null;
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
function assertIsMounted(fiber) {
|
175 |
|
|
if (!(getNearestMountedFiber(fiber) === fiber)) {
|
176 |
|
|
{
|
177 |
|
|
throw Error( "Unable to find node on an unmounted component." );
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
function findCurrentFiberUsingSlowPath(fiber) {
|
183 |
|
|
var alternate = fiber.alternate;
|
184 |
|
|
|
185 |
|
|
if (!alternate) {
|
186 |
|
|
// If there is no alternate, then we only need to check if it is mounted.
|
187 |
|
|
var nearestMounted = getNearestMountedFiber(fiber);
|
188 |
|
|
|
189 |
|
|
if (!(nearestMounted !== null)) {
|
190 |
|
|
{
|
191 |
|
|
throw Error( "Unable to find node on an unmounted component." );
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
if (nearestMounted !== fiber) {
|
196 |
|
|
return null;
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
return fiber;
|
200 |
|
|
} // If we have two possible branches, we'll walk backwards up to the root
|
201 |
|
|
// to see what path the root points to. On the way we may hit one of the
|
202 |
|
|
// special cases and we'll deal with them.
|
203 |
|
|
|
204 |
|
|
|
205 |
|
|
var a = fiber;
|
206 |
|
|
var b = alternate;
|
207 |
|
|
|
208 |
|
|
while (true) {
|
209 |
|
|
var parentA = a.return;
|
210 |
|
|
|
211 |
|
|
if (parentA === null) {
|
212 |
|
|
// We're at the root.
|
213 |
|
|
break;
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
var parentB = parentA.alternate;
|
217 |
|
|
|
218 |
|
|
if (parentB === null) {
|
219 |
|
|
// There is no alternate. This is an unusual case. Currently, it only
|
220 |
|
|
// happens when a Suspense component is hidden. An extra fragment fiber
|
221 |
|
|
// is inserted in between the Suspense fiber and its children. Skip
|
222 |
|
|
// over this extra fragment fiber and proceed to the next parent.
|
223 |
|
|
var nextParent = parentA.return;
|
224 |
|
|
|
225 |
|
|
if (nextParent !== null) {
|
226 |
|
|
a = b = nextParent;
|
227 |
|
|
continue;
|
228 |
|
|
} // If there's no parent, we're at the root.
|
229 |
|
|
|
230 |
|
|
|
231 |
|
|
break;
|
232 |
|
|
} // If both copies of the parent fiber point to the same child, we can
|
233 |
|
|
// assume that the child is current. This happens when we bailout on low
|
234 |
|
|
// priority: the bailed out fiber's child reuses the current child.
|
235 |
|
|
|
236 |
|
|
|
237 |
|
|
if (parentA.child === parentB.child) {
|
238 |
|
|
var child = parentA.child;
|
239 |
|
|
|
240 |
|
|
while (child) {
|
241 |
|
|
if (child === a) {
|
242 |
|
|
// We've determined that A is the current branch.
|
243 |
|
|
assertIsMounted(parentA);
|
244 |
|
|
return fiber;
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
if (child === b) {
|
248 |
|
|
// We've determined that B is the current branch.
|
249 |
|
|
assertIsMounted(parentA);
|
250 |
|
|
return alternate;
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
child = child.sibling;
|
254 |
|
|
} // We should never have an alternate for any mounting node. So the only
|
255 |
|
|
// way this could possibly happen is if this was unmounted, if at all.
|
256 |
|
|
|
257 |
|
|
|
258 |
|
|
{
|
259 |
|
|
{
|
260 |
|
|
throw Error( "Unable to find node on an unmounted component." );
|
261 |
|
|
}
|
262 |
|
|
}
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
if (a.return !== b.return) {
|
266 |
|
|
// The return pointer of A and the return pointer of B point to different
|
267 |
|
|
// fibers. We assume that return pointers never criss-cross, so A must
|
268 |
|
|
// belong to the child set of A.return, and B must belong to the child
|
269 |
|
|
// set of B.return.
|
270 |
|
|
a = parentA;
|
271 |
|
|
b = parentB;
|
272 |
|
|
} else {
|
273 |
|
|
// The return pointers point to the same fiber. We'll have to use the
|
274 |
|
|
// default, slow path: scan the child sets of each parent alternate to see
|
275 |
|
|
// which child belongs to which set.
|
276 |
|
|
//
|
277 |
|
|
// Search parent A's child set
|
278 |
|
|
var didFindChild = false;
|
279 |
|
|
var _child = parentA.child;
|
280 |
|
|
|
281 |
|
|
while (_child) {
|
282 |
|
|
if (_child === a) {
|
283 |
|
|
didFindChild = true;
|
284 |
|
|
a = parentA;
|
285 |
|
|
b = parentB;
|
286 |
|
|
break;
|
287 |
|
|
}
|
288 |
|
|
|
289 |
|
|
if (_child === b) {
|
290 |
|
|
didFindChild = true;
|
291 |
|
|
b = parentA;
|
292 |
|
|
a = parentB;
|
293 |
|
|
break;
|
294 |
|
|
}
|
295 |
|
|
|
296 |
|
|
_child = _child.sibling;
|
297 |
|
|
}
|
298 |
|
|
|
299 |
|
|
if (!didFindChild) {
|
300 |
|
|
// Search parent B's child set
|
301 |
|
|
_child = parentB.child;
|
302 |
|
|
|
303 |
|
|
while (_child) {
|
304 |
|
|
if (_child === a) {
|
305 |
|
|
didFindChild = true;
|
306 |
|
|
a = parentB;
|
307 |
|
|
b = parentA;
|
308 |
|
|
break;
|
309 |
|
|
}
|
310 |
|
|
|
311 |
|
|
if (_child === b) {
|
312 |
|
|
didFindChild = true;
|
313 |
|
|
b = parentB;
|
314 |
|
|
a = parentA;
|
315 |
|
|
break;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
_child = _child.sibling;
|
319 |
|
|
}
|
320 |
|
|
|
321 |
|
|
if (!didFindChild) {
|
322 |
|
|
{
|
323 |
|
|
throw Error( "Child was not found in either parent set. This indicates a bug in React related to the return pointer. Please file an issue." );
|
324 |
|
|
}
|
325 |
|
|
}
|
326 |
|
|
}
|
327 |
|
|
}
|
328 |
|
|
|
329 |
|
|
if (!(a.alternate === b)) {
|
330 |
|
|
{
|
331 |
|
|
throw Error( "Return fibers should always be each others' alternates. This error is likely caused by a bug in React. Please file an issue." );
|
332 |
|
|
}
|
333 |
|
|
}
|
334 |
|
|
} // If the root is not a host container, we're in a disconnected tree. I.e.
|
335 |
|
|
// unmounted.
|
336 |
|
|
|
337 |
|
|
|
338 |
|
|
if (!(a.tag === HostRoot)) {
|
339 |
|
|
{
|
340 |
|
|
throw Error( "Unable to find node on an unmounted component." );
|
341 |
|
|
}
|
342 |
|
|
}
|
343 |
|
|
|
344 |
|
|
if (a.stateNode.current === a) {
|
345 |
|
|
// We've determined that A is the current branch.
|
346 |
|
|
return fiber;
|
347 |
|
|
} // Otherwise B has to be current branch.
|
348 |
|
|
|
349 |
|
|
|
350 |
|
|
return alternate;
|
351 |
|
|
}
|
352 |
|
|
|
353 |
|
|
var EVENT_POOL_SIZE = 10;
|
354 |
|
|
/**
|
355 |
|
|
* @interface Event
|
356 |
|
|
* @see http://www.w3.org/TR/DOM-Level-3-Events/
|
357 |
|
|
*/
|
358 |
|
|
|
359 |
|
|
var EventInterface = {
|
360 |
|
|
type: null,
|
361 |
|
|
target: null,
|
362 |
|
|
// currentTarget is set when dispatching; no use in copying it here
|
363 |
|
|
currentTarget: function () {
|
364 |
|
|
return null;
|
365 |
|
|
},
|
366 |
|
|
eventPhase: null,
|
367 |
|
|
bubbles: null,
|
368 |
|
|
cancelable: null,
|
369 |
|
|
timeStamp: function (event) {
|
370 |
|
|
return event.timeStamp || Date.now();
|
371 |
|
|
},
|
372 |
|
|
defaultPrevented: null,
|
373 |
|
|
isTrusted: null
|
374 |
|
|
};
|
375 |
|
|
|
376 |
|
|
function functionThatReturnsTrue() {
|
377 |
|
|
return true;
|
378 |
|
|
}
|
379 |
|
|
|
380 |
|
|
function functionThatReturnsFalse() {
|
381 |
|
|
return false;
|
382 |
|
|
}
|
383 |
|
|
/**
|
384 |
|
|
* Synthetic events are dispatched by event plugins, typically in response to a
|
385 |
|
|
* top-level event delegation handler.
|
386 |
|
|
*
|
387 |
|
|
* These systems should generally use pooling to reduce the frequency of garbage
|
388 |
|
|
* collection. The system should check `isPersistent` to determine whether the
|
389 |
|
|
* event should be released into the pool after being dispatched. Users that
|
390 |
|
|
* need a persisted event should invoke `persist`.
|
391 |
|
|
*
|
392 |
|
|
* Synthetic events (and subclasses) implement the DOM Level 3 Events API by
|
393 |
|
|
* normalizing browser quirks. Subclasses do not necessarily have to implement a
|
394 |
|
|
* DOM interface; custom application-specific events can also subclass this.
|
395 |
|
|
*
|
396 |
|
|
* @param {object} dispatchConfig Configuration used to dispatch this event.
|
397 |
|
|
* @param {*} targetInst Marker identifying the event target.
|
398 |
|
|
* @param {object} nativeEvent Native browser event.
|
399 |
|
|
* @param {DOMEventTarget} nativeEventTarget Target node.
|
400 |
|
|
*/
|
401 |
|
|
|
402 |
|
|
|
403 |
|
|
function SyntheticEvent(dispatchConfig, targetInst, nativeEvent, nativeEventTarget) {
|
404 |
|
|
{
|
405 |
|
|
// these have a getter/setter for warnings
|
406 |
|
|
delete this.nativeEvent;
|
407 |
|
|
delete this.preventDefault;
|
408 |
|
|
delete this.stopPropagation;
|
409 |
|
|
delete this.isDefaultPrevented;
|
410 |
|
|
delete this.isPropagationStopped;
|
411 |
|
|
}
|
412 |
|
|
|
413 |
|
|
this.dispatchConfig = dispatchConfig;
|
414 |
|
|
this._targetInst = targetInst;
|
415 |
|
|
this.nativeEvent = nativeEvent;
|
416 |
|
|
var Interface = this.constructor.Interface;
|
417 |
|
|
|
418 |
|
|
for (var propName in Interface) {
|
419 |
|
|
if (!Interface.hasOwnProperty(propName)) {
|
420 |
|
|
continue;
|
421 |
|
|
}
|
422 |
|
|
|
423 |
|
|
{
|
424 |
|
|
delete this[propName]; // this has a getter/setter for warnings
|
425 |
|
|
}
|
426 |
|
|
|
427 |
|
|
var normalize = Interface[propName];
|
428 |
|
|
|
429 |
|
|
if (normalize) {
|
430 |
|
|
this[propName] = normalize(nativeEvent);
|
431 |
|
|
} else {
|
432 |
|
|
if (propName === 'target') {
|
433 |
|
|
this.target = nativeEventTarget;
|
434 |
|
|
} else {
|
435 |
|
|
this[propName] = nativeEvent[propName];
|
436 |
|
|
}
|
437 |
|
|
}
|
438 |
|
|
}
|
439 |
|
|
|
440 |
|
|
var defaultPrevented = nativeEvent.defaultPrevented != null ? nativeEvent.defaultPrevented : nativeEvent.returnValue === false;
|
441 |
|
|
|
442 |
|
|
if (defaultPrevented) {
|
443 |
|
|
this.isDefaultPrevented = functionThatReturnsTrue;
|
444 |
|
|
} else {
|
445 |
|
|
this.isDefaultPrevented = functionThatReturnsFalse;
|
446 |
|
|
}
|
447 |
|
|
|
448 |
|
|
this.isPropagationStopped = functionThatReturnsFalse;
|
449 |
|
|
return this;
|
450 |
|
|
}
|
451 |
|
|
|
452 |
|
|
_assign(SyntheticEvent.prototype, {
|
453 |
|
|
preventDefault: function () {
|
454 |
|
|
this.defaultPrevented = true;
|
455 |
|
|
var event = this.nativeEvent;
|
456 |
|
|
|
457 |
|
|
if (!event) {
|
458 |
|
|
return;
|
459 |
|
|
}
|
460 |
|
|
|
461 |
|
|
if (event.preventDefault) {
|
462 |
|
|
event.preventDefault();
|
463 |
|
|
} else if (typeof event.returnValue !== 'unknown') {
|
464 |
|
|
event.returnValue = false;
|
465 |
|
|
}
|
466 |
|
|
|
467 |
|
|
this.isDefaultPrevented = functionThatReturnsTrue;
|
468 |
|
|
},
|
469 |
|
|
stopPropagation: function () {
|
470 |
|
|
var event = this.nativeEvent;
|
471 |
|
|
|
472 |
|
|
if (!event) {
|
473 |
|
|
return;
|
474 |
|
|
}
|
475 |
|
|
|
476 |
|
|
if (event.stopPropagation) {
|
477 |
|
|
event.stopPropagation();
|
478 |
|
|
} else if (typeof event.cancelBubble !== 'unknown') {
|
479 |
|
|
// The ChangeEventPlugin registers a "propertychange" event for
|
480 |
|
|
// IE. This event does not support bubbling or cancelling, and
|
481 |
|
|
// any references to cancelBubble throw "Member not found". A
|
482 |
|
|
// typeof check of "unknown" circumvents this issue (and is also
|
483 |
|
|
// IE specific).
|
484 |
|
|
event.cancelBubble = true;
|
485 |
|
|
}
|
486 |
|
|
|
487 |
|
|
this.isPropagationStopped = functionThatReturnsTrue;
|
488 |
|
|
},
|
489 |
|
|
|
490 |
|
|
/**
|
491 |
|
|
* We release all dispatched `SyntheticEvent`s after each event loop, adding
|
492 |
|
|
* them back into the pool. This allows a way to hold onto a reference that
|
493 |
|
|
* won't be added back into the pool.
|
494 |
|
|
*/
|
495 |
|
|
persist: function () {
|
496 |
|
|
this.isPersistent = functionThatReturnsTrue;
|
497 |
|
|
},
|
498 |
|
|
|
499 |
|
|
/**
|
500 |
|
|
* Checks if this event should be released back into the pool.
|
501 |
|
|
*
|
502 |
|
|
* @return {boolean} True if this should not be released, false otherwise.
|
503 |
|
|
*/
|
504 |
|
|
isPersistent: functionThatReturnsFalse,
|
505 |
|
|
|
506 |
|
|
/**
|
507 |
|
|
* `PooledClass` looks for `destructor` on each instance it releases.
|
508 |
|
|
*/
|
509 |
|
|
destructor: function () {
|
510 |
|
|
var Interface = this.constructor.Interface;
|
511 |
|
|
|
512 |
|
|
for (var propName in Interface) {
|
513 |
|
|
{
|
514 |
|
|
Object.defineProperty(this, propName, getPooledWarningPropertyDefinition(propName, Interface[propName]));
|
515 |
|
|
}
|
516 |
|
|
}
|
517 |
|
|
|
518 |
|
|
this.dispatchConfig = null;
|
519 |
|
|
this._targetInst = null;
|
520 |
|
|
this.nativeEvent = null;
|
521 |
|
|
this.isDefaultPrevented = functionThatReturnsFalse;
|
522 |
|
|
this.isPropagationStopped = functionThatReturnsFalse;
|
523 |
|
|
this._dispatchListeners = null;
|
524 |
|
|
this._dispatchInstances = null;
|
525 |
|
|
|
526 |
|
|
{
|
527 |
|
|
Object.defineProperty(this, 'nativeEvent', getPooledWarningPropertyDefinition('nativeEvent', null));
|
528 |
|
|
Object.defineProperty(this, 'isDefaultPrevented', getPooledWarningPropertyDefinition('isDefaultPrevented', functionThatReturnsFalse));
|
529 |
|
|
Object.defineProperty(this, 'isPropagationStopped', getPooledWarningPropertyDefinition('isPropagationStopped', functionThatReturnsFalse));
|
530 |
|
|
Object.defineProperty(this, 'preventDefault', getPooledWarningPropertyDefinition('preventDefault', function () {}));
|
531 |
|
|
Object.defineProperty(this, 'stopPropagation', getPooledWarningPropertyDefinition('stopPropagation', function () {}));
|
532 |
|
|
}
|
533 |
|
|
}
|
534 |
|
|
});
|
535 |
|
|
|
536 |
|
|
SyntheticEvent.Interface = EventInterface;
|
537 |
|
|
/**
|
538 |
|
|
* Helper to reduce boilerplate when creating subclasses.
|
539 |
|
|
*/
|
540 |
|
|
|
541 |
|
|
SyntheticEvent.extend = function (Interface) {
|
542 |
|
|
var Super = this;
|
543 |
|
|
|
544 |
|
|
var E = function () {};
|
545 |
|
|
|
546 |
|
|
E.prototype = Super.prototype;
|
547 |
|
|
var prototype = new E();
|
548 |
|
|
|
549 |
|
|
function Class() {
|
550 |
|
|
return Super.apply(this, arguments);
|
551 |
|
|
}
|
552 |
|
|
|
553 |
|
|
_assign(prototype, Class.prototype);
|
554 |
|
|
|
555 |
|
|
Class.prototype = prototype;
|
556 |
|
|
Class.prototype.constructor = Class;
|
557 |
|
|
Class.Interface = _assign({}, Super.Interface, Interface);
|
558 |
|
|
Class.extend = Super.extend;
|
559 |
|
|
addEventPoolingTo(Class);
|
560 |
|
|
return Class;
|
561 |
|
|
};
|
562 |
|
|
|
563 |
|
|
addEventPoolingTo(SyntheticEvent);
|
564 |
|
|
/**
|
565 |
|
|
* Helper to nullify syntheticEvent instance properties when destructing
|
566 |
|
|
*
|
567 |
|
|
* @param {String} propName
|
568 |
|
|
* @param {?object} getVal
|
569 |
|
|
* @return {object} defineProperty object
|
570 |
|
|
*/
|
571 |
|
|
|
572 |
|
|
function getPooledWarningPropertyDefinition(propName, getVal) {
|
573 |
|
|
var isFunction = typeof getVal === 'function';
|
574 |
|
|
return {
|
575 |
|
|
configurable: true,
|
576 |
|
|
set: set,
|
577 |
|
|
get: get
|
578 |
|
|
};
|
579 |
|
|
|
580 |
|
|
function set(val) {
|
581 |
|
|
var action = isFunction ? 'setting the method' : 'setting the property';
|
582 |
|
|
warn(action, 'This is effectively a no-op');
|
583 |
|
|
return val;
|
584 |
|
|
}
|
585 |
|
|
|
586 |
|
|
function get() {
|
587 |
|
|
var action = isFunction ? 'accessing the method' : 'accessing the property';
|
588 |
|
|
var result = isFunction ? 'This is a no-op function' : 'This is set to null';
|
589 |
|
|
warn(action, result);
|
590 |
|
|
return getVal;
|
591 |
|
|
}
|
592 |
|
|
|
593 |
|
|
function warn(action, result) {
|
594 |
|
|
{
|
595 |
|
|
error("This synthetic event is reused for performance reasons. If you're seeing this, " + "you're %s `%s` on a released/nullified synthetic event. %s. " + 'If you must keep the original synthetic event around, use event.persist(). ' + 'See https://fb.me/react-event-pooling for more information.', action, propName, result);
|
596 |
|
|
}
|
597 |
|
|
}
|
598 |
|
|
}
|
599 |
|
|
|
600 |
|
|
function getPooledEvent(dispatchConfig, targetInst, nativeEvent, nativeInst) {
|
601 |
|
|
var EventConstructor = this;
|
602 |
|
|
|
603 |
|
|
if (EventConstructor.eventPool.length) {
|
604 |
|
|
var instance = EventConstructor.eventPool.pop();
|
605 |
|
|
EventConstructor.call(instance, dispatchConfig, targetInst, nativeEvent, nativeInst);
|
606 |
|
|
return instance;
|
607 |
|
|
}
|
608 |
|
|
|
609 |
|
|
return new EventConstructor(dispatchConfig, targetInst, nativeEvent, nativeInst);
|
610 |
|
|
}
|
611 |
|
|
|
612 |
|
|
function releasePooledEvent(event) {
|
613 |
|
|
var EventConstructor = this;
|
614 |
|
|
|
615 |
|
|
if (!(event instanceof EventConstructor)) {
|
616 |
|
|
{
|
617 |
|
|
throw Error( "Trying to release an event instance into a pool of a different type." );
|
618 |
|
|
}
|
619 |
|
|
}
|
620 |
|
|
|
621 |
|
|
event.destructor();
|
622 |
|
|
|
623 |
|
|
if (EventConstructor.eventPool.length < EVENT_POOL_SIZE) {
|
624 |
|
|
EventConstructor.eventPool.push(event);
|
625 |
|
|
}
|
626 |
|
|
}
|
627 |
|
|
|
628 |
|
|
function addEventPoolingTo(EventConstructor) {
|
629 |
|
|
EventConstructor.eventPool = [];
|
630 |
|
|
EventConstructor.getPooled = getPooledEvent;
|
631 |
|
|
EventConstructor.release = releasePooledEvent;
|
632 |
|
|
}
|
633 |
|
|
|
634 |
|
|
/**
|
635 |
|
|
* HTML nodeType values that represent the type of the node
|
636 |
|
|
*/
|
637 |
|
|
var ELEMENT_NODE = 1;
|
638 |
|
|
|
639 |
|
|
// Do not use the below two methods directly!
|
640 |
|
|
// Instead use constants exported from DOMTopLevelEventTypes in ReactDOM.
|
641 |
|
|
// (It is the only module that is allowed to access these methods.)
|
642 |
|
|
function unsafeCastStringToDOMTopLevelType(topLevelType) {
|
643 |
|
|
return topLevelType;
|
644 |
|
|
}
|
645 |
|
|
|
646 |
|
|
var canUseDOM = !!(typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined');
|
647 |
|
|
|
648 |
|
|
/**
|
649 |
|
|
* Generate a mapping of standard vendor prefixes using the defined style property and event name.
|
650 |
|
|
*
|
651 |
|
|
* @param {string} styleProp
|
652 |
|
|
* @param {string} eventName
|
653 |
|
|
* @returns {object}
|
654 |
|
|
*/
|
655 |
|
|
|
656 |
|
|
function makePrefixMap(styleProp, eventName) {
|
657 |
|
|
var prefixes = {};
|
658 |
|
|
prefixes[styleProp.toLowerCase()] = eventName.toLowerCase();
|
659 |
|
|
prefixes['Webkit' + styleProp] = 'webkit' + eventName;
|
660 |
|
|
prefixes['Moz' + styleProp] = 'moz' + eventName;
|
661 |
|
|
return prefixes;
|
662 |
|
|
}
|
663 |
|
|
/**
|
664 |
|
|
* A list of event names to a configurable list of vendor prefixes.
|
665 |
|
|
*/
|
666 |
|
|
|
667 |
|
|
|
668 |
|
|
var vendorPrefixes = {
|
669 |
|
|
animationend: makePrefixMap('Animation', 'AnimationEnd'),
|
670 |
|
|
animationiteration: makePrefixMap('Animation', 'AnimationIteration'),
|
671 |
|
|
animationstart: makePrefixMap('Animation', 'AnimationStart'),
|
672 |
|
|
transitionend: makePrefixMap('Transition', 'TransitionEnd')
|
673 |
|
|
};
|
674 |
|
|
/**
|
675 |
|
|
* Event names that have already been detected and prefixed (if applicable).
|
676 |
|
|
*/
|
677 |
|
|
|
678 |
|
|
var prefixedEventNames = {};
|
679 |
|
|
/**
|
680 |
|
|
* Element to check for prefixes on.
|
681 |
|
|
*/
|
682 |
|
|
|
683 |
|
|
var style = {};
|
684 |
|
|
/**
|
685 |
|
|
* Bootstrap if a DOM exists.
|
686 |
|
|
*/
|
687 |
|
|
|
688 |
|
|
if (canUseDOM) {
|
689 |
|
|
style = document.createElement('div').style; // On some platforms, in particular some releases of Android 4.x,
|
690 |
|
|
// the un-prefixed "animation" and "transition" properties are defined on the
|
691 |
|
|
// style object but the events that fire will still be prefixed, so we need
|
692 |
|
|
// to check if the un-prefixed events are usable, and if not remove them from the map.
|
693 |
|
|
|
694 |
|
|
if (!('AnimationEvent' in window)) {
|
695 |
|
|
delete vendorPrefixes.animationend.animation;
|
696 |
|
|
delete vendorPrefixes.animationiteration.animation;
|
697 |
|
|
delete vendorPrefixes.animationstart.animation;
|
698 |
|
|
} // Same as above
|
699 |
|
|
|
700 |
|
|
|
701 |
|
|
if (!('TransitionEvent' in window)) {
|
702 |
|
|
delete vendorPrefixes.transitionend.transition;
|
703 |
|
|
}
|
704 |
|
|
}
|
705 |
|
|
/**
|
706 |
|
|
* Attempts to determine the correct vendor prefixed event name.
|
707 |
|
|
*
|
708 |
|
|
* @param {string} eventName
|
709 |
|
|
* @returns {string}
|
710 |
|
|
*/
|
711 |
|
|
|
712 |
|
|
|
713 |
|
|
function getVendorPrefixedEventName(eventName) {
|
714 |
|
|
if (prefixedEventNames[eventName]) {
|
715 |
|
|
return prefixedEventNames[eventName];
|
716 |
|
|
} else if (!vendorPrefixes[eventName]) {
|
717 |
|
|
return eventName;
|
718 |
|
|
}
|
719 |
|
|
|
720 |
|
|
var prefixMap = vendorPrefixes[eventName];
|
721 |
|
|
|
722 |
|
|
for (var styleProp in prefixMap) {
|
723 |
|
|
if (prefixMap.hasOwnProperty(styleProp) && styleProp in style) {
|
724 |
|
|
return prefixedEventNames[eventName] = prefixMap[styleProp];
|
725 |
|
|
}
|
726 |
|
|
}
|
727 |
|
|
|
728 |
|
|
return eventName;
|
729 |
|
|
}
|
730 |
|
|
|
731 |
|
|
/**
|
732 |
|
|
* To identify top level events in ReactDOM, we use constants defined by this
|
733 |
|
|
* module. This is the only module that uses the unsafe* methods to express
|
734 |
|
|
* that the constants actually correspond to the browser event names. This lets
|
735 |
|
|
* us save some bundle size by avoiding a top level type -> event name map.
|
736 |
|
|
* The rest of ReactDOM code should import top level types from this file.
|
737 |
|
|
*/
|
738 |
|
|
|
739 |
|
|
var TOP_ABORT = unsafeCastStringToDOMTopLevelType('abort');
|
740 |
|
|
var TOP_ANIMATION_END = unsafeCastStringToDOMTopLevelType(getVendorPrefixedEventName('animationend'));
|
741 |
|
|
var TOP_ANIMATION_ITERATION = unsafeCastStringToDOMTopLevelType(getVendorPrefixedEventName('animationiteration'));
|
742 |
|
|
var TOP_ANIMATION_START = unsafeCastStringToDOMTopLevelType(getVendorPrefixedEventName('animationstart'));
|
743 |
|
|
var TOP_BLUR = unsafeCastStringToDOMTopLevelType('blur');
|
744 |
|
|
var TOP_CAN_PLAY = unsafeCastStringToDOMTopLevelType('canplay');
|
745 |
|
|
var TOP_CAN_PLAY_THROUGH = unsafeCastStringToDOMTopLevelType('canplaythrough');
|
746 |
|
|
var TOP_CANCEL = unsafeCastStringToDOMTopLevelType('cancel');
|
747 |
|
|
var TOP_CHANGE = unsafeCastStringToDOMTopLevelType('change');
|
748 |
|
|
var TOP_CLICK = unsafeCastStringToDOMTopLevelType('click');
|
749 |
|
|
var TOP_CLOSE = unsafeCastStringToDOMTopLevelType('close');
|
750 |
|
|
var TOP_COMPOSITION_END = unsafeCastStringToDOMTopLevelType('compositionend');
|
751 |
|
|
var TOP_COMPOSITION_START = unsafeCastStringToDOMTopLevelType('compositionstart');
|
752 |
|
|
var TOP_COMPOSITION_UPDATE = unsafeCastStringToDOMTopLevelType('compositionupdate');
|
753 |
|
|
var TOP_CONTEXT_MENU = unsafeCastStringToDOMTopLevelType('contextmenu');
|
754 |
|
|
var TOP_COPY = unsafeCastStringToDOMTopLevelType('copy');
|
755 |
|
|
var TOP_CUT = unsafeCastStringToDOMTopLevelType('cut');
|
756 |
|
|
var TOP_DOUBLE_CLICK = unsafeCastStringToDOMTopLevelType('dblclick');
|
757 |
|
|
var TOP_DRAG = unsafeCastStringToDOMTopLevelType('drag');
|
758 |
|
|
var TOP_DRAG_END = unsafeCastStringToDOMTopLevelType('dragend');
|
759 |
|
|
var TOP_DRAG_ENTER = unsafeCastStringToDOMTopLevelType('dragenter');
|
760 |
|
|
var TOP_DRAG_EXIT = unsafeCastStringToDOMTopLevelType('dragexit');
|
761 |
|
|
var TOP_DRAG_LEAVE = unsafeCastStringToDOMTopLevelType('dragleave');
|
762 |
|
|
var TOP_DRAG_OVER = unsafeCastStringToDOMTopLevelType('dragover');
|
763 |
|
|
var TOP_DRAG_START = unsafeCastStringToDOMTopLevelType('dragstart');
|
764 |
|
|
var TOP_DROP = unsafeCastStringToDOMTopLevelType('drop');
|
765 |
|
|
var TOP_DURATION_CHANGE = unsafeCastStringToDOMTopLevelType('durationchange');
|
766 |
|
|
var TOP_EMPTIED = unsafeCastStringToDOMTopLevelType('emptied');
|
767 |
|
|
var TOP_ENCRYPTED = unsafeCastStringToDOMTopLevelType('encrypted');
|
768 |
|
|
var TOP_ENDED = unsafeCastStringToDOMTopLevelType('ended');
|
769 |
|
|
var TOP_ERROR = unsafeCastStringToDOMTopLevelType('error');
|
770 |
|
|
var TOP_FOCUS = unsafeCastStringToDOMTopLevelType('focus');
|
771 |
|
|
var TOP_INPUT = unsafeCastStringToDOMTopLevelType('input');
|
772 |
|
|
var TOP_KEY_DOWN = unsafeCastStringToDOMTopLevelType('keydown');
|
773 |
|
|
var TOP_KEY_PRESS = unsafeCastStringToDOMTopLevelType('keypress');
|
774 |
|
|
var TOP_KEY_UP = unsafeCastStringToDOMTopLevelType('keyup');
|
775 |
|
|
var TOP_LOAD = unsafeCastStringToDOMTopLevelType('load');
|
776 |
|
|
var TOP_LOAD_START = unsafeCastStringToDOMTopLevelType('loadstart');
|
777 |
|
|
var TOP_LOADED_DATA = unsafeCastStringToDOMTopLevelType('loadeddata');
|
778 |
|
|
var TOP_LOADED_METADATA = unsafeCastStringToDOMTopLevelType('loadedmetadata');
|
779 |
|
|
var TOP_MOUSE_DOWN = unsafeCastStringToDOMTopLevelType('mousedown');
|
780 |
|
|
var TOP_MOUSE_MOVE = unsafeCastStringToDOMTopLevelType('mousemove');
|
781 |
|
|
var TOP_MOUSE_OUT = unsafeCastStringToDOMTopLevelType('mouseout');
|
782 |
|
|
var TOP_MOUSE_OVER = unsafeCastStringToDOMTopLevelType('mouseover');
|
783 |
|
|
var TOP_MOUSE_UP = unsafeCastStringToDOMTopLevelType('mouseup');
|
784 |
|
|
var TOP_PASTE = unsafeCastStringToDOMTopLevelType('paste');
|
785 |
|
|
var TOP_PAUSE = unsafeCastStringToDOMTopLevelType('pause');
|
786 |
|
|
var TOP_PLAY = unsafeCastStringToDOMTopLevelType('play');
|
787 |
|
|
var TOP_PLAYING = unsafeCastStringToDOMTopLevelType('playing');
|
788 |
|
|
var TOP_PROGRESS = unsafeCastStringToDOMTopLevelType('progress');
|
789 |
|
|
var TOP_RATE_CHANGE = unsafeCastStringToDOMTopLevelType('ratechange');
|
790 |
|
|
var TOP_SCROLL = unsafeCastStringToDOMTopLevelType('scroll');
|
791 |
|
|
var TOP_SEEKED = unsafeCastStringToDOMTopLevelType('seeked');
|
792 |
|
|
var TOP_SEEKING = unsafeCastStringToDOMTopLevelType('seeking');
|
793 |
|
|
var TOP_SELECTION_CHANGE = unsafeCastStringToDOMTopLevelType('selectionchange');
|
794 |
|
|
var TOP_STALLED = unsafeCastStringToDOMTopLevelType('stalled');
|
795 |
|
|
var TOP_SUSPEND = unsafeCastStringToDOMTopLevelType('suspend');
|
796 |
|
|
var TOP_TEXT_INPUT = unsafeCastStringToDOMTopLevelType('textInput');
|
797 |
|
|
var TOP_TIME_UPDATE = unsafeCastStringToDOMTopLevelType('timeupdate');
|
798 |
|
|
var TOP_TOGGLE = unsafeCastStringToDOMTopLevelType('toggle');
|
799 |
|
|
var TOP_TOUCH_CANCEL = unsafeCastStringToDOMTopLevelType('touchcancel');
|
800 |
|
|
var TOP_TOUCH_END = unsafeCastStringToDOMTopLevelType('touchend');
|
801 |
|
|
var TOP_TOUCH_MOVE = unsafeCastStringToDOMTopLevelType('touchmove');
|
802 |
|
|
var TOP_TOUCH_START = unsafeCastStringToDOMTopLevelType('touchstart');
|
803 |
|
|
var TOP_TRANSITION_END = unsafeCastStringToDOMTopLevelType(getVendorPrefixedEventName('transitionend'));
|
804 |
|
|
var TOP_VOLUME_CHANGE = unsafeCastStringToDOMTopLevelType('volumechange');
|
805 |
|
|
var TOP_WAITING = unsafeCastStringToDOMTopLevelType('waiting');
|
806 |
|
|
var TOP_WHEEL = unsafeCastStringToDOMTopLevelType('wheel'); // List of events that need to be individually attached to media elements.
|
807 |
|
|
|
808 |
|
|
var PLUGIN_EVENT_SYSTEM = 1;
|
809 |
|
|
|
810 |
|
|
var didWarnAboutMessageChannel = false;
|
811 |
|
|
var enqueueTaskImpl = null;
|
812 |
|
|
function enqueueTask(task) {
|
813 |
|
|
if (enqueueTaskImpl === null) {
|
814 |
|
|
try {
|
815 |
|
|
// read require off the module object to get around the bundlers.
|
816 |
|
|
// we don't want them to detect a require and bundle a Node polyfill.
|
817 |
|
|
var requireString = ('require' + Math.random()).slice(0, 7);
|
818 |
|
|
var nodeRequire = module && module[requireString]; // assuming we're in node, let's try to get node's
|
819 |
|
|
// version of setImmediate, bypassing fake timers if any.
|
820 |
|
|
|
821 |
|
|
enqueueTaskImpl = nodeRequire('timers').setImmediate;
|
822 |
|
|
} catch (_err) {
|
823 |
|
|
// we're in a browser
|
824 |
|
|
// we can't use regular timers because they may still be faked
|
825 |
|
|
// so we try MessageChannel+postMessage instead
|
826 |
|
|
enqueueTaskImpl = function (callback) {
|
827 |
|
|
{
|
828 |
|
|
if (didWarnAboutMessageChannel === false) {
|
829 |
|
|
didWarnAboutMessageChannel = true;
|
830 |
|
|
|
831 |
|
|
if (typeof MessageChannel === 'undefined') {
|
832 |
|
|
error('This browser does not have a MessageChannel implementation, ' + 'so enqueuing tasks via await act(async () => ...) will fail. ' + 'Please file an issue at https://github.com/facebook/react/issues ' + 'if you encounter this warning.');
|
833 |
|
|
}
|
834 |
|
|
}
|
835 |
|
|
}
|
836 |
|
|
|
837 |
|
|
var channel = new MessageChannel();
|
838 |
|
|
channel.port1.onmessage = callback;
|
839 |
|
|
channel.port2.postMessage(undefined);
|
840 |
|
|
};
|
841 |
|
|
}
|
842 |
|
|
}
|
843 |
|
|
|
844 |
|
|
return enqueueTaskImpl(task);
|
845 |
|
|
}
|
846 |
|
|
|
847 |
|
|
// ReactDOM.js, and ReactTestUtils.js:
|
848 |
|
|
|
849 |
|
|
var _ReactDOM$__SECRET_IN = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Events,
|
850 |
|
|
|
851 |
|
|
/* eslint-disable no-unused-vars */
|
852 |
|
|
getInstanceFromNode = _ReactDOM$__SECRET_IN[0],
|
853 |
|
|
getNodeFromInstance = _ReactDOM$__SECRET_IN[1],
|
854 |
|
|
getFiberCurrentPropsFromNode = _ReactDOM$__SECRET_IN[2],
|
855 |
|
|
injectEventPluginsByName = _ReactDOM$__SECRET_IN[3],
|
856 |
|
|
eventNameDispatchConfigs = _ReactDOM$__SECRET_IN[4],
|
857 |
|
|
accumulateTwoPhaseDispatches = _ReactDOM$__SECRET_IN[5],
|
858 |
|
|
accumulateDirectDispatches = _ReactDOM$__SECRET_IN[6],
|
859 |
|
|
enqueueStateRestore = _ReactDOM$__SECRET_IN[7],
|
860 |
|
|
restoreStateIfNeeded = _ReactDOM$__SECRET_IN[8],
|
861 |
|
|
dispatchEvent = _ReactDOM$__SECRET_IN[9],
|
862 |
|
|
runEventsInBatch = _ReactDOM$__SECRET_IN[10],
|
863 |
|
|
|
864 |
|
|
/* eslint-enable no-unused-vars */
|
865 |
|
|
flushPassiveEffects = _ReactDOM$__SECRET_IN[11],
|
866 |
|
|
IsThisRendererActing = _ReactDOM$__SECRET_IN[12];
|
867 |
|
|
var batchedUpdates = ReactDOM.unstable_batchedUpdates;
|
868 |
|
|
var IsSomeRendererActing = ReactSharedInternals.IsSomeRendererActing; // this implementation should be exactly the same in
|
869 |
|
|
// ReactTestUtilsAct.js, ReactTestRendererAct.js, createReactNoop.js
|
870 |
|
|
|
871 |
|
|
var isSchedulerMocked = typeof Scheduler.unstable_flushAllWithoutAsserting === 'function';
|
872 |
|
|
|
873 |
|
|
var flushWork = Scheduler.unstable_flushAllWithoutAsserting || function () {
|
874 |
|
|
var didFlushWork = false;
|
875 |
|
|
|
876 |
|
|
while (flushPassiveEffects()) {
|
877 |
|
|
didFlushWork = true;
|
878 |
|
|
}
|
879 |
|
|
|
880 |
|
|
return didFlushWork;
|
881 |
|
|
};
|
882 |
|
|
|
883 |
|
|
function flushWorkAndMicroTasks(onDone) {
|
884 |
|
|
try {
|
885 |
|
|
flushWork();
|
886 |
|
|
enqueueTask(function () {
|
887 |
|
|
if (flushWork()) {
|
888 |
|
|
flushWorkAndMicroTasks(onDone);
|
889 |
|
|
} else {
|
890 |
|
|
onDone();
|
891 |
|
|
}
|
892 |
|
|
});
|
893 |
|
|
} catch (err) {
|
894 |
|
|
onDone(err);
|
895 |
|
|
}
|
896 |
|
|
} // we track the 'depth' of the act() calls with this counter,
|
897 |
|
|
// so we can tell if any async act() calls try to run in parallel.
|
898 |
|
|
|
899 |
|
|
|
900 |
|
|
var actingUpdatesScopeDepth = 0;
|
901 |
|
|
|
902 |
|
|
function act(callback) {
|
903 |
|
|
|
904 |
|
|
var previousActingUpdatesScopeDepth = actingUpdatesScopeDepth;
|
905 |
|
|
var previousIsSomeRendererActing;
|
906 |
|
|
var previousIsThisRendererActing;
|
907 |
|
|
actingUpdatesScopeDepth++;
|
908 |
|
|
previousIsSomeRendererActing = IsSomeRendererActing.current;
|
909 |
|
|
previousIsThisRendererActing = IsThisRendererActing.current;
|
910 |
|
|
IsSomeRendererActing.current = true;
|
911 |
|
|
IsThisRendererActing.current = true;
|
912 |
|
|
|
913 |
|
|
function onDone() {
|
914 |
|
|
actingUpdatesScopeDepth--;
|
915 |
|
|
IsSomeRendererActing.current = previousIsSomeRendererActing;
|
916 |
|
|
IsThisRendererActing.current = previousIsThisRendererActing;
|
917 |
|
|
|
918 |
|
|
{
|
919 |
|
|
if (actingUpdatesScopeDepth > previousActingUpdatesScopeDepth) {
|
920 |
|
|
// if it's _less than_ previousActingUpdatesScopeDepth, then we can assume the 'other' one has warned
|
921 |
|
|
error('You seem to have overlapping act() calls, this is not supported. ' + 'Be sure to await previous act() calls before making a new one. ');
|
922 |
|
|
}
|
923 |
|
|
}
|
924 |
|
|
}
|
925 |
|
|
|
926 |
|
|
var result;
|
927 |
|
|
|
928 |
|
|
try {
|
929 |
|
|
result = batchedUpdates(callback);
|
930 |
|
|
} catch (error) {
|
931 |
|
|
// on sync errors, we still want to 'cleanup' and decrement actingUpdatesScopeDepth
|
932 |
|
|
onDone();
|
933 |
|
|
throw error;
|
934 |
|
|
}
|
935 |
|
|
|
936 |
|
|
if (result !== null && typeof result === 'object' && typeof result.then === 'function') {
|
937 |
|
|
// setup a boolean that gets set to true only
|
938 |
|
|
// once this act() call is await-ed
|
939 |
|
|
var called = false;
|
940 |
|
|
|
941 |
|
|
{
|
942 |
|
|
if (typeof Promise !== 'undefined') {
|
943 |
|
|
//eslint-disable-next-line no-undef
|
944 |
|
|
Promise.resolve().then(function () {}).then(function () {
|
945 |
|
|
if (called === false) {
|
946 |
|
|
error('You called act(async () => ...) without await. ' + 'This could lead to unexpected testing behaviour, interleaving multiple act ' + 'calls and mixing their scopes. You should - await act(async () => ...);');
|
947 |
|
|
}
|
948 |
|
|
});
|
949 |
|
|
}
|
950 |
|
|
} // in the async case, the returned thenable runs the callback, flushes
|
951 |
|
|
// effects and microtasks in a loop until flushPassiveEffects() === false,
|
952 |
|
|
// and cleans up
|
953 |
|
|
|
954 |
|
|
|
955 |
|
|
return {
|
956 |
|
|
then: function (resolve, reject) {
|
957 |
|
|
called = true;
|
958 |
|
|
result.then(function () {
|
959 |
|
|
if (actingUpdatesScopeDepth > 1 || isSchedulerMocked === true && previousIsSomeRendererActing === true) {
|
960 |
|
|
onDone();
|
961 |
|
|
resolve();
|
962 |
|
|
return;
|
963 |
|
|
} // we're about to exit the act() scope,
|
964 |
|
|
// now's the time to flush tasks/effects
|
965 |
|
|
|
966 |
|
|
|
967 |
|
|
flushWorkAndMicroTasks(function (err) {
|
968 |
|
|
onDone();
|
969 |
|
|
|
970 |
|
|
if (err) {
|
971 |
|
|
reject(err);
|
972 |
|
|
} else {
|
973 |
|
|
resolve();
|
974 |
|
|
}
|
975 |
|
|
});
|
976 |
|
|
}, function (err) {
|
977 |
|
|
onDone();
|
978 |
|
|
reject(err);
|
979 |
|
|
});
|
980 |
|
|
}
|
981 |
|
|
};
|
982 |
|
|
} else {
|
983 |
|
|
{
|
984 |
|
|
if (result !== undefined) {
|
985 |
|
|
error('The callback passed to act(...) function ' + 'must return undefined, or a Promise. You returned %s', result);
|
986 |
|
|
}
|
987 |
|
|
} // flush effects until none remain, and cleanup
|
988 |
|
|
|
989 |
|
|
|
990 |
|
|
try {
|
991 |
|
|
if (actingUpdatesScopeDepth === 1 && (isSchedulerMocked === false || previousIsSomeRendererActing === false)) {
|
992 |
|
|
// we're about to exit the act() scope,
|
993 |
|
|
// now's the time to flush effects
|
994 |
|
|
flushWork();
|
995 |
|
|
}
|
996 |
|
|
|
997 |
|
|
onDone();
|
998 |
|
|
} catch (err) {
|
999 |
|
|
onDone();
|
1000 |
|
|
throw err;
|
1001 |
|
|
} // in the sync case, the returned thenable only warns *if* await-ed
|
1002 |
|
|
|
1003 |
|
|
|
1004 |
|
|
return {
|
1005 |
|
|
then: function (resolve) {
|
1006 |
|
|
{
|
1007 |
|
|
error('Do not await the result of calling act(...) with sync logic, it is not a Promise.');
|
1008 |
|
|
}
|
1009 |
|
|
|
1010 |
|
|
resolve();
|
1011 |
|
|
}
|
1012 |
|
|
};
|
1013 |
|
|
}
|
1014 |
|
|
}
|
1015 |
|
|
|
1016 |
|
|
var findDOMNode = ReactDOM.findDOMNode; // Keep in sync with ReactDOMUnstableNativeDependencies.js
|
1017 |
|
|
// ReactDOM.js, and ReactTestUtilsAct.js:
|
1018 |
|
|
|
1019 |
|
|
var _ReactDOM$__SECRET_IN$1 = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Events,
|
1020 |
|
|
getInstanceFromNode$1 = _ReactDOM$__SECRET_IN$1[0],
|
1021 |
|
|
|
1022 |
|
|
/* eslint-disable no-unused-vars */
|
1023 |
|
|
getNodeFromInstance$1 = _ReactDOM$__SECRET_IN$1[1],
|
1024 |
|
|
getFiberCurrentPropsFromNode$1 = _ReactDOM$__SECRET_IN$1[2],
|
1025 |
|
|
injectEventPluginsByName$1 = _ReactDOM$__SECRET_IN$1[3],
|
1026 |
|
|
|
1027 |
|
|
/* eslint-enable no-unused-vars */
|
1028 |
|
|
eventNameDispatchConfigs$1 = _ReactDOM$__SECRET_IN$1[4],
|
1029 |
|
|
accumulateTwoPhaseDispatches$1 = _ReactDOM$__SECRET_IN$1[5],
|
1030 |
|
|
accumulateDirectDispatches$1 = _ReactDOM$__SECRET_IN$1[6],
|
1031 |
|
|
enqueueStateRestore$1 = _ReactDOM$__SECRET_IN$1[7],
|
1032 |
|
|
restoreStateIfNeeded$1 = _ReactDOM$__SECRET_IN$1[8],
|
1033 |
|
|
dispatchEvent$1 = _ReactDOM$__SECRET_IN$1[9],
|
1034 |
|
|
runEventsInBatch$1 = _ReactDOM$__SECRET_IN$1[10],
|
1035 |
|
|
|
1036 |
|
|
/* eslint-disable no-unused-vars */
|
1037 |
|
|
flushPassiveEffects$1 = _ReactDOM$__SECRET_IN$1[11],
|
1038 |
|
|
IsThisRendererActing$1
|
1039 |
|
|
/* eslint-enable no-unused-vars */
|
1040 |
|
|
= _ReactDOM$__SECRET_IN$1[12];
|
1041 |
|
|
|
1042 |
|
|
function Event(suffix) {}
|
1043 |
|
|
|
1044 |
|
|
var hasWarnedAboutDeprecatedMockComponent = false;
|
1045 |
|
|
/**
|
1046 |
|
|
* @class ReactTestUtils
|
1047 |
|
|
*/
|
1048 |
|
|
|
1049 |
|
|
/**
|
1050 |
|
|
* Simulates a top level event being dispatched from a raw event that occurred
|
1051 |
|
|
* on an `Element` node.
|
1052 |
|
|
* @param {number} topLevelType A number from `TopLevelEventTypes`
|
1053 |
|
|
* @param {!Element} node The dom to simulate an event occurring on.
|
1054 |
|
|
* @param {?Event} fakeNativeEvent Fake native event to use in SyntheticEvent.
|
1055 |
|
|
*/
|
1056 |
|
|
|
1057 |
|
|
function simulateNativeEventOnNode(topLevelType, node, fakeNativeEvent) {
|
1058 |
|
|
fakeNativeEvent.target = node;
|
1059 |
|
|
dispatchEvent$1(topLevelType, PLUGIN_EVENT_SYSTEM, document, fakeNativeEvent);
|
1060 |
|
|
}
|
1061 |
|
|
/**
|
1062 |
|
|
* Simulates a top level event being dispatched from a raw event that occurred
|
1063 |
|
|
* on the `ReactDOMComponent` `comp`.
|
1064 |
|
|
* @param {Object} topLevelType A type from `BrowserEventConstants.topLevelTypes`.
|
1065 |
|
|
* @param {!ReactDOMComponent} comp
|
1066 |
|
|
* @param {?Event} fakeNativeEvent Fake native event to use in SyntheticEvent.
|
1067 |
|
|
*/
|
1068 |
|
|
|
1069 |
|
|
|
1070 |
|
|
function simulateNativeEventOnDOMComponent(topLevelType, comp, fakeNativeEvent) {
|
1071 |
|
|
simulateNativeEventOnNode(topLevelType, findDOMNode(comp), fakeNativeEvent);
|
1072 |
|
|
}
|
1073 |
|
|
|
1074 |
|
|
function findAllInRenderedFiberTreeInternal(fiber, test) {
|
1075 |
|
|
if (!fiber) {
|
1076 |
|
|
return [];
|
1077 |
|
|
}
|
1078 |
|
|
|
1079 |
|
|
var currentParent = findCurrentFiberUsingSlowPath(fiber);
|
1080 |
|
|
|
1081 |
|
|
if (!currentParent) {
|
1082 |
|
|
return [];
|
1083 |
|
|
}
|
1084 |
|
|
|
1085 |
|
|
var node = currentParent;
|
1086 |
|
|
var ret = [];
|
1087 |
|
|
|
1088 |
|
|
while (true) {
|
1089 |
|
|
if (node.tag === HostComponent || node.tag === HostText || node.tag === ClassComponent || node.tag === FunctionComponent) {
|
1090 |
|
|
var publicInst = node.stateNode;
|
1091 |
|
|
|
1092 |
|
|
if (test(publicInst)) {
|
1093 |
|
|
ret.push(publicInst);
|
1094 |
|
|
}
|
1095 |
|
|
}
|
1096 |
|
|
|
1097 |
|
|
if (node.child) {
|
1098 |
|
|
node.child.return = node;
|
1099 |
|
|
node = node.child;
|
1100 |
|
|
continue;
|
1101 |
|
|
}
|
1102 |
|
|
|
1103 |
|
|
if (node === currentParent) {
|
1104 |
|
|
return ret;
|
1105 |
|
|
}
|
1106 |
|
|
|
1107 |
|
|
while (!node.sibling) {
|
1108 |
|
|
if (!node.return || node.return === currentParent) {
|
1109 |
|
|
return ret;
|
1110 |
|
|
}
|
1111 |
|
|
|
1112 |
|
|
node = node.return;
|
1113 |
|
|
}
|
1114 |
|
|
|
1115 |
|
|
node.sibling.return = node.return;
|
1116 |
|
|
node = node.sibling;
|
1117 |
|
|
}
|
1118 |
|
|
}
|
1119 |
|
|
|
1120 |
|
|
function validateClassInstance(inst, methodName) {
|
1121 |
|
|
if (!inst) {
|
1122 |
|
|
// This is probably too relaxed but it's existing behavior.
|
1123 |
|
|
return;
|
1124 |
|
|
}
|
1125 |
|
|
|
1126 |
|
|
if (get(inst)) {
|
1127 |
|
|
// This is a public instance indeed.
|
1128 |
|
|
return;
|
1129 |
|
|
}
|
1130 |
|
|
|
1131 |
|
|
var received;
|
1132 |
|
|
var stringified = '' + inst;
|
1133 |
|
|
|
1134 |
|
|
if (Array.isArray(inst)) {
|
1135 |
|
|
received = 'an array';
|
1136 |
|
|
} else if (inst && inst.nodeType === ELEMENT_NODE && inst.tagName) {
|
1137 |
|
|
received = 'a DOM node';
|
1138 |
|
|
} else if (stringified === '[object Object]') {
|
1139 |
|
|
received = 'object with keys {' + Object.keys(inst).join(', ') + '}';
|
1140 |
|
|
} else {
|
1141 |
|
|
received = stringified;
|
1142 |
|
|
}
|
1143 |
|
|
|
1144 |
|
|
{
|
1145 |
|
|
{
|
1146 |
|
|
throw Error( methodName + "(...): the first argument must be a React class instance. Instead received: " + received + "." );
|
1147 |
|
|
}
|
1148 |
|
|
}
|
1149 |
|
|
}
|
1150 |
|
|
/**
|
1151 |
|
|
* Utilities for making it easy to test React components.
|
1152 |
|
|
*
|
1153 |
|
|
* See https://reactjs.org/docs/test-utils.html
|
1154 |
|
|
*
|
1155 |
|
|
* Todo: Support the entire DOM.scry query syntax. For now, these simple
|
1156 |
|
|
* utilities will suffice for testing purposes.
|
1157 |
|
|
* @lends ReactTestUtils
|
1158 |
|
|
*/
|
1159 |
|
|
|
1160 |
|
|
|
1161 |
|
|
var ReactTestUtils = {
|
1162 |
|
|
renderIntoDocument: function (element) {
|
1163 |
|
|
var div = document.createElement('div'); // None of our tests actually require attaching the container to the
|
1164 |
|
|
// DOM, and doing so creates a mess that we rely on test isolation to
|
1165 |
|
|
// clean up, so we're going to stop honoring the name of this method
|
1166 |
|
|
// (and probably rename it eventually) if no problems arise.
|
1167 |
|
|
// document.documentElement.appendChild(div);
|
1168 |
|
|
|
1169 |
|
|
return ReactDOM.render(element, div);
|
1170 |
|
|
},
|
1171 |
|
|
isElement: function (element) {
|
1172 |
|
|
return React.isValidElement(element);
|
1173 |
|
|
},
|
1174 |
|
|
isElementOfType: function (inst, convenienceConstructor) {
|
1175 |
|
|
return React.isValidElement(inst) && inst.type === convenienceConstructor;
|
1176 |
|
|
},
|
1177 |
|
|
isDOMComponent: function (inst) {
|
1178 |
|
|
return !!(inst && inst.nodeType === ELEMENT_NODE && inst.tagName);
|
1179 |
|
|
},
|
1180 |
|
|
isDOMComponentElement: function (inst) {
|
1181 |
|
|
return !!(inst && React.isValidElement(inst) && !!inst.tagName);
|
1182 |
|
|
},
|
1183 |
|
|
isCompositeComponent: function (inst) {
|
1184 |
|
|
if (ReactTestUtils.isDOMComponent(inst)) {
|
1185 |
|
|
// Accessing inst.setState warns; just return false as that'll be what
|
1186 |
|
|
// this returns when we have DOM nodes as refs directly
|
1187 |
|
|
return false;
|
1188 |
|
|
}
|
1189 |
|
|
|
1190 |
|
|
return inst != null && typeof inst.render === 'function' && typeof inst.setState === 'function';
|
1191 |
|
|
},
|
1192 |
|
|
isCompositeComponentWithType: function (inst, type) {
|
1193 |
|
|
if (!ReactTestUtils.isCompositeComponent(inst)) {
|
1194 |
|
|
return false;
|
1195 |
|
|
}
|
1196 |
|
|
|
1197 |
|
|
var internalInstance = get(inst);
|
1198 |
|
|
var constructor = internalInstance.type;
|
1199 |
|
|
return constructor === type;
|
1200 |
|
|
},
|
1201 |
|
|
findAllInRenderedTree: function (inst, test) {
|
1202 |
|
|
validateClassInstance(inst, 'findAllInRenderedTree');
|
1203 |
|
|
|
1204 |
|
|
if (!inst) {
|
1205 |
|
|
return [];
|
1206 |
|
|
}
|
1207 |
|
|
|
1208 |
|
|
var internalInstance = get(inst);
|
1209 |
|
|
return findAllInRenderedFiberTreeInternal(internalInstance, test);
|
1210 |
|
|
},
|
1211 |
|
|
|
1212 |
|
|
/**
|
1213 |
|
|
* Finds all instance of components in the rendered tree that are DOM
|
1214 |
|
|
* components with the class name matching `className`.
|
1215 |
|
|
* @return {array} an array of all the matches.
|
1216 |
|
|
*/
|
1217 |
|
|
scryRenderedDOMComponentsWithClass: function (root, classNames) {
|
1218 |
|
|
validateClassInstance(root, 'scryRenderedDOMComponentsWithClass');
|
1219 |
|
|
return ReactTestUtils.findAllInRenderedTree(root, function (inst) {
|
1220 |
|
|
if (ReactTestUtils.isDOMComponent(inst)) {
|
1221 |
|
|
var className = inst.className;
|
1222 |
|
|
|
1223 |
|
|
if (typeof className !== 'string') {
|
1224 |
|
|
// SVG, probably.
|
1225 |
|
|
className = inst.getAttribute('class') || '';
|
1226 |
|
|
}
|
1227 |
|
|
|
1228 |
|
|
var classList = className.split(/\s+/);
|
1229 |
|
|
|
1230 |
|
|
if (!Array.isArray(classNames)) {
|
1231 |
|
|
if (!(classNames !== undefined)) {
|
1232 |
|
|
{
|
1233 |
|
|
throw Error( "TestUtils.scryRenderedDOMComponentsWithClass expects a className as a second argument." );
|
1234 |
|
|
}
|
1235 |
|
|
}
|
1236 |
|
|
|
1237 |
|
|
classNames = classNames.split(/\s+/);
|
1238 |
|
|
}
|
1239 |
|
|
|
1240 |
|
|
return classNames.every(function (name) {
|
1241 |
|
|
return classList.indexOf(name) !== -1;
|
1242 |
|
|
});
|
1243 |
|
|
}
|
1244 |
|
|
|
1245 |
|
|
return false;
|
1246 |
|
|
});
|
1247 |
|
|
},
|
1248 |
|
|
|
1249 |
|
|
/**
|
1250 |
|
|
* Like scryRenderedDOMComponentsWithClass but expects there to be one result,
|
1251 |
|
|
* and returns that one result, or throws exception if there is any other
|
1252 |
|
|
* number of matches besides one.
|
1253 |
|
|
* @return {!ReactDOMComponent} The one match.
|
1254 |
|
|
*/
|
1255 |
|
|
findRenderedDOMComponentWithClass: function (root, className) {
|
1256 |
|
|
validateClassInstance(root, 'findRenderedDOMComponentWithClass');
|
1257 |
|
|
var all = ReactTestUtils.scryRenderedDOMComponentsWithClass(root, className);
|
1258 |
|
|
|
1259 |
|
|
if (all.length !== 1) {
|
1260 |
|
|
throw new Error('Did not find exactly one match (found: ' + all.length + ') ' + 'for class:' + className);
|
1261 |
|
|
}
|
1262 |
|
|
|
1263 |
|
|
return all[0];
|
1264 |
|
|
},
|
1265 |
|
|
|
1266 |
|
|
/**
|
1267 |
|
|
* Finds all instance of components in the rendered tree that are DOM
|
1268 |
|
|
* components with the tag name matching `tagName`.
|
1269 |
|
|
* @return {array} an array of all the matches.
|
1270 |
|
|
*/
|
1271 |
|
|
scryRenderedDOMComponentsWithTag: function (root, tagName) {
|
1272 |
|
|
validateClassInstance(root, 'scryRenderedDOMComponentsWithTag');
|
1273 |
|
|
return ReactTestUtils.findAllInRenderedTree(root, function (inst) {
|
1274 |
|
|
return ReactTestUtils.isDOMComponent(inst) && inst.tagName.toUpperCase() === tagName.toUpperCase();
|
1275 |
|
|
});
|
1276 |
|
|
},
|
1277 |
|
|
|
1278 |
|
|
/**
|
1279 |
|
|
* Like scryRenderedDOMComponentsWithTag but expects there to be one result,
|
1280 |
|
|
* and returns that one result, or throws exception if there is any other
|
1281 |
|
|
* number of matches besides one.
|
1282 |
|
|
* @return {!ReactDOMComponent} The one match.
|
1283 |
|
|
*/
|
1284 |
|
|
findRenderedDOMComponentWithTag: function (root, tagName) {
|
1285 |
|
|
validateClassInstance(root, 'findRenderedDOMComponentWithTag');
|
1286 |
|
|
var all = ReactTestUtils.scryRenderedDOMComponentsWithTag(root, tagName);
|
1287 |
|
|
|
1288 |
|
|
if (all.length !== 1) {
|
1289 |
|
|
throw new Error('Did not find exactly one match (found: ' + all.length + ') ' + 'for tag:' + tagName);
|
1290 |
|
|
}
|
1291 |
|
|
|
1292 |
|
|
return all[0];
|
1293 |
|
|
},
|
1294 |
|
|
|
1295 |
|
|
/**
|
1296 |
|
|
* Finds all instances of components with type equal to `componentType`.
|
1297 |
|
|
* @return {array} an array of all the matches.
|
1298 |
|
|
*/
|
1299 |
|
|
scryRenderedComponentsWithType: function (root, componentType) {
|
1300 |
|
|
validateClassInstance(root, 'scryRenderedComponentsWithType');
|
1301 |
|
|
return ReactTestUtils.findAllInRenderedTree(root, function (inst) {
|
1302 |
|
|
return ReactTestUtils.isCompositeComponentWithType(inst, componentType);
|
1303 |
|
|
});
|
1304 |
|
|
},
|
1305 |
|
|
|
1306 |
|
|
/**
|
1307 |
|
|
* Same as `scryRenderedComponentsWithType` but expects there to be one result
|
1308 |
|
|
* and returns that one result, or throws exception if there is any other
|
1309 |
|
|
* number of matches besides one.
|
1310 |
|
|
* @return {!ReactComponent} The one match.
|
1311 |
|
|
*/
|
1312 |
|
|
findRenderedComponentWithType: function (root, componentType) {
|
1313 |
|
|
validateClassInstance(root, 'findRenderedComponentWithType');
|
1314 |
|
|
var all = ReactTestUtils.scryRenderedComponentsWithType(root, componentType);
|
1315 |
|
|
|
1316 |
|
|
if (all.length !== 1) {
|
1317 |
|
|
throw new Error('Did not find exactly one match (found: ' + all.length + ') ' + 'for componentType:' + componentType);
|
1318 |
|
|
}
|
1319 |
|
|
|
1320 |
|
|
return all[0];
|
1321 |
|
|
},
|
1322 |
|
|
|
1323 |
|
|
/**
|
1324 |
|
|
* Pass a mocked component module to this method to augment it with
|
1325 |
|
|
* useful methods that allow it to be used as a dummy React component.
|
1326 |
|
|
* Instead of rendering as usual, the component will become a simple
|
1327 |
|
|
* <div> containing any provided children.
|
1328 |
|
|
*
|
1329 |
|
|
* @param {object} module the mock function object exported from a
|
1330 |
|
|
* module that defines the component to be mocked
|
1331 |
|
|
* @param {?string} mockTagName optional dummy root tag name to return
|
1332 |
|
|
* from render method (overrides
|
1333 |
|
|
* module.mockTagName if provided)
|
1334 |
|
|
* @return {object} the ReactTestUtils object (for chaining)
|
1335 |
|
|
*/
|
1336 |
|
|
mockComponent: function (module, mockTagName) {
|
1337 |
|
|
{
|
1338 |
|
|
if (!hasWarnedAboutDeprecatedMockComponent) {
|
1339 |
|
|
hasWarnedAboutDeprecatedMockComponent = true;
|
1340 |
|
|
|
1341 |
|
|
warn('ReactTestUtils.mockComponent() is deprecated. ' + 'Use shallow rendering or jest.mock() instead.\n\n' + 'See https://fb.me/test-utils-mock-component for more information.');
|
1342 |
|
|
}
|
1343 |
|
|
}
|
1344 |
|
|
|
1345 |
|
|
mockTagName = mockTagName || module.mockTagName || 'div';
|
1346 |
|
|
module.prototype.render.mockImplementation(function () {
|
1347 |
|
|
return React.createElement(mockTagName, null, this.props.children);
|
1348 |
|
|
});
|
1349 |
|
|
return this;
|
1350 |
|
|
},
|
1351 |
|
|
nativeTouchData: function (x, y) {
|
1352 |
|
|
return {
|
1353 |
|
|
touches: [{
|
1354 |
|
|
pageX: x,
|
1355 |
|
|
pageY: y
|
1356 |
|
|
}]
|
1357 |
|
|
};
|
1358 |
|
|
},
|
1359 |
|
|
Simulate: null,
|
1360 |
|
|
SimulateNative: {},
|
1361 |
|
|
act: act
|
1362 |
|
|
};
|
1363 |
|
|
/**
|
1364 |
|
|
* Exports:
|
1365 |
|
|
*
|
1366 |
|
|
* - `ReactTestUtils.Simulate.click(Element)`
|
1367 |
|
|
* - `ReactTestUtils.Simulate.mouseMove(Element)`
|
1368 |
|
|
* - `ReactTestUtils.Simulate.change(Element)`
|
1369 |
|
|
* - ... (All keys from event plugin `eventTypes` objects)
|
1370 |
|
|
*/
|
1371 |
|
|
|
1372 |
|
|
function makeSimulator(eventType) {
|
1373 |
|
|
return function (domNode, eventData) {
|
1374 |
|
|
if (!!React.isValidElement(domNode)) {
|
1375 |
|
|
{
|
1376 |
|
|
throw Error( "TestUtils.Simulate expected a DOM node as the first argument but received a React element. Pass the DOM node you wish to simulate the event on instead. Note that TestUtils.Simulate will not work if you are using shallow rendering." );
|
1377 |
|
|
}
|
1378 |
|
|
}
|
1379 |
|
|
|
1380 |
|
|
if (!!ReactTestUtils.isCompositeComponent(domNode)) {
|
1381 |
|
|
{
|
1382 |
|
|
throw Error( "TestUtils.Simulate expected a DOM node as the first argument but received a component instance. Pass the DOM node you wish to simulate the event on instead." );
|
1383 |
|
|
}
|
1384 |
|
|
}
|
1385 |
|
|
|
1386 |
|
|
var dispatchConfig = eventNameDispatchConfigs$1[eventType];
|
1387 |
|
|
var fakeNativeEvent = new Event();
|
1388 |
|
|
fakeNativeEvent.target = domNode;
|
1389 |
|
|
fakeNativeEvent.type = eventType.toLowerCase(); // We don't use SyntheticEvent.getPooled in order to not have to worry about
|
1390 |
|
|
// properly destroying any properties assigned from `eventData` upon release
|
1391 |
|
|
|
1392 |
|
|
var targetInst = getInstanceFromNode$1(domNode);
|
1393 |
|
|
var event = new SyntheticEvent(dispatchConfig, targetInst, fakeNativeEvent, domNode); // Since we aren't using pooling, always persist the event. This will make
|
1394 |
|
|
// sure it's marked and won't warn when setting additional properties.
|
1395 |
|
|
|
1396 |
|
|
event.persist();
|
1397 |
|
|
|
1398 |
|
|
_assign(event, eventData);
|
1399 |
|
|
|
1400 |
|
|
if (dispatchConfig.phasedRegistrationNames) {
|
1401 |
|
|
accumulateTwoPhaseDispatches$1(event);
|
1402 |
|
|
} else {
|
1403 |
|
|
accumulateDirectDispatches$1(event);
|
1404 |
|
|
}
|
1405 |
|
|
|
1406 |
|
|
ReactDOM.unstable_batchedUpdates(function () {
|
1407 |
|
|
// Normally extractEvent enqueues a state restore, but we'll just always
|
1408 |
|
|
// do that since we're by-passing it here.
|
1409 |
|
|
enqueueStateRestore$1(domNode);
|
1410 |
|
|
runEventsInBatch$1(event);
|
1411 |
|
|
});
|
1412 |
|
|
restoreStateIfNeeded$1();
|
1413 |
|
|
};
|
1414 |
|
|
}
|
1415 |
|
|
|
1416 |
|
|
function buildSimulators() {
|
1417 |
|
|
ReactTestUtils.Simulate = {};
|
1418 |
|
|
var eventType;
|
1419 |
|
|
|
1420 |
|
|
for (eventType in eventNameDispatchConfigs$1) {
|
1421 |
|
|
/**
|
1422 |
|
|
* @param {!Element|ReactDOMComponent} domComponentOrNode
|
1423 |
|
|
* @param {?object} eventData Fake event data to use in SyntheticEvent.
|
1424 |
|
|
*/
|
1425 |
|
|
ReactTestUtils.Simulate[eventType] = makeSimulator(eventType);
|
1426 |
|
|
}
|
1427 |
|
|
}
|
1428 |
|
|
|
1429 |
|
|
buildSimulators();
|
1430 |
|
|
/**
|
1431 |
|
|
* Exports:
|
1432 |
|
|
*
|
1433 |
|
|
* - `ReactTestUtils.SimulateNative.click(Element/ReactDOMComponent)`
|
1434 |
|
|
* - `ReactTestUtils.SimulateNative.mouseMove(Element/ReactDOMComponent)`
|
1435 |
|
|
* - `ReactTestUtils.SimulateNative.mouseIn/ReactDOMComponent)`
|
1436 |
|
|
* - `ReactTestUtils.SimulateNative.mouseOut(Element/ReactDOMComponent)`
|
1437 |
|
|
* - ... (All keys from `BrowserEventConstants.topLevelTypes`)
|
1438 |
|
|
*
|
1439 |
|
|
* Note: Top level event types are a subset of the entire set of handler types
|
1440 |
|
|
* (which include a broader set of "synthetic" events). For example, onDragDone
|
1441 |
|
|
* is a synthetic event. Except when testing an event plugin or React's event
|
1442 |
|
|
* handling code specifically, you probably want to use ReactTestUtils.Simulate
|
1443 |
|
|
* to dispatch synthetic events.
|
1444 |
|
|
*/
|
1445 |
|
|
|
1446 |
|
|
function makeNativeSimulator(eventType, topLevelType) {
|
1447 |
|
|
return function (domComponentOrNode, nativeEventData) {
|
1448 |
|
|
var fakeNativeEvent = new Event(eventType);
|
1449 |
|
|
|
1450 |
|
|
_assign(fakeNativeEvent, nativeEventData);
|
1451 |
|
|
|
1452 |
|
|
if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
|
1453 |
|
|
simulateNativeEventOnDOMComponent(topLevelType, domComponentOrNode, fakeNativeEvent);
|
1454 |
|
|
} else if (domComponentOrNode.tagName) {
|
1455 |
|
|
// Will allow on actual dom nodes.
|
1456 |
|
|
simulateNativeEventOnNode(topLevelType, domComponentOrNode, fakeNativeEvent);
|
1457 |
|
|
}
|
1458 |
|
|
};
|
1459 |
|
|
}
|
1460 |
|
|
|
1461 |
|
|
[[TOP_ABORT, 'abort'], [TOP_ANIMATION_END, 'animationEnd'], [TOP_ANIMATION_ITERATION, 'animationIteration'], [TOP_ANIMATION_START, 'animationStart'], [TOP_BLUR, 'blur'], [TOP_CAN_PLAY_THROUGH, 'canPlayThrough'], [TOP_CAN_PLAY, 'canPlay'], [TOP_CANCEL, 'cancel'], [TOP_CHANGE, 'change'], [TOP_CLICK, 'click'], [TOP_CLOSE, 'close'], [TOP_COMPOSITION_END, 'compositionEnd'], [TOP_COMPOSITION_START, 'compositionStart'], [TOP_COMPOSITION_UPDATE, 'compositionUpdate'], [TOP_CONTEXT_MENU, 'contextMenu'], [TOP_COPY, 'copy'], [TOP_CUT, 'cut'], [TOP_DOUBLE_CLICK, 'doubleClick'], [TOP_DRAG_END, 'dragEnd'], [TOP_DRAG_ENTER, 'dragEnter'], [TOP_DRAG_EXIT, 'dragExit'], [TOP_DRAG_LEAVE, 'dragLeave'], [TOP_DRAG_OVER, 'dragOver'], [TOP_DRAG_START, 'dragStart'], [TOP_DRAG, 'drag'], [TOP_DROP, 'drop'], [TOP_DURATION_CHANGE, 'durationChange'], [TOP_EMPTIED, 'emptied'], [TOP_ENCRYPTED, 'encrypted'], [TOP_ENDED, 'ended'], [TOP_ERROR, 'error'], [TOP_FOCUS, 'focus'], [TOP_INPUT, 'input'], [TOP_KEY_DOWN, 'keyDown'], [TOP_KEY_PRESS, 'keyPress'], [TOP_KEY_UP, 'keyUp'], [TOP_LOAD_START, 'loadStart'], [TOP_LOAD_START, 'loadStart'], [TOP_LOAD, 'load'], [TOP_LOADED_DATA, 'loadedData'], [TOP_LOADED_METADATA, 'loadedMetadata'], [TOP_MOUSE_DOWN, 'mouseDown'], [TOP_MOUSE_MOVE, 'mouseMove'], [TOP_MOUSE_OUT, 'mouseOut'], [TOP_MOUSE_OVER, 'mouseOver'], [TOP_MOUSE_UP, 'mouseUp'], [TOP_PASTE, 'paste'], [TOP_PAUSE, 'pause'], [TOP_PLAY, 'play'], [TOP_PLAYING, 'playing'], [TOP_PROGRESS, 'progress'], [TOP_RATE_CHANGE, 'rateChange'], [TOP_SCROLL, 'scroll'], [TOP_SEEKED, 'seeked'], [TOP_SEEKING, 'seeking'], [TOP_SELECTION_CHANGE, 'selectionChange'], [TOP_STALLED, 'stalled'], [TOP_SUSPEND, 'suspend'], [TOP_TEXT_INPUT, 'textInput'], [TOP_TIME_UPDATE, 'timeUpdate'], [TOP_TOGGLE, 'toggle'], [TOP_TOUCH_CANCEL, 'touchCancel'], [TOP_TOUCH_END, 'touchEnd'], [TOP_TOUCH_MOVE, 'touchMove'], [TOP_TOUCH_START, 'touchStart'], [TOP_TRANSITION_END, 'transitionEnd'], [TOP_VOLUME_CHANGE, 'volumeChange'], [TOP_WAITING, 'waiting'], [TOP_WHEEL, 'wheel']].forEach(function (_ref) {
|
1462 |
|
|
var topLevelType = _ref[0],
|
1463 |
|
|
eventType = _ref[1];
|
1464 |
|
|
|
1465 |
|
|
/**
|
1466 |
|
|
* @param {!Element|ReactDOMComponent} domComponentOrNode
|
1467 |
|
|
* @param {?Event} nativeEventData Fake native event to use in SyntheticEvent.
|
1468 |
|
|
*/
|
1469 |
|
|
ReactTestUtils.SimulateNative[eventType] = makeNativeSimulator(eventType, topLevelType);
|
1470 |
|
|
});
|
1471 |
|
|
|
1472 |
|
|
// TODO: decide on the top-level export form.
|
1473 |
|
|
// This is hacky but makes it work with both Rollup and Jest.
|
1474 |
|
|
|
1475 |
|
|
|
1476 |
|
|
var testUtils = ReactTestUtils.default || ReactTestUtils;
|
1477 |
|
|
|
1478 |
|
|
module.exports = testUtils;
|
1479 |
|
|
})();
|
1480 |
|
|
}
|