Skip to content

Commit b683c07

Browse files
authored
Remove TestUtils dependency on event registry (facebook#19235)
1 parent 9e7f5c0 commit b683c07

File tree

4 files changed

+109
-26
lines changed

4 files changed

+109
-26
lines changed

packages/react-dom/src/client/ReactDOM.js

-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import {
3838
} from 'react-reconciler/src/ReactFiberReconciler';
3939
import {createPortal as createPortalImpl} from 'react-reconciler/src/ReactPortal';
4040
import {canUseDOM} from 'shared/ExecutionEnvironment';
41-
import {eventNameDispatchConfigs} from '../events/EventPluginRegistry';
4241
import ReactVersion from 'shared/ReactVersion';
4342
import invariant from 'shared/invariant';
4443
import {
@@ -175,7 +174,6 @@ const Internals = {
175174
getInstanceFromNode,
176175
getNodeFromInstance,
177176
getFiberCurrentPropsFromNode,
178-
eventNameDispatchConfigs,
179177
enqueueStateRestore,
180178
restoreStateIfNeeded,
181179
flushPassiveEffects,

packages/react-dom/src/events/EventPluginRegistry.js

-13
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ import type {EventTypes} from './PluginModuleType';
1212

1313
import invariant from 'shared/invariant';
1414

15-
/**
16-
* Mapping from event name to dispatch config
17-
*/
18-
export const eventNameDispatchConfigs = {};
19-
2015
/**
2116
* Mapping from registration name to plugin module
2217
*/
@@ -40,15 +35,7 @@ function publishEventForPlugin(
4035
eventTypes: EventTypes,
4136
eventName: string,
4237
): boolean {
43-
invariant(
44-
!eventNameDispatchConfigs.hasOwnProperty(eventName),
45-
'EventPluginRegistry: More than one plugin attempted to publish the same ' +
46-
'event name, `%s`.',
47-
eventName,
48-
);
4938
const dispatchConfig = eventTypes[eventName];
50-
eventNameDispatchConfigs[eventName] = dispatchConfig;
51-
5239
const phasedRegistrationNames = dispatchConfig.phasedRegistrationNames;
5340
if (phasedRegistrationNames) {
5441
for (const phaseName in phasedRegistrationNames) {

packages/react-dom/src/test-utils/ReactTestUtils.js

+109-10
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const [
3131
getNodeFromInstance,
3232
getFiberCurrentPropsFromNode,
3333
/* eslint-enable no-unused-vars */
34-
eventNameDispatchConfigs,
3534
enqueueStateRestore,
3635
restoreStateIfNeeded,
3736
/* eslint-disable no-unused-vars */
@@ -549,6 +548,13 @@ function accumulateTwoPhaseDispatchesSingle(event) {
549548

550549
const Simulate = {};
551550

551+
const directDispatchEventTypes = new Set([
552+
'mouseEnter',
553+
'mouseLeave',
554+
'pointerEnter',
555+
'pointerLeave',
556+
]);
557+
552558
/**
553559
* Exports:
554560
*
@@ -571,7 +577,19 @@ function makeSimulator(eventType) {
571577
'a component instance. Pass the DOM node you wish to simulate the event on instead.',
572578
);
573579

574-
const dispatchConfig = eventNameDispatchConfigs[eventType];
580+
// Reconstruct more or less what the original event system produced.
581+
// We could remove this indirection here but we also don't plan to invest in Simulate anyway.
582+
const dispatchConfig = {};
583+
if (directDispatchEventTypes.has(eventType)) {
584+
dispatchConfig.registrationName =
585+
'on' + eventType[0].toUpperCase() + eventType.slice(1);
586+
} else {
587+
dispatchConfig.phasedRegistrationNames = {
588+
bubbled: 'on' + eventType[0].toUpperCase() + eventType.slice(1),
589+
captured:
590+
'on' + eventType[0].toUpperCase() + eventType.slice(1) + 'Capture',
591+
};
592+
}
575593

576594
const fakeNativeEvent = new Event();
577595
fakeNativeEvent.target = domNode;
@@ -607,17 +625,98 @@ function makeSimulator(eventType) {
607625
};
608626
}
609627

628+
// A one-time snapshot with no plans to update. We'll probably want to deprecate Simulate API.
629+
const simulatedEventTypes = [
630+
'blur',
631+
'cancel',
632+
'click',
633+
'close',
634+
'contextMenu',
635+
'copy',
636+
'cut',
637+
'auxClick',
638+
'doubleClick',
639+
'dragEnd',
640+
'dragStart',
641+
'drop',
642+
'focus',
643+
'input',
644+
'invalid',
645+
'keyDown',
646+
'keyPress',
647+
'keyUp',
648+
'mouseDown',
649+
'mouseUp',
650+
'paste',
651+
'pause',
652+
'play',
653+
'pointerCancel',
654+
'pointerDown',
655+
'pointerUp',
656+
'rateChange',
657+
'reset',
658+
'seeked',
659+
'submit',
660+
'touchCancel',
661+
'touchEnd',
662+
'touchStart',
663+
'volumeChange',
664+
'drag',
665+
'dragEnter',
666+
'dragExit',
667+
'dragLeave',
668+
'dragOver',
669+
'mouseMove',
670+
'mouseOut',
671+
'mouseOver',
672+
'pointerMove',
673+
'pointerOut',
674+
'pointerOver',
675+
'scroll',
676+
'toggle',
677+
'touchMove',
678+
'wheel',
679+
'abort',
680+
'animationEnd',
681+
'animationIteration',
682+
'animationStart',
683+
'canPlay',
684+
'canPlayThrough',
685+
'durationChange',
686+
'emptied',
687+
'encrypted',
688+
'ended',
689+
'error',
690+
'gotPointerCapture',
691+
'load',
692+
'loadedData',
693+
'loadedMetadata',
694+
'loadStart',
695+
'lostPointerCapture',
696+
'playing',
697+
'progress',
698+
'seeking',
699+
'stalled',
700+
'suspend',
701+
'timeUpdate',
702+
'transitionEnd',
703+
'waiting',
704+
'mouseEnter',
705+
'mouseLeave',
706+
'pointerEnter',
707+
'pointerLeave',
708+
'change',
709+
'select',
710+
'beforeInput',
711+
'compositionEnd',
712+
'compositionStart',
713+
'compositionUpdate',
714+
];
610715
function buildSimulators() {
611-
let eventType;
612-
for (eventType in eventNameDispatchConfigs) {
613-
/**
614-
* @param {!Element|ReactDOMComponent} domComponentOrNode
615-
* @param {?object} eventData Fake event data to use in SyntheticEvent.
616-
*/
716+
simulatedEventTypes.forEach(eventType => {
617717
Simulate[eventType] = makeSimulator(eventType);
618-
}
718+
});
619719
}
620-
621720
buildSimulators();
622721

623722
export {

packages/react-dom/src/test-utils/ReactTestUtilsAct.js

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const [
2020
getInstanceFromNode,
2121
getNodeFromInstance,
2222
getFiberCurrentPropsFromNode,
23-
eventNameDispatchConfigs,
2423
enqueueStateRestore,
2524
restoreStateIfNeeded,
2625
/* eslint-enable no-unused-vars */

0 commit comments

Comments
 (0)