Skip to content

Commit 73622eb

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

File tree

4 files changed

+49
-48
lines changed

4 files changed

+49
-48
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: 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: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ 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,
35+
// eslint-disable-next-line no-unused-vars
36+
getFiberCurrentPropsFromNode,
37+
getInstanceFromNode,
38+
] = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Events;
3539

3640
function Event(suffix) {}
3741

@@ -50,7 +54,7 @@ let hasWarnedAboutDeprecatedMockComponent = false;
5054
*/
5155
function simulateNativeEventOnNode(topLevelType, node, fakeNativeEvent) {
5256
fakeNativeEvent.target = node;
53-
ReactDOMEventListener.dispatchEvent(topLevelType, fakeNativeEvent);
57+
dispatchEvent(topLevelType, fakeNativeEvent);
5458
}
5559

5660
/**
@@ -399,16 +403,15 @@ function makeSimulator(eventType) {
399403
'a component instance. Pass the DOM node you wish to simulate the event on instead.',
400404
);
401405

402-
const dispatchConfig =
403-
EventPluginRegistry.eventNameDispatchConfigs[eventType];
406+
const dispatchConfig = eventNameDispatchConfigs[eventType];
404407

405408
const fakeNativeEvent = new Event();
406409
fakeNativeEvent.target = domNode;
407410
fakeNativeEvent.type = eventType.toLowerCase();
408411

409412
// We don't use SyntheticEvent.getPooled in order to not have to worry about
410413
// properly destroying any properties assigned from `eventData` upon release
411-
const targetInst = ReactDOMComponentTree.getInstanceFromNode(domNode);
414+
const targetInst = getInstanceFromNode(domNode);
412415
const event = new SyntheticEvent(
413416
dispatchConfig,
414417
targetInst,
@@ -422,26 +425,26 @@ function makeSimulator(eventType) {
422425
Object.assign(event, eventData);
423426

424427
if (dispatchConfig.phasedRegistrationNames) {
425-
EventPropagators.accumulateTwoPhaseDispatches(event);
428+
accumulateTwoPhaseDispatches(event);
426429
} else {
427-
EventPropagators.accumulateDirectDispatches(event);
430+
accumulateDirectDispatches(event);
428431
}
429432

430433
ReactDOM.unstable_batchedUpdates(function() {
431434
// Normally extractEvent enqueues a state restore, but we'll just always
432435
// do that since we we're by-passing it here.
433-
ReactControlledComponent.enqueueStateRestore(domNode);
434-
EventPluginHub.runEventsInBatch(event, true);
436+
enqueueStateRestore(domNode);
437+
runEventsInBatch(event, true);
435438
});
436-
ReactControlledComponent.restoreStateIfNeeded();
439+
restoreStateIfNeeded();
437440
};
438441
}
439442

440443
function buildSimulators() {
441444
ReactTestUtils.Simulate = {};
442445

443446
let eventType;
444-
for (eventType in EventPluginRegistry.eventNameDispatchConfigs) {
447+
for (eventType in eventNameDispatchConfigs) {
445448
/**
446449
* @param {!Element|ReactDOMComponent} domComponentOrNode
447450
* @param {?object} eventData Fake event data to use in SyntheticEvent.
@@ -450,19 +453,6 @@ function buildSimulators() {
450453
}
451454
}
452455

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-
466456
buildSimulators();
467457

468458
/**

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)