RN: Refactor `MockNativeMethods` in Jest

Reviewed By: sahrens

Differential Revision: D7917498

fbshipit-source-id: 97636080588bf2641a56256688cb0f2ec81ae463
This commit is contained in:
Tim Yung 2018-05-09 00:47:45 -07:00 committed by Facebook Github Bot
parent 67dbcbd57e
commit 5d4c542c58
2 changed files with 41 additions and 26 deletions

38
jest/MockNativeMethods.js Normal file
View File

@ -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;

View File

@ -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);