From 3e9a371ace5f25b2eb7a0d30177251f8a0c10ed9 Mon Sep 17 00:00:00 2001 From: Eli White Date: Fri, 9 Mar 2018 13:09:12 -0800 Subject: [PATCH] Mock ReactNative.NativeComponent native methods in jest Reviewed By: yungsters Differential Revision: D7218964 fbshipit-source-id: f4b25a533b7e150c978863ff8411dc80937a4fed --- jest/setup.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/jest/setup.js b/jest/setup.js index 680e391ba..0d4553b3f 100644 --- a/jest/setup.js +++ b/jest/setup.js @@ -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; })