Skip to content

Commit 3c1dcd3

Browse files
authored
Expose less internals for TestUtils (facebook#13539)
* Expose less internals for TestUtils * Keep EventPluginHub for www * Reorder to simplify
1 parent 0b74e95 commit 3c1dcd3

File tree

5 files changed

+51
-49
lines changed

5 files changed

+51
-49
lines changed

packages/events/__tests__/ResponderEventPlugin-test.internal.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,8 @@ describe('ResponderEventPlugin', () => {
393393
beforeEach(() => {
394394
jest.resetModules();
395395

396-
const ReactDOM = require('react-dom');
397396
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');
401398
const injectComponentTree =
402399
ReactDOMUnstableNativeDependencies.injectComponentTree;
403400
ResponderEventPlugin =

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -739,14 +739,20 @@ const ReactDOM: Object = {
739739
unstable_flushControlled: DOMRenderer.flushControlled,
740740

741741
__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+
],
750756
},
751757
};
752758

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @flow
88
*/
99

10+
import * as EventPluginHub from 'events/EventPluginHub';
1011
import * as ReactFiberTreeReflection from 'react-reconciler/reflection';
1112
import * as ReactInstanceMap from 'shared/ReactInstanceMap';
1213
import {addUserTimingListener} from 'shared/ReactFeatureFlags';
@@ -25,6 +26,7 @@ Object.assign(
2526
ReactDOMComponentTree,
2627
ReactInstanceMap,
2728
// Used by www msite:
29+
EventPluginHub,
2830
TapEventPlugin,
2931
// Perf experiment
3032
addUserTimingListener,

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

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,22 @@ import {ELEMENT_NODE} from '../shared/HTMLNodeType';
2424
import * as DOMTopLevelEventTypes from '../events/DOMTopLevelEventTypes';
2525

2626
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;
3543

3644
function Event(suffix) {}
3745

@@ -50,7 +58,7 @@ let hasWarnedAboutDeprecatedMockComponent = false;
5058
*/
5159
function simulateNativeEventOnNode(topLevelType, node, fakeNativeEvent) {
5260
fakeNativeEvent.target = node;
53-
ReactDOMEventListener.dispatchEvent(topLevelType, fakeNativeEvent);
61+
dispatchEvent(topLevelType, fakeNativeEvent);
5462
}
5563

5664
/**
@@ -399,16 +407,15 @@ function makeSimulator(eventType) {
399407
'a component instance. Pass the DOM node you wish to simulate the event on instead.',
400408
);
401409

402-
const dispatchConfig =
403-
EventPluginRegistry.eventNameDispatchConfigs[eventType];
410+
const dispatchConfig = eventNameDispatchConfigs[eventType];
404411

405412
const fakeNativeEvent = new Event();
406413
fakeNativeEvent.target = domNode;
407414
fakeNativeEvent.type = eventType.toLowerCase();
408415

409416
// We don't use SyntheticEvent.getPooled in order to not have to worry about
410417
// properly destroying any properties assigned from `eventData` upon release
411-
const targetInst = ReactDOMComponentTree.getInstanceFromNode(domNode);
418+
const targetInst = getInstanceFromNode(domNode);
412419
const event = new SyntheticEvent(
413420
dispatchConfig,
414421
targetInst,
@@ -422,26 +429,26 @@ function makeSimulator(eventType) {
422429
Object.assign(event, eventData);
423430

424431
if (dispatchConfig.phasedRegistrationNames) {
425-
EventPropagators.accumulateTwoPhaseDispatches(event);
432+
accumulateTwoPhaseDispatches(event);
426433
} else {
427-
EventPropagators.accumulateDirectDispatches(event);
434+
accumulateDirectDispatches(event);
428435
}
429436

430437
ReactDOM.unstable_batchedUpdates(function() {
431438
// Normally extractEvent enqueues a state restore, but we'll just always
432439
// 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);
435442
});
436-
ReactControlledComponent.restoreStateIfNeeded();
443+
restoreStateIfNeeded();
437444
};
438445
}
439446

440447
function buildSimulators() {
441448
ReactTestUtils.Simulate = {};
442449

443450
let eventType;
444-
for (eventType in EventPluginRegistry.eventNameDispatchConfigs) {
451+
for (eventType in eventNameDispatchConfigs) {
445452
/**
446453
* @param {!Element|ReactDOMComponent} domComponentOrNode
447454
* @param {?object} eventData Fake event data to use in SyntheticEvent.
@@ -450,19 +457,6 @@ function buildSimulators() {
450457
}
451458
}
452459

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-
466460
buildSimulators();
467461

468462
/**

packages/react-dom/src/unstable-native-dependencies/ReactDOMUnstableNativeDependencies.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ export function injectComponentTree(ComponentTree) {
2222
export {ResponderEventPlugin, ResponderTouchHistoryStore};
2323

2424
// 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;
2831
EventPluginUtils.setComponentTree(
29-
ReactDOMComponentTree.getFiberCurrentPropsFromNode,
30-
ReactDOMComponentTree.getInstanceFromNode,
31-
ReactDOMComponentTree.getNodeFromInstance,
32+
getFiberCurrentPropsFromNode,
33+
getInstanceFromNode,
34+
getNodeFromInstance,
3235
);

0 commit comments

Comments
 (0)