@samitouri / QOS-React-2 / commits / 3c1dcd349a

Expose less internals for TestUtils (#13539)

* Expose less internals for TestUtils * Keep EventPluginHub for www * Reorder to simplify

Dan Abramov committed Sep 3, 2018 at 17:21 UTC 3c1dcd349a8e4205322c296b1db8c313ed06b5ce
5 files changed +51 -49
packages/events/__tests__/ResponderEventPlugin-test.internal.js
+1 -4
@@ -393,11 +393,8 @@ describe('ResponderEventPlugin', () => {
393 beforeEach(() => {
394 jest.resetModules();
395
396 - const ReactDOM = require('react-dom');
396 const ReactDOMUnstableNativeDependencies = require('react-dom/unstable-native-dependencies');
398 - EventPluginHub =
399 - ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
400 - .EventPluginHub;
397 + EventPluginHub = require('events/EventPluginHub');
398 const injectComponentTree =
399 ReactDOMUnstableNativeDependencies.injectComponentTree;
400 ResponderEventPlugin =
packages/react-dom/src/client/ReactDOM.js
+14 -8
@@ -739,14 +739,20 @@ const ReactDOM: Object = {
739 unstable_flushControlled: DOMRenderer.flushControlled,
740
741 __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: {
742 - // For TapEventPlugin which is popular in open source
743 - EventPluginHub,
744 - // Used by test-utils
745 - EventPluginRegistry,
746 - EventPropagators,
747 - ReactControlledComponent,
748 - ReactDOMComponentTree,
749 - ReactDOMEventListener,
742 + // Keep in sync with ReactDOMUnstableNativeDependencies.js
743 + // and ReactTestUtils.js. This is an array for better minification.
744 + Events: [
745 + ReactDOMComponentTree.getInstanceFromNode,
746 + ReactDOMComponentTree.getNodeFromInstance,
747 + ReactDOMComponentTree.getFiberCurrentPropsFromNode,
748 + EventPluginRegistry.eventNameDispatchConfigs,
749 + EventPropagators.accumulateTwoPhaseDispatches,
750 + EventPropagators.accumulateDirectDispatches,
751 + ReactControlledComponent.enqueueStateRestore,
752 + ReactControlledComponent.restoreStateIfNeeded,
753 + ReactDOMEventListener.dispatchEvent,
754 + EventPluginHub.runEventsInBatch,
755 + ],
756 },
757 };
758
packages/react-dom/src/client/ReactDOMFB.js
+2
@@ -7,6 +7,7 @@
7 * @flow
8 */
9
10 +import * as EventPluginHub from 'events/EventPluginHub';
11 import * as ReactFiberTreeReflection from 'react-reconciler/reflection';
12 import * as ReactInstanceMap from 'shared/ReactInstanceMap';
13 import {addUserTimingListener} from 'shared/ReactFeatureFlags';
@@ -25,6 +26,7 @@ Object.assign(
26 ReactDOMComponentTree,
27 ReactInstanceMap,
28 // Used by www msite:
29 + EventPluginHub,
30 TapEventPlugin,
31 // Perf experiment
32 addUserTimingListener,
packages/react-dom/src/test-utils/ReactTestUtils.js
+25 -31
@@ -24,14 +24,22 @@ import {ELEMENT_NODE} from '../shared/HTMLNodeType';
24 import * as DOMTopLevelEventTypes from '../events/DOMTopLevelEventTypes';
25
26 const {findDOMNode} = ReactDOM;
27 -const {
28 - EventPluginHub,
29 - EventPluginRegistry,
30 - EventPropagators,
31 - ReactControlledComponent,
32 - ReactDOMComponentTree,
33 - ReactDOMEventListener,
34 -} = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
27 +// Keep in sync with ReactDOMUnstableNativeDependencies.js
28 +// and ReactDOM.js:
29 +const [
30 + getInstanceFromNode,
31 + /* eslint-disable no-unused-vars */
32 + getNodeFromInstance,
33 + getFiberCurrentPropsFromNode,
34 + /* eslint-enable no-unused-vars */
35 + eventNameDispatchConfigs,
36 + accumulateTwoPhaseDispatches,
37 + accumulateDirectDispatches,
38 + enqueueStateRestore,
39 + restoreStateIfNeeded,
40 + dispatchEvent,
41 + runEventsInBatch,
42 +] = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Events;
43
44 function Event(suffix) {}
45
@@ -50,7 +58,7 @@ let hasWarnedAboutDeprecatedMockComponent = false;
58 */
59 function simulateNativeEventOnNode(topLevelType, node, fakeNativeEvent) {
60 fakeNativeEvent.target = node;
53 - ReactDOMEventListener.dispatchEvent(topLevelType, fakeNativeEvent);
61 + dispatchEvent(topLevelType, fakeNativeEvent);
62 }
63
64 /**
@@ -399,8 +407,7 @@ function makeSimulator(eventType) {
407 'a component instance. Pass the DOM node you wish to simulate the event on instead.',
408 );
409
402 - const dispatchConfig =
403 - EventPluginRegistry.eventNameDispatchConfigs[eventType];
410 + const dispatchConfig = eventNameDispatchConfigs[eventType];
411
412 const fakeNativeEvent = new Event();
413 fakeNativeEvent.target = domNode;
@@ -408,7 +415,7 @@ function makeSimulator(eventType) {
415
416 // We don't use SyntheticEvent.getPooled in order to not have to worry about
417 // properly destroying any properties assigned from `eventData` upon release
411 - const targetInst = ReactDOMComponentTree.getInstanceFromNode(domNode);
418 + const targetInst = getInstanceFromNode(domNode);
419 const event = new SyntheticEvent(
420 dispatchConfig,
421 targetInst,
@@ -422,18 +429,18 @@ function makeSimulator(eventType) {
429 Object.assign(event, eventData);
430
431 if (dispatchConfig.phasedRegistrationNames) {
425 - EventPropagators.accumulateTwoPhaseDispatches(event);
432 + accumulateTwoPhaseDispatches(event);
433 } else {
427 - EventPropagators.accumulateDirectDispatches(event);
434 + accumulateDirectDispatches(event);
435 }
436
437 ReactDOM.unstable_batchedUpdates(function() {
438 // Normally extractEvent enqueues a state restore, but we'll just always
439 // do that since we we're by-passing it here.
433 - ReactControlledComponent.enqueueStateRestore(domNode);
434 - EventPluginHub.runEventsInBatch(event, true);
440 + enqueueStateRestore(domNode);
441 + runEventsInBatch(event, true);
442 });
436 - ReactControlledComponent.restoreStateIfNeeded();
443 + restoreStateIfNeeded();
444 };
445 }
446
@@ -441,7 +448,7 @@ function buildSimulators() {
448 ReactTestUtils.Simulate = {};
449
450 let eventType;
444 - for (eventType in EventPluginRegistry.eventNameDispatchConfigs) {
451 + for (eventType in eventNameDispatchConfigs) {
452 /**
453 * @param {!Element|ReactDOMComponent} domComponentOrNode
454 * @param {?object} eventData Fake event data to use in SyntheticEvent.
@@ -450,19 +457,6 @@ function buildSimulators() {
457 }
458 }
459
453 -// Rebuild ReactTestUtils.Simulate whenever event plugins are injected
454 -const oldInjectEventPluginOrder =
455 - EventPluginHub.injection.injectEventPluginOrder;
456 -EventPluginHub.injection.injectEventPluginOrder = function() {
457 - oldInjectEventPluginOrder.apply(this, arguments);
458 - buildSimulators();
459 -};
460 -const oldInjectEventPlugins = EventPluginHub.injection.injectEventPluginsByName;
461 -EventPluginHub.injection.injectEventPluginsByName = function() {
462 - oldInjectEventPlugins.apply(this, arguments);
463 - buildSimulators();
464 -};
465 -
460 buildSimulators();
461
462 /**
packages/react-dom/src/unstable-native-dependencies/ReactDOMUnstableNativeDependencies.js
+9 -6
@@ -22,11 +22,14 @@ export function injectComponentTree(ComponentTree) {
22 export {ResponderEventPlugin, ResponderTouchHistoryStore};
23
24 // Inject react-dom's ComponentTree into this module.
25 -const {
26 - ReactDOMComponentTree,
27 -} = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
25 +// Keep in sync with ReactDOM.js and ReactTestUtils.js:
26 +const [
27 + getInstanceFromNode,
28 + getNodeFromInstance,
29 + getFiberCurrentPropsFromNode,
30 +] = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Events;
31 EventPluginUtils.setComponentTree(
29 - ReactDOMComponentTree.getFiberCurrentPropsFromNode,
30 - ReactDOMComponentTree.getInstanceFromNode,
31 - ReactDOMComponentTree.getNodeFromInstance,
32 + getFiberCurrentPropsFromNode,
33 + getInstanceFromNode,
34 + getNodeFromInstance,
35 );