RN: Refactor `MockNativeMethods` in Jest
Reviewed By: sahrens Differential Revision: D7917498 fbshipit-source-id: 97636080588bf2641a56256688cb0f2ec81ae463
This commit is contained in:
parent
67dbcbd57e
commit
5d4c542c58
|
@ -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;
|
|
@ -6,6 +6,7 @@
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const MockNativeMethods = require.requireActual('./MockNativeMethods');
|
||||||
const mockComponent = require.requireActual('./mockComponent');
|
const mockComponent = require.requireActual('./mockComponent');
|
||||||
|
|
||||||
require.requireActual('../Libraries/polyfills/babelHelpers.js');
|
require.requireActual('../Libraries/polyfills/babelHelpers.js');
|
||||||
|
@ -81,33 +82,9 @@ jest
|
||||||
const NativeMethodsMixin =
|
const NativeMethodsMixin =
|
||||||
ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.NativeMethodsMixin;
|
ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.NativeMethodsMixin;
|
||||||
|
|
||||||
const mockFunction = (key) => {
|
Object.assign(NativeMethodsMixin, MockNativeMethods);
|
||||||
let warned = false;
|
Object.assign(ReactNative.NativeComponent.prototype, MockNativeMethods);
|
||||||
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.',
|
|
||||||
);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
[
|
|
||||||
'measure',
|
|
||||||
'measureInWindow',
|
|
||||||
'measureLayout',
|
|
||||||
'setNativeProps',
|
|
||||||
'focus',
|
|
||||||
'blur',
|
|
||||||
].forEach((key) => {
|
|
||||||
NativeMethodsMixin[key] = mockFunction(key);
|
|
||||||
ReactNative.NativeComponent.prototype[key] = mockFunction(key);
|
|
||||||
});
|
|
||||||
return ReactNative;
|
return ReactNative;
|
||||||
})
|
})
|
||||||
.mock('ensureComponentIsNative', () => () => true);
|
.mock('ensureComponentIsNative', () => () => true);
|
||||||
|
|
Loading…
Reference in New Issue