Skip to content

Commit

Permalink
Mock ReactNative.NativeComponent native methods in jest
Browse files Browse the repository at this point in the history
Reviewed By: yungsters

Differential Revision: D7218964

fbshipit-source-id: f4b25a533b7e150c978863ff8411dc80937a4fed
  • Loading branch information
elicwhite authored and facebook-github-bot committed Mar 9, 2018
1 parent b6b80f6 commit 3e9a371
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
})
Expand Down

0 comments on commit 3e9a371

Please sign in to comment.