Skip to content

Commit 8226759

Browse files
committed
Expose less internals for TestUtils
1 parent 28b9289 commit 8226759

File tree

4 files changed

+48
-47
lines changed

4 files changed

+48
-47
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,7 @@ describe('ResponderEventPlugin', () => {
395395

396396
const ReactDOM = require('react-dom');
397397
const ReactDOMUnstableNativeDependencies = require('react-dom/unstable-native-dependencies');
398-
EventPluginHub =
399-
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
400-
.EventPluginHub;
398+
EventPluginHub = require('events/EventPluginHub');
401399
const injectComponentTree =
402400
ReactDOMUnstableNativeDependencies.injectComponentTree;
403401
ResponderEventPlugin =

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -739,14 +739,19 @@ 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,
744742
// Used by test-utils
745-
EventPluginRegistry,
746-
EventPropagators,
747-
ReactControlledComponent,
748-
ReactDOMComponentTree,
749-
ReactDOMEventListener,
743+
Events: [
744+
EventPluginRegistry.eventNameDispatchConfigs,
745+
EventPropagators.accumulateTwoPhaseDispatches,
746+
EventPropagators.accumulateDirectDispatches,
747+
ReactControlledComponent.enqueueStateRestore,
748+
ReactControlledComponent.restoreStateIfNeeded,
749+
ReactDOMEventListener.dispatchEvent,
750+
EventPluginHub.runEventsInBatch,
751+
ReactDOMComponentTree.getFiberCurrentPropsFromNode,
752+
ReactDOMComponentTree.getInstanceFromNode,
753+
ReactDOMComponentTree.getNodeFromInstance,
754+
],
750755
},
751756
};
752757

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

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ 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+
const [
28+
eventNameDispatchConfigs,
29+
accumulateTwoPhaseDispatches,
30+
accumulateDirectDispatches,
31+
enqueueStateRestore,
32+
restoreStateIfNeeded,
33+
dispatchEvent,
34+
runEventsInBatch, // getFiberCurrentPropsFromNode
35+
,
36+
getInstanceFromNode,
37+
] = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Events;
3538

3639
function Event(suffix) {}
3740

@@ -50,7 +53,7 @@ let hasWarnedAboutDeprecatedMockComponent = false;
5053
*/
5154
function simulateNativeEventOnNode(topLevelType, node, fakeNativeEvent) {
5255
fakeNativeEvent.target = node;
53-
ReactDOMEventListener.dispatchEvent(topLevelType, fakeNativeEvent);
56+
dispatchEvent(topLevelType, fakeNativeEvent);
5457
}
5558

5659
/**
@@ -399,16 +402,15 @@ function makeSimulator(eventType) {
399402
'a component instance. Pass the DOM node you wish to simulate the event on instead.',
400403
);
401404

402-
const dispatchConfig =
403-
EventPluginRegistry.eventNameDispatchConfigs[eventType];
405+
const dispatchConfig = eventNameDispatchConfigs[eventType];
404406

405407
const fakeNativeEvent = new Event();
406408
fakeNativeEvent.target = domNode;
407409
fakeNativeEvent.type = eventType.toLowerCase();
408410

409411
// We don't use SyntheticEvent.getPooled in order to not have to worry about
410412
// properly destroying any properties assigned from `eventData` upon release
411-
const targetInst = ReactDOMComponentTree.getInstanceFromNode(domNode);
413+
const targetInst = getInstanceFromNode(domNode);
412414
const event = new SyntheticEvent(
413415
dispatchConfig,
414416
targetInst,
@@ -422,26 +424,26 @@ function makeSimulator(eventType) {
422424
Object.assign(event, eventData);
423425

424426
if (dispatchConfig.phasedRegistrationNames) {
425-
EventPropagators.accumulateTwoPhaseDispatches(event);
427+
accumulateTwoPhaseDispatches(event);
426428
} else {
427-
EventPropagators.accumulateDirectDispatches(event);
429+
accumulateDirectDispatches(event);
428430
}
429431

430432
ReactDOM.unstable_batchedUpdates(function() {
431433
// Normally extractEvent enqueues a state restore, but we'll just always
432434
// do that since we we're by-passing it here.
433-
ReactControlledComponent.enqueueStateRestore(domNode);
434-
EventPluginHub.runEventsInBatch(event, true);
435+
enqueueStateRestore(domNode);
436+
runEventsInBatch(event, true);
435437
});
436-
ReactControlledComponent.restoreStateIfNeeded();
438+
restoreStateIfNeeded();
437439
};
438440
}
439441

440442
function buildSimulators() {
441443
ReactTestUtils.Simulate = {};
442444

443445
let eventType;
444-
for (eventType in EventPluginRegistry.eventNameDispatchConfigs) {
446+
for (eventType in eventNameDispatchConfigs) {
445447
/**
446448
* @param {!Element|ReactDOMComponent} domComponentOrNode
447449
* @param {?object} eventData Fake event data to use in SyntheticEvent.
@@ -450,19 +452,6 @@ function buildSimulators() {
450452
}
451453
}
452454

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-
466455
buildSimulators();
467456

468457
/**

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,20 @@ 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+
const [
26+
// eventNameDispatchConfigs,
27+
// accumulateTwoPhaseDispatches,
28+
// accumulateDirectDispatches,
29+
// enqueueStateRestore,
30+
// restoreStateIfNeeded,
31+
// dispatchEvent,
32+
// runEventsInBatch,
33+
getFiberCurrentPropsFromNode,
34+
getInstanceFromNode,
35+
getNodeFromInstance,
36+
] = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Events;
2837
EventPluginUtils.setComponentTree(
29-
ReactDOMComponentTree.getFiberCurrentPropsFromNode,
30-
ReactDOMComponentTree.getInstanceFromNode,
31-
ReactDOMComponentTree.getNodeFromInstance,
38+
getFiberCurrentPropsFromNode,
39+
getInstanceFromNode,
40+
getNodeFromInstance,
3241
);

0 commit comments

Comments
 (0)