Mock ReactNative.NativeComponent native methods in jest

Reviewed By: yungsters

Differential Revision: D7218964

fbshipit-source-id: f4b25a533b7e150c978863ff8411dc80937a4fed
This commit is contained in:
Eli White 2018-03-09 13:09:12 -08:00 committed by Facebook Github Bot
parent b6b80f6a70
commit 3e9a371ace
1 changed files with 15 additions and 9 deletions

View File

@ -80,16 +80,10 @@ jest
const ReactNative = require.requireActual('ReactNative');
const NativeMethodsMixin =
ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.NativeMethodsMixin;
[
'measure',
'measureInWindow',
'measureLayout',
'setNativeProps',
'focus',
'blur',
].forEach((key) => {
const mockFunction = (key) => {
let warned = false;
NativeMethodsMixin[key] = function() {
return function() {
if (warned) {
return;
}
@ -101,6 +95,18 @@ jest
'native environment.',
);
};
};
[
'measure',
'measureInWindow',
'measureLayout',
'setNativeProps',
'focus',
'blur',
].forEach((key) => {
NativeMethodsMixin[key] = mockFunction(key);
ReactNative.NativeComponent.prototype[key] = mockFunction(key);
});
return ReactNative;
})