@samitouri / QOS-React-2 / commits / b683c07ccc

Remove TestUtils dependency on event registry (#19235)

Dan Abramov committed Jul 1, 2020 at 22:54 UTC b683c07ccce340b9d687683d5dd7347a4c866787
4 files changed +109 -26
packages/react-dom/src/client/ReactDOM.js
-2
@@ -38,7 +38,6 @@ import {
38 } from 'react-reconciler/src/ReactFiberReconciler';
39 import {createPortal as createPortalImpl} from 'react-reconciler/src/ReactPortal';
40 import {canUseDOM} from 'shared/ExecutionEnvironment';
41 -import {eventNameDispatchConfigs} from '../events/EventPluginRegistry';
41 import ReactVersion from 'shared/ReactVersion';
42 import invariant from 'shared/invariant';
43 import {
@@ -175,7 +174,6 @@ const Internals = {
174 getInstanceFromNode,
175 getNodeFromInstance,
176 getFiberCurrentPropsFromNode,
178 - eventNameDispatchConfigs,
177 enqueueStateRestore,
178 restoreStateIfNeeded,
179 flushPassiveEffects,
packages/react-dom/src/events/EventPluginRegistry.js
-13
@@ -12,11 +12,6 @@ import type {EventTypes} from './PluginModuleType';
12
13 import invariant from 'shared/invariant';
14
15 -/**
16 - * Mapping from event name to dispatch config
17 - */
18 -export const eventNameDispatchConfigs = {};
19 -
15 /**
16 * Mapping from registration name to plugin module
17 */
@@ -40,15 +35,7 @@ function publishEventForPlugin(
35 eventTypes: EventTypes,
36 eventName: string,
37 ): 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 - );
38 const dispatchConfig = eventTypes[eventName];
50 - eventNameDispatchConfigs[eventName] = dispatchConfig;
51 -
39 const phasedRegistrationNames = dispatchConfig.phasedRegistrationNames;
40 if (phasedRegistrationNames) {
41 for (const phaseName in phasedRegistrationNames) {
packages/react-dom/src/test-utils/ReactTestUtils.js
+109 -10
@@ -31,7 +31,6 @@ const [
31 getNodeFromInstance,
32 getFiberCurrentPropsFromNode,
33 /* eslint-enable no-unused-vars */
34 - eventNameDispatchConfigs,
34 enqueueStateRestore,
35 restoreStateIfNeeded,
36 /* eslint-disable no-unused-vars */
@@ -549,6 +548,13 @@ function accumulateTwoPhaseDispatchesSingle(event) {
548
549 const Simulate = {};
550
551 +const directDispatchEventTypes = new Set([
552 + 'mouseEnter',
553 + 'mouseLeave',
554 + 'pointerEnter',
555 + 'pointerLeave',
556 +]);
557 +
558 /**
559 * Exports:
560 *
@@ -571,7 +577,19 @@ function makeSimulator(eventType) {
577 'a component instance. Pass the DOM node you wish to simulate the event on instead.',
578 );
579
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 + }
593
594 const fakeNativeEvent = new Event();
595 fakeNativeEvent.target = domNode;
@@ -607,17 +625,98 @@ function makeSimulator(eventType) {
625 };
626 }
627
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 +];
715 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 => {
717 Simulate[eventType] = makeSimulator(eventType);
618 - }
718 + });
719 }
620 -
720 buildSimulators();
721
722 export {
packages/react-dom/src/test-utils/ReactTestUtilsAct.js
-1
@@ -20,7 +20,6 @@ const [
20 getInstanceFromNode,
21 getNodeFromInstance,
22 getFiberCurrentPropsFromNode,
23 - eventNameDispatchConfigs,
23 enqueueStateRestore,
24 restoreStateIfNeeded,
25 /* eslint-enable no-unused-vars */