diff --git a/jest/MockNativeMethods.js b/jest/MockNativeMethods.js new file mode 100644 index 0000000000..9b44fc45fe --- /dev/null +++ b/jest/MockNativeMethods.js @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2013-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +'use strict'; + +const mockNativeFunction = methodName => { + let warned = false; + return function() { + if (warned) { + return; + } + warned = true; + console.warn( + 'Calling .' + + methodName + + '() in the test renderer environment is not supported. Instead, mock ' + + 'out your components that use findNodeHandle with replacements that ' + + "don't rely on the native environment.", + ); + }; +}; + +const MockNativeMethods = { + measure: mockNativeFunction('measure'), + measureInWindow: mockNativeFunction('measureInWindow'), + measureLayout: mockNativeFunction('measureLayout'), + setNativeProps: mockNativeFunction('setNativeProps'), + focus: mockNativeFunction('focus'), + blur: mockNativeFunction('blur'), +}; + +module.exports = MockNativeMethods; diff --git a/jest/setup.js b/jest/setup.js index 25fed46877..b40bcebae2 100644 --- a/jest/setup.js +++ b/jest/setup.js @@ -6,6 +6,7 @@ */ 'use strict'; +const MockNativeMethods = require.requireActual('./MockNativeMethods'); const mockComponent = require.requireActual('./mockComponent'); require.requireActual('../Libraries/polyfills/babelHelpers.js'); @@ -81,33 +82,9 @@ jest const NativeMethodsMixin = ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.NativeMethodsMixin; - const mockFunction = (key) => { - let warned = false; - return function() { - if (warned) { - return; - } - warned = true; - console.warn( - 'Calling .' + key + '() in the test renderer environment is not ' + - 'supported. Instead, mock out your components that use ' + - 'findNodeHandle with replacements that don\'t rely on the ' + - 'native environment.', - ); - }; - }; + Object.assign(NativeMethodsMixin, MockNativeMethods); + Object.assign(ReactNative.NativeComponent.prototype, MockNativeMethods); - [ - 'measure', - 'measureInWindow', - 'measureLayout', - 'setNativeProps', - 'focus', - 'blur', - ].forEach((key) => { - NativeMethodsMixin[key] = mockFunction(key); - ReactNative.NativeComponent.prototype[key] = mockFunction(key); - }); return ReactNative; }) .mock('ensureComponentIsNative', () => () => true);