From b025c43487d72fa8a1d92608702e86148b459f0b Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 27 Mar 2017 15:46:18 +0100 Subject: [PATCH] Added init() dev block to ReactTestUtils --- scripts/rollup/build.js | 1 + src/test/ReactTestUtils.js | 60 ++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js index d5e81b0a25..3b1a10c1b2 100644 --- a/scripts/rollup/build.js +++ b/scripts/rollup/build.js @@ -184,6 +184,7 @@ function uglifyConfig(mangle) { }, mangle: mangle ? { screw_ie8: true, + toplevel: true, } : false, }; } diff --git a/src/test/ReactTestUtils.js b/src/test/ReactTestUtils.js index d3190de877..8ea04565bd 100644 --- a/src/test/ReactTestUtils.js +++ b/src/test/ReactTestUtils.js @@ -486,19 +486,7 @@ function buildSimulators() { } } -// Rebuild ReactTestUtils.Simulate whenever event plugins are injected -var oldInjectEventPluginOrder = EventPluginHub.injection.injectEventPluginOrder; -EventPluginHub.injection.injectEventPluginOrder = function() { - oldInjectEventPluginOrder.apply(this, arguments); - buildSimulators(); -}; -var oldInjectEventPlugins = EventPluginHub.injection.injectEventPluginsByName; -EventPluginHub.injection.injectEventPluginsByName = function() { - oldInjectEventPlugins.apply(this, arguments); - buildSimulators(); -}; -buildSimulators(); /** * Exports: @@ -537,18 +525,40 @@ function makeNativeSimulator(eventType) { }; } -Object.keys(topLevelTypes).forEach(function(eventType) { - // Event type is stored as 'topClick' - we transform that to 'click' - var convenienceName = eventType.indexOf('top') === 0 - ? eventType.charAt(3).toLowerCase() + eventType.substr(4) - : eventType; - /** - * @param {!Element|ReactDOMComponent} domComponentOrNode - * @param {?Event} nativeEventData Fake native event to use in SyntheticEvent. - */ - ReactTestUtils.SimulateNative[convenienceName] = makeNativeSimulator( - eventType, - ); -}); +function init() { + // Rebuild ReactTestUtils.Simulate whenever event plugins are injected + var oldInjectEventPluginOrder = EventPluginHub.injection.injectEventPluginOrder; + EventPluginHub.injection.injectEventPluginOrder = function() { + oldInjectEventPluginOrder.apply(this, arguments); + buildSimulators(); + }; + var oldInjectEventPlugins = EventPluginHub.injection.injectEventPluginsByName; + EventPluginHub.injection.injectEventPluginsByName = function() { + oldInjectEventPlugins.apply(this, arguments); + buildSimulators(); + }; + + buildSimulators(); + + Object.keys(topLevelTypes).forEach(function(eventType) { + // Event type is stored as 'topClick' - we transform that to 'click' + var convenienceName = eventType.indexOf('top') === 0 + ? eventType.charAt(3).toLowerCase() + eventType.substr(4) + : eventType; + /** + * @param {!Element|ReactDOMComponent} domComponentOrNode + * @param {?Event} nativeEventData Fake native event to use in SyntheticEvent. + */ + ReactTestUtils.SimulateNative[convenienceName] = makeNativeSimulator( + eventType, + ); + }); +} + +if (__DEV__) { + init(); +} else { + throw new Error('ReactTestUtils requires DEV'); +} module.exports = ReactTestUtils;