diff --git a/android/src/main/java/com/instabug/reactlibrary/RNInstabugBugReportingModule.java b/android/src/main/java/com/instabug/reactlibrary/RNInstabugBugReportingModule.java index 5a0de326e..1f0b6ddac 100644 --- a/android/src/main/java/com/instabug/reactlibrary/RNInstabugBugReportingModule.java +++ b/android/src/main/java/com/instabug/reactlibrary/RNInstabugBugReportingModule.java @@ -6,7 +6,6 @@ import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.WritableMap; @@ -20,15 +19,14 @@ import com.instabug.library.invocation.util.InstabugFloatingButtonEdge; import com.instabug.library.invocation.util.InstabugVideoRecordingButtonPosition; import com.instabug.reactlibrary.utils.ArrayUtil; -import com.instabug.reactlibrary.utils.InstabugUtil; +import com.instabug.reactlibrary.utils.EventEmitterModule; import com.instabug.reactlibrary.utils.MainThreadHandler; import java.util.ArrayList; import javax.annotation.Nonnull; -public class RNInstabugBugReportingModule extends ReactContextBaseJavaModule { - +public class RNInstabugBugReportingModule extends EventEmitterModule { public RNInstabugBugReportingModule(ReactApplicationContext reactContext) { super(reactContext); } @@ -39,6 +37,16 @@ public String getName() { return "IBGBugReporting"; } + @ReactMethod + public void addListener(String event) { + super.addListener(event); + } + + @ReactMethod + public void removeListeners(Integer count) { + super.removeListeners(count); + } + /** * Enable or disable all BugReporting related features. * @param isEnabled boolean indicating enabled or disabled. @@ -235,10 +243,10 @@ public void setOnInvokeHandler(final Callback onInvokeHandler) { @Override public void run() { try { - BugReporting.setOnInvokeCallback(new OnInvokeCallback() { + BugReporting.setOnInvokeCallback(new OnInvokeCallback() { @Override public void onInvoke() { - InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_PRE_INVOCATION_HANDLER, null); + sendEvent(Constants.IBG_PRE_INVOCATION_HANDLER, null); } }); } catch (java.lang.Exception exception) { @@ -286,7 +294,7 @@ public void call(DismissType dismissType, ReportType reportType) { WritableMap params = Arguments.createMap(); params.putString("dismissType", dismissType.toString()); params.putString("reportType", reportType.toString()); - InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_POST_INVOCATION_HANDLER, params); + sendEvent(Constants.IBG_POST_INVOCATION_HANDLER, params); } }); } catch (java.lang.Exception exception) { diff --git a/android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java b/android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java index 85d29c05a..ee3b9aa59 100644 --- a/android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java +++ b/android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java @@ -1,42 +1,32 @@ package com.instabug.reactlibrary; import android.annotation.SuppressLint; -import android.app.Application; import android.graphics.Bitmap; import android.net.Uri; -import android.os.Handler; -import android.os.Looper; import android.util.Log; import android.view.View; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReadableArray; -import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.WritableArray; import com.facebook.react.bridge.WritableNativeArray; import com.facebook.react.bridge.WritableMap; import com.facebook.react.bridge.WritableNativeMap; import com.facebook.react.bridge.Callback; -import com.facebook.react.modules.core.DeviceEventManagerModule; import com.facebook.react.uimanager.NativeViewHierarchyManager; import com.facebook.react.uimanager.UIBlock; import com.facebook.react.uimanager.UIManagerModule; import com.instabug.apm.APM; import com.instabug.bug.BugReporting; import com.instabug.bug.instabugdisclaimer.Internal; -import com.instabug.bug.invocation.Option; import com.instabug.chat.Replies; -import com.instabug.crash.CrashReporting; import com.instabug.featuresrequest.FeatureRequests; -import com.instabug.featuresrequest.ActionType; import com.instabug.library.Feature; import com.instabug.library.Instabug; -import com.instabug.library.InstabugState; import com.instabug.library.OnSdkDismissCallback; import com.instabug.library.Platform; import com.instabug.library.LogLevel; @@ -45,8 +35,6 @@ import com.instabug.library.invocation.InstabugInvocationEvent; import com.instabug.library.InstabugColorTheme; import com.instabug.library.invocation.OnInvokeCallback; -import com.instabug.library.invocation.util.InstabugFloatingButtonEdge; -import com.instabug.library.invocation.util.InstabugVideoRecordingButtonPosition; import com.instabug.library.logging.InstabugLog; import com.instabug.library.ui.onboarding.WelcomeMessage; import com.instabug.library.InstabugCustomTextPlaceHolder; @@ -55,6 +43,7 @@ import com.instabug.library.visualusersteps.State; import com.instabug.reactlibrary.utils.ArrayUtil; +import com.instabug.reactlibrary.utils.EventEmitterModule; import com.instabug.reactlibrary.utils.InstabugUtil; import com.instabug.reactlibrary.utils.MainThreadHandler; import com.instabug.reactlibrary.utils.ReportUtil; @@ -84,7 +73,7 @@ /** * The type Rn instabug reactnative module. */ -public class RNInstabugReactnativeModule extends ReactContextBaseJavaModule { +public class RNInstabugReactnativeModule extends EventEmitterModule { private static final String TAG = RNInstabugReactnativeModule.class.getSimpleName(); @@ -98,7 +87,7 @@ public class RNInstabugReactnativeModule extends ReactContextBaseJavaModule { */ public RNInstabugReactnativeModule(ReactApplicationContext reactContext) { super(reactContext); - //init placHolders + //init placeHolders placeHolders = new InstabugCustomTextPlaceHolder(); } @@ -106,7 +95,18 @@ public RNInstabugReactnativeModule(ReactApplicationContext reactContext) { public String getName() { return "Instabug"; } - + + + @ReactMethod + public void addListener(String event) { + super.addListener(event); + } + + @ReactMethod + public void removeListeners(Integer count) { + super.removeListeners(count); + } + /** * Enables or disables Instabug functionality. * @param isEnabled A boolean to enable/disable Instabug. @@ -953,7 +953,7 @@ public void run() { BugReporting.setOnInvokeCallback(new OnInvokeCallback() { @Override public void onInvoke() { - sendEvent(getReactApplicationContext(), "IBGpreInvocationHandler", null); + sendEvent("IBGpreInvocationHandler", null); } }); } catch (java.lang.Exception exception) { @@ -986,7 +986,7 @@ public void onReportCreated(Report report) { reportParam.putString("userData", report.getUserData()); reportParam.putMap("userAttributes", convertFromHashMapToWriteableMap(report.getUserAttributes())); reportParam.putMap("fileAttachments", convertFromHashMapToWriteableMap(report.getFileAttachments())); - sendEvent(getReactApplicationContext(), "IBGpreSendingHandler", reportParam); + sendEvent("IBGpreSendingHandler", reportParam); currentReport = report; } }); @@ -1159,7 +1159,7 @@ public void call(DismissType dismissType, ReportType reportType) { WritableMap params = Arguments.createMap(); params.putString("dismissType", dismissType.toString()); params.putString("reportType", reportType.toString()); - sendEvent(getReactApplicationContext(), "IBGpostInvocationHandler", params); + sendEvent("IBGpostInvocationHandler", params); } }); } catch (java.lang.Exception exception) { @@ -1203,7 +1203,7 @@ public void run() { Surveys.setOnShowCallback(new OnShowCallback() { @Override public void onShow() { - sendEvent(getReactApplicationContext(), "IBGWillShowSurvey", null); + sendEvent("IBGWillShowSurvey", null); } }); } @@ -1225,7 +1225,7 @@ public void run() { Surveys.setOnDismissCallback(new OnDismissCallback() { @Override public void onDismiss() { - sendEvent(getReactApplicationContext(), "IBGDidDismissSurvey", null); + sendEvent("IBGDidDismissSurvey", null); } }); } @@ -1347,7 +1347,7 @@ public void run() { Runnable onNewReplyReceivedRunnable = new Runnable() { @Override public void run() { - sendEvent(getReactApplicationContext(), "IBGOnNewReplyReceivedCallback", null); + sendEvent("IBGOnNewReplyReceivedCallback", null); } }; Replies.setOnNewReplyReceivedCallback(onNewReplyReceivedRunnable); @@ -1697,14 +1697,6 @@ public void run() { }); } - private void sendEvent(ReactApplicationContext reactContext, - String eventName, - WritableMap params) { - reactContext - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit(eventName, params); - } - @ReactMethod public void addExperiments(final ReadableArray experiments) { MainThreadHandler.runOnMainThread(new Runnable() { diff --git a/android/src/main/java/com/instabug/reactlibrary/RNInstabugRepliesModule.java b/android/src/main/java/com/instabug/reactlibrary/RNInstabugRepliesModule.java index c83fd61db..25e14575f 100644 --- a/android/src/main/java/com/instabug/reactlibrary/RNInstabugRepliesModule.java +++ b/android/src/main/java/com/instabug/reactlibrary/RNInstabugRepliesModule.java @@ -1,25 +1,21 @@ package com.instabug.reactlibrary; -import android.os.Handler; -import android.os.Looper; - import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableType; import com.facebook.react.bridge.ReadableMapKeySetIterator; import com.instabug.chat.Replies; import com.instabug.library.Feature; -import com.instabug.reactlibrary.utils.InstabugUtil; +import com.instabug.reactlibrary.utils.EventEmitterModule; import com.instabug.reactlibrary.utils.MainThreadHandler; import javax.annotation.Nonnull; import java.util.HashMap; import java.util.Map; -public class RNInstabugRepliesModule extends ReactContextBaseJavaModule { +public class RNInstabugRepliesModule extends EventEmitterModule { public RNInstabugRepliesModule(ReactApplicationContext reactApplicationContext) { super(reactApplicationContext); @@ -31,6 +27,16 @@ public String getName() { return "IBGReplies"; } + @ReactMethod + public void addListener(String event) { + super.addListener(event); + } + + @ReactMethod + public void removeListeners(Integer count) { + super.removeListeners(count); + } + @ReactMethod public void setEnabled(final boolean isEnabled) { MainThreadHandler.runOnMainThread(new Runnable() { @@ -278,7 +284,7 @@ public void run() { Runnable onNewReplyReceivedRunnable = new Runnable() { @Override public void run() { - InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_ON_NEW_REPLY_RECEIVED_CALLBACK, null); + sendEvent(Constants.IBG_ON_NEW_REPLY_RECEIVED_CALLBACK, null); } }; Replies.setOnNewReplyReceivedCallback(onNewReplyReceivedRunnable); diff --git a/android/src/main/java/com/instabug/reactlibrary/RNInstabugSurveysModule.java b/android/src/main/java/com/instabug/reactlibrary/RNInstabugSurveysModule.java index 349d02f67..c3e59be8f 100644 --- a/android/src/main/java/com/instabug/reactlibrary/RNInstabugSurveysModule.java +++ b/android/src/main/java/com/instabug/reactlibrary/RNInstabugSurveysModule.java @@ -1,15 +1,12 @@ package com.instabug.reactlibrary; -import android.os.Handler; -import android.os.Looper; - import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.WritableArray; import com.instabug.library.Feature; import com.instabug.reactlibrary.utils.ArrayUtil; +import com.instabug.reactlibrary.utils.EventEmitterModule; import com.instabug.reactlibrary.utils.InstabugUtil; import com.instabug.reactlibrary.utils.MainThreadHandler; import com.instabug.survey.callbacks.*; @@ -22,7 +19,7 @@ import javax.annotation.Nonnull; -public class RNInstabugSurveysModule extends ReactContextBaseJavaModule { +public class RNInstabugSurveysModule extends EventEmitterModule { public RNInstabugSurveysModule(ReactApplicationContext reactContext) { super(reactContext); @@ -34,6 +31,16 @@ public String getName() { return "IBGSurveys"; } + @ReactMethod + public void addListener(String event) { + super.addListener(event); + } + + @ReactMethod + public void removeListeners(Integer count) { + super.removeListeners(count); + } + /** * Returns true if the survey with a specific token was answered before. * Will return false if the token does not exist or if the survey was not answered before. @@ -137,7 +144,7 @@ public void run() { Surveys.setOnShowCallback(new OnShowCallback() { @Override public void onShow() { - InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_ON_SHOW_SURVEY_HANDLER, null); + sendEvent(Constants.IBG_ON_SHOW_SURVEY_HANDLER, null); } }); } @@ -159,7 +166,7 @@ public void run() { Surveys.setOnDismissCallback(new OnDismissCallback() { @Override public void onDismiss() { - InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_ON_DISMISS_SURVEY_HANDLER, null); + sendEvent(Constants.IBG_ON_DISMISS_SURVEY_HANDLER, null); } }); } diff --git a/android/src/main/java/com/instabug/reactlibrary/utils/EventEmitterModule.java b/android/src/main/java/com/instabug/reactlibrary/utils/EventEmitterModule.java new file mode 100644 index 000000000..d2226cc49 --- /dev/null +++ b/android/src/main/java/com/instabug/reactlibrary/utils/EventEmitterModule.java @@ -0,0 +1,34 @@ +package com.instabug.reactlibrary.utils; + +import androidx.annotation.Nullable; +import androidx.annotation.VisibleForTesting; + +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.WritableMap; +import com.facebook.react.modules.core.DeviceEventManagerModule; + +public abstract class EventEmitterModule extends ReactContextBaseJavaModule { + private int listenerCount = 0; + + public EventEmitterModule(ReactApplicationContext context) { + super(context); + } + + @VisibleForTesting + public void sendEvent(String event, @Nullable WritableMap params) { + if (listenerCount > 0) { + getReactApplicationContext() + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit(event, params); + } + } + + protected void addListener(String ignoredEvent) { + listenerCount++; + } + + protected void removeListeners(Integer count) { + listenerCount -= count; + } +} diff --git a/android/src/main/java/com/instabug/reactlibrary/utils/InstabugUtil.java b/android/src/main/java/com/instabug/reactlibrary/utils/InstabugUtil.java index f2ca2505b..2b64a2538 100644 --- a/android/src/main/java/com/instabug/reactlibrary/utils/InstabugUtil.java +++ b/android/src/main/java/com/instabug/reactlibrary/utils/InstabugUtil.java @@ -41,14 +41,6 @@ public static Method getMethod(Class clazz, String methodName, Class... paramete return null; } - public static void sendEvent(ReactApplicationContext reactContext, - String eventName, - WritableMap params) { - reactContext - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit(eventName, params); - } - /** * Convenience method to convert from a list of Surveys to a JSON array * diff --git a/android/src/test/java/com/instabug/reactlibrary/RNInstabugBugReportingModuleTest.java b/android/src/test/java/com/instabug/reactlibrary/RNInstabugBugReportingModuleTest.java index dab156cbe..ce8ace4f3 100644 --- a/android/src/test/java/com/instabug/reactlibrary/RNInstabugBugReportingModuleTest.java +++ b/android/src/test/java/com/instabug/reactlibrary/RNInstabugBugReportingModuleTest.java @@ -37,12 +37,13 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class RNInstabugBugReportingModuleTest { - private RNInstabugBugReportingModule bugReportingModule = new RNInstabugBugReportingModule(mock(ReactApplicationContext.class)); + private RNInstabugBugReportingModule bugReportingModule = spy(new RNInstabugBugReportingModule(mock(ReactApplicationContext.class))); private final static ScheduledExecutorService mainThread = Executors.newSingleThreadScheduledExecutor(); // Mock Objects @@ -240,8 +241,7 @@ public Object answer(InvocationOnMock invocation) { bugReportingModule.setOnInvokeHandler(null); // then - verify(InstabugUtil.class,VerificationModeFactory.times(1)); - InstabugUtil.sendEvent(any(ReactApplicationContext.class), eq(Constants.IBG_PRE_INVOCATION_HANDLER), Matchers.isNull(WritableMap.class)); + verify(bugReportingModule).sendEvent(Constants.IBG_PRE_INVOCATION_HANDLER, null); } @@ -255,7 +255,6 @@ public Object answer(InvocationOnMock invocation) { when(Arguments.createMap()).thenReturn(new JavaOnlyMap()); mockBugReporting.when(() -> BugReporting.setOnDismissCallback(any(OnSdkDismissCallback.class))).thenAnswer(new Answer() { public Object answer(InvocationOnMock invocation) { - InstabugUtil.sendEvent(any(),any(),any()); ((OnSdkDismissCallback) invocation.getArguments()[0]) .call(OnSdkDismissCallback.DismissType.CANCEL, OnSdkDismissCallback.ReportType.BUG); return null; @@ -266,8 +265,7 @@ public Object answer(InvocationOnMock invocation) { WritableMap params = new JavaOnlyMap(); params.putString("dismissType", OnSdkDismissCallback.DismissType.CANCEL.toString()); params.putString("reportType", OnSdkDismissCallback.ReportType.BUG.toString()); - verify(InstabugUtil.class,VerificationModeFactory.times(1)); - InstabugUtil.sendEvent(any(ReactApplicationContext.class), eq(Constants.IBG_POST_INVOCATION_HANDLER), eq(params)); + verify(bugReportingModule).sendEvent(Constants.IBG_POST_INVOCATION_HANDLER, params); mockArgument.close(); mockReactApplicationContext.close(); } diff --git a/android/src/test/java/com/instabug/reactlibrary/RNInstabugSurveysModuleTest.java b/android/src/test/java/com/instabug/reactlibrary/RNInstabugSurveysModuleTest.java index 38d666a7c..e216ce433 100644 --- a/android/src/test/java/com/instabug/reactlibrary/RNInstabugSurveysModuleTest.java +++ b/android/src/test/java/com/instabug/reactlibrary/RNInstabugSurveysModuleTest.java @@ -7,7 +7,6 @@ import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.JavaOnlyArray; import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.WritableMap; import com.instabug.library.Feature; import com.instabug.reactlibrary.utils.InstabugUtil; import com.instabug.survey.Survey; @@ -23,7 +22,6 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.mockito.Matchers; import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; @@ -34,19 +32,17 @@ import java.util.concurrent.ScheduledExecutorService; import static org.mockito.Matchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; public class RNInstabugSurveysModuleTest { - private RNInstabugSurveysModule surveysModule = new RNInstabugSurveysModule(mock(ReactApplicationContext.class)); - - private final static ScheduledExecutorService mainThread = Executors.newSingleThreadScheduledExecutor(); + private RNInstabugSurveysModule surveysModule = spy(new RNInstabugSurveysModule(mock(ReactApplicationContext.class))); // Mock Objects private MockedStatic mockLooper; @@ -180,8 +176,7 @@ public Object answer(InvocationOnMock invocation) { surveysModule.setOnShowHandler(null); // then - verify(InstabugUtil.class,times(1)); - InstabugUtil.sendEvent(any(ReactApplicationContext.class), eq(Constants.IBG_ON_SHOW_SURVEY_HANDLER), Matchers.isNull(WritableMap.class)); + verify(surveysModule).sendEvent(Constants.IBG_ON_SHOW_SURVEY_HANDLER, null); } @Test @@ -200,8 +195,7 @@ public Object answer(InvocationOnMock invocation) { surveysModule.setOnDismissHandler(null); // then - verify(InstabugUtil.class,times(1)); - InstabugUtil.sendEvent(any(ReactApplicationContext.class), eq(Constants.IBG_ON_DISMISS_SURVEY_HANDLER), Matchers.isNull(WritableMap.class)); + verify(surveysModule).sendEvent(Constants.IBG_ON_DISMISS_SURVEY_HANDLER, null); } @Test diff --git a/ios/RNInstabug/InstabugReactBridge.m b/ios/RNInstabug/InstabugReactBridge.m index 3989f8a58..3f8025db7 100644 --- a/ios/RNInstabug/InstabugReactBridge.m +++ b/ios/RNInstabug/InstabugReactBridge.m @@ -24,10 +24,7 @@ + (void)setWillSendReportHandler_private:(void(^)(IBGReport *report, void(^repor @implementation InstabugReactBridge - (NSArray *)supportedEvents { - return @[ - @"IBGpreSendingHandler", - @"IBGSetNetworkDataObfuscationHandler", - ]; + return @[@"IBGpreSendingHandler"]; } RCT_EXPORT_MODULE(Instabug) diff --git a/src/index.ts b/src/index.ts index dc26cb2dc..75d434101 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ import * as CrashReporting from './modules/CrashReporting'; import * as FeatureRequests from './modules/FeatureRequests'; import * as Instabug from './modules/Instabug'; import * as NetworkLogger from './modules/NetworkLogger'; -import type { NetworkData } from './modules/NetworkLogger'; +import type { NetworkData, NetworkDataObfuscationHandler } from './modules/NetworkLogger'; import * as Replies from './modules/Replies'; import type { Survey } from './modules/Surveys'; import * as Surveys from './modules/Surveys'; @@ -26,8 +26,6 @@ export { Replies, Surveys, }; -export type { InstabugConfig }; -export type { Survey }; -export type { NetworkData }; +export type { InstabugConfig, Survey, NetworkData, NetworkDataObfuscationHandler }; export default Instabug; diff --git a/src/modules/BugReporting.ts b/src/modules/BugReporting.ts index 2708ff622..8f9b0eb64 100644 --- a/src/modules/BugReporting.ts +++ b/src/modules/BugReporting.ts @@ -1,6 +1,6 @@ import { Platform } from 'react-native'; -import { NativeBugReporting } from '../native/NativeBugReporting'; +import { NativeBugReporting, NativeEvents, emitter } from '../native/NativeBugReporting'; import { dismissType, extendedBugReportMode, @@ -19,8 +19,6 @@ import type { RecordingButtonPosition, ReportType, } from '../utils/Enums'; -import IBGEventEmitter from '../utils/IBGEventEmitter'; -import InstabugConstants from '../utils/InstabugConstants'; export { invocationEvent, extendedBugReportMode, reportType, option, position }; @@ -57,7 +55,7 @@ export const setOptions = (options: option[] | InvocationOption[]) => { * @param handler A callback that gets executed before invoking the SDK */ export const onInvokeHandler = (handler: () => void) => { - IBGEventEmitter.addListener(NativeBugReporting, InstabugConstants.ON_INVOKE_HANDLER, handler); + emitter.addListener(NativeEvents.ON_INVOKE_HANDLER, handler); NativeBugReporting.setOnInvokeHandler(handler); }; @@ -70,13 +68,9 @@ export const onInvokeHandler = (handler: () => void) => { export const onSDKDismissedHandler = ( handler: (dismissType: dismissType | DismissType, reportType: reportType | ReportType) => void, ) => { - IBGEventEmitter.addListener( - NativeBugReporting, - InstabugConstants.ON_SDK_DISMISSED_HANDLER, - (payload) => { - handler(payload.dismissType, payload.reportType); - }, - ); + emitter.addListener(NativeEvents.ON_DISMISS_HANDLER, (payload) => { + handler(payload.dismissType, payload.reportType); + }); NativeBugReporting.setOnSDKDismissedHandler(handler); }; @@ -193,13 +187,9 @@ export const setDidSelectPromptOptionHandler = (handler: (promptOption: string) if (Platform.OS === 'android') { return; } - IBGEventEmitter.addListener( - NativeBugReporting, - InstabugConstants.DID_SELECT_PROMPT_OPTION_HANDLER, - (payload) => { - handler(payload.promptOption); - }, - ); + emitter.addListener(NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, (payload) => { + handler(payload.promptOption); + }); NativeBugReporting.setDidSelectPromptOptionHandler(handler); }; diff --git a/src/modules/Instabug.ts b/src/modules/Instabug.ts index 77c082171..fa9ef7acb 100644 --- a/src/modules/Instabug.ts +++ b/src/modules/Instabug.ts @@ -7,7 +7,7 @@ import type { NavigationAction, NavigationState as NavigationStateV4 } from 'rea import type { InstabugConfig } from '../models/InstabugConfig'; import Report from '../models/Report'; -import { NativeInstabug } from '../native/NativeInstabug'; +import { NativeEvents, NativeInstabug, emitter } from '../native/NativeInstabug'; import { IBGPosition, actionTypes, @@ -31,8 +31,6 @@ import { StringKey, WelcomeMessageMode, } from '../utils/Enums'; -import IBGEventEmitter from '../utils/IBGEventEmitter'; -import InstabugConstants from '../utils/InstabugConstants'; import InstabugUtils, { stringifyIfNotString } from '../utils/InstabugUtils'; import * as NetworkLogger from './NetworkLogger'; @@ -533,7 +531,7 @@ export const show = () => { }; export const onReportSubmitHandler = (handler?: (report: Report) => void) => { - IBGEventEmitter.addListener(NativeInstabug, InstabugConstants.PRESENDING_HANDLER, (report) => { + emitter.addListener(NativeEvents.PRESENDING_HANDLER, (report) => { const { tags, consoleLogs, instabugLogs, userAttributes, fileAttachments } = report; const reportObj = new Report(tags, consoleLogs, instabugLogs, userAttributes, fileAttachments); handler && handler(reportObj); diff --git a/src/modules/NetworkLogger.ts b/src/modules/NetworkLogger.ts index 7d9d162df..8e102e3ab 100644 --- a/src/modules/NetworkLogger.ts +++ b/src/modules/NetworkLogger.ts @@ -4,13 +4,13 @@ import type { RequestHandler } from '@apollo/client'; import { NativeAPM } from '../native/NativeAPM'; import { NativeInstabug } from '../native/NativeInstabug'; -import IBGEventEmitter from '../utils/IBGEventEmitter'; import InstabugConstants from '../utils/InstabugConstants'; import xhr, { NetworkData, ProgressCallback } from '../utils/XhrNetworkInterceptor'; export type { NetworkData }; -let _networkDataObfuscationHandlerSet = false; +export type NetworkDataObfuscationHandler = (data: NetworkData) => Promise; +let _networkDataObfuscationHandler: NetworkDataObfuscationHandler | null | undefined; let _requestFilterExpression = 'false'; /** @@ -21,23 +21,23 @@ let _requestFilterExpression = 'false'; export const setEnabled = (isEnabled: boolean) => { if (isEnabled) { xhr.enableInterception(); - xhr.setOnDoneCallback((network) => { + xhr.setOnDoneCallback(async (network) => { // eslint-disable-next-line no-new-func const predicate = Function('network', 'return ' + _requestFilterExpression); if (!predicate(network)) { - if (_networkDataObfuscationHandlerSet) { - IBGEventEmitter.emit(InstabugConstants.NETWORK_DATA_OBFUSCATION_HANDLER_EVENT, network); - } else { - try { - if (Platform.OS === 'android') { - NativeInstabug.networkLog(JSON.stringify(network)); - NativeAPM.networkLog(JSON.stringify(network)); - } else { - NativeInstabug.networkLog(network); - } - } catch (e) { - console.error(e); + try { + if (_networkDataObfuscationHandler) { + network = await _networkDataObfuscationHandler(network); } + + if (Platform.OS === 'android') { + NativeInstabug.networkLog(JSON.stringify(network)); + NativeAPM.networkLog(JSON.stringify(network)); + } else { + NativeInstabug.networkLog(network); + } + } catch (e) { + console.error(e); } } }); @@ -51,32 +51,9 @@ export const setEnabled = (isEnabled: boolean) => { * @param handler */ export const setNetworkDataObfuscationHandler = ( - handler: (data: NetworkData) => Promise, + handler?: NetworkDataObfuscationHandler | null | undefined, ) => { - if (handler === null) { - _networkDataObfuscationHandlerSet = false; - return; - } - _networkDataObfuscationHandlerSet = true; - - IBGEventEmitter.addListener( - NativeInstabug, - InstabugConstants.NETWORK_DATA_OBFUSCATION_HANDLER_EVENT, - async (data: NetworkData) => { - try { - const newData = await handler(data); - - if (Platform.OS === 'android') { - NativeInstabug.networkLog(JSON.stringify(newData)); - NativeAPM.networkLog(JSON.stringify(newData)); - } else { - NativeInstabug.networkLog(newData); - } - } catch (e) { - console.error(e); - } - }, - ); + _networkDataObfuscationHandler = handler; }; /** diff --git a/src/modules/Replies.ts b/src/modules/Replies.ts index dfd21fdda..26d253d50 100644 --- a/src/modules/Replies.ts +++ b/src/modules/Replies.ts @@ -1,8 +1,6 @@ import { Platform } from 'react-native'; -import { NativeReplies } from '../native/NativeReplies'; -import IBGEventEmitter from '../utils/IBGEventEmitter'; -import InstabugConstants from '../utils/InstabugConstants'; +import { NativeEvents, NativeReplies, emitter } from '../native/NativeReplies'; /** * Enables and disables everything related to receiving replies. @@ -32,7 +30,7 @@ export const show = () => { * @param handler A callback that gets executed when a new message is received. */ export const setOnNewReplyReceivedHandler = (handler: () => void) => { - IBGEventEmitter.addListener(NativeReplies, InstabugConstants.ON_REPLY_RECEIVED_HANDLER, handler); + emitter.addListener(NativeEvents.ON_REPLY_RECEIVED_HANDLER, handler); NativeReplies.setOnNewReplyReceivedHandler(handler); }; diff --git a/src/modules/Surveys.ts b/src/modules/Surveys.ts index 41386a38e..790cf4fb0 100644 --- a/src/modules/Surveys.ts +++ b/src/modules/Surveys.ts @@ -1,9 +1,7 @@ import { Platform } from 'react-native'; -import { NativeSurveys } from '../native/NativeSurveys'; +import { NativeEvents, NativeSurveys, emitter } from '../native/NativeSurveys'; import type { Survey } from '../native/NativeSurveys'; -import IBGEventEmitter from '../utils/IBGEventEmitter'; -import InstabugConstants from '../utils/InstabugConstants'; export type { Survey }; @@ -55,11 +53,7 @@ export const setAutoShowingEnabled = (autoShowingSurveysEnabled: boolean) => { * presenting the survey's UI. */ export const setOnShowHandler = (onShowHandler: () => void) => { - IBGEventEmitter.addListener( - NativeSurveys, - InstabugConstants.WILL_SHOW_SURVEY_HANDLER, - onShowHandler, - ); + emitter.addListener(NativeEvents.WILL_SHOW_SURVEY_HANDLER, onShowHandler); NativeSurveys.setOnShowHandler(onShowHandler); }; @@ -71,11 +65,7 @@ export const setOnShowHandler = (onShowHandler: () => void) => { * the survey's UI is dismissed. */ export const setOnDismissHandler = (onDismissHandler: () => void) => { - IBGEventEmitter.addListener( - NativeSurveys, - InstabugConstants.DID_DISMISS_SURVEY_HANDLER, - onDismissHandler, - ); + emitter.addListener(NativeEvents.DID_DISMISS_SURVEY_HANDLER, onDismissHandler); NativeSurveys.setOnDismissHandler(onDismissHandler); }; diff --git a/src/native/NativeBugReporting.ts b/src/native/NativeBugReporting.ts index 70eebfacd..827fa6836 100644 --- a/src/native/NativeBugReporting.ts +++ b/src/native/NativeBugReporting.ts @@ -1,4 +1,4 @@ -import type { NativeModule } from 'react-native'; +import { NativeEventEmitter, NativeModule } from 'react-native'; import type { dismissType, @@ -60,3 +60,11 @@ export interface BugReportingNativeModule extends NativeModule { } export const NativeBugReporting = NativeModules.IBGBugReporting; + +export enum NativeEvents { + ON_INVOKE_HANDLER = 'IBGpreInvocationHandler', + ON_DISMISS_HANDLER = 'IBGpostInvocationHandler', + DID_SELECT_PROMPT_OPTION_HANDLER = 'IBGDidSelectPromptOptionHandler', +} + +export const emitter = new NativeEventEmitter(NativeBugReporting); diff --git a/src/native/NativeInstabug.ts b/src/native/NativeInstabug.ts index dc241fc04..dd5fa1229 100644 --- a/src/native/NativeInstabug.ts +++ b/src/native/NativeInstabug.ts @@ -1,4 +1,4 @@ -import type { NativeModule, ProcessedColorValue } from 'react-native'; +import { NativeEventEmitter, NativeModule, ProcessedColorValue } from 'react-native'; import type Report from '../models/Report'; import type { @@ -123,3 +123,9 @@ export interface InstabugNativeModule extends NativeModule { } export const NativeInstabug = NativeModules.Instabug; + +export enum NativeEvents { + PRESENDING_HANDLER = 'IBGpreSendingHandler', +} + +export const emitter = new NativeEventEmitter(NativeInstabug); diff --git a/src/native/NativeReplies.ts b/src/native/NativeReplies.ts index 74d4571bc..e7624d5dc 100644 --- a/src/native/NativeReplies.ts +++ b/src/native/NativeReplies.ts @@ -1,4 +1,4 @@ -import type { NativeModule } from 'react-native'; +import { NativeEventEmitter, NativeModule } from 'react-native'; import { NativeModules } from './NativePackage'; @@ -26,3 +26,9 @@ export interface RepliesNativeModule extends NativeModule { } export const NativeReplies = NativeModules.IBGReplies; + +export enum NativeEvents { + ON_REPLY_RECEIVED_HANDLER = 'IBGOnNewReplyReceivedCallback', +} + +export const emitter = new NativeEventEmitter(NativeReplies); diff --git a/src/native/NativeSurveys.ts b/src/native/NativeSurveys.ts index 03281c8ff..9c1233c27 100644 --- a/src/native/NativeSurveys.ts +++ b/src/native/NativeSurveys.ts @@ -1,4 +1,4 @@ -import type { NativeModule } from 'react-native'; +import { NativeEventEmitter, NativeModule } from 'react-native'; import { NativeModules } from './NativePackage'; @@ -28,3 +28,10 @@ export interface SurveysNativeModule extends NativeModule { } export const NativeSurveys = NativeModules.IBGSurveys; + +export enum NativeEvents { + WILL_SHOW_SURVEY_HANDLER = 'IBGWillShowSurvey', + DID_DISMISS_SURVEY_HANDLER = 'IBGDidDismissSurvey', +} + +export const emitter = new NativeEventEmitter(NativeSurveys); diff --git a/src/utils/IBGEventEmitter.ts b/src/utils/IBGEventEmitter.ts deleted file mode 100644 index 556f9ccaf..000000000 --- a/src/utils/IBGEventEmitter.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { DeviceEventEmitter, NativeAppEventEmitter, NativeModule, Platform } from 'react-native'; - -export default { - addListener: (nativeModule: NativeModule, eventName: string, callback: (data: any) => void) => { - if (Platform.OS === 'ios') { - nativeModule.addListener(eventName); - NativeAppEventEmitter.addListener(eventName, callback); - } else { - DeviceEventEmitter.addListener(eventName, callback); - } - }, - emit: (eventName: string, ...eventParams: any[]) => { - if (Platform.OS === 'ios') { - NativeAppEventEmitter.emit(eventName, ...eventParams); - } else { - DeviceEventEmitter.emit(eventName, ...eventParams); - } - }, - removeAllListeners: () => { - if (Platform.OS === 'ios') { - NativeAppEventEmitter.removeAllListeners(); - } else { - DeviceEventEmitter.removeAllListeners(); - } - }, - getListeners: (eventName: string) => { - // Dirty trick to get tests passing before the events migration - if (Platform.OS === 'ios') { - return { length: NativeAppEventEmitter.listenerCount(eventName) }; - } else { - return { length: DeviceEventEmitter.listenerCount(eventName) }; - } - }, -}; diff --git a/src/utils/InstabugConstants.ts b/src/utils/InstabugConstants.ts index cd42e77c5..c7b498877 100644 --- a/src/utils/InstabugConstants.ts +++ b/src/utils/InstabugConstants.ts @@ -1,12 +1,4 @@ enum InstabugConstants { - NETWORK_DATA_OBFUSCATION_HANDLER_EVENT = 'IBGSetNetworkDataObfuscationHandler', - PRESENDING_HANDLER = 'IBGpreSendingHandler', - ON_INVOKE_HANDLER = 'IBGpreInvocationHandler', - ON_SDK_DISMISSED_HANDLER = 'IBGpostInvocationHandler', - ON_REPLY_RECEIVED_HANDLER = 'IBGOnNewReplyReceivedCallback', - WILL_SHOW_SURVEY_HANDLER = 'IBGWillShowSurvey', - DID_DISMISS_SURVEY_HANDLER = 'IBGDidDismissSurvey', - DID_SELECT_PROMPT_OPTION_HANDLER = 'IBGDidSelectPromptOptionHandler', GRAPHQL_HEADER = 'ibg-graphql-header', } diff --git a/test/modules/BugReporting.spec.ts b/test/modules/BugReporting.spec.ts index f07b5c151..c825b5b96 100644 --- a/test/modules/BugReporting.spec.ts +++ b/test/modules/BugReporting.spec.ts @@ -1,7 +1,7 @@ import { Platform } from 'react-native'; import * as BugReporting from '../../src/modules/BugReporting'; -import { NativeBugReporting } from '../../src/native/NativeBugReporting'; +import { NativeBugReporting, NativeEvents, emitter } from '../../src/native/NativeBugReporting'; import { ExtendedBugReportMode, FloatingButtonPosition, @@ -10,10 +10,15 @@ import { RecordingButtonPosition, ReportType, } from '../../src/utils/Enums'; -import IBGEventEmitter from '../../src/utils/IBGEventEmitter'; -import IBGConstants from '../../src/utils/InstabugConstants'; describe('Testing BugReporting Module', () => { + beforeEach(() => { + const events = Object.values(NativeEvents); + events.forEach((event) => { + emitter.removeAllListeners(event); + }); + }); + it('should call the native method setBugReportingEnabled', () => { BugReporting.setEnabled(true); @@ -131,9 +136,9 @@ describe('Testing BugReporting Module', () => { it('should invoke callback on emitting the event IBGpreInvocationHandler', () => { const callback = jest.fn(); BugReporting.onInvokeHandler(callback); - IBGEventEmitter.emit(IBGConstants.ON_INVOKE_HANDLER); + emitter.emit(NativeEvents.ON_INVOKE_HANDLER); - expect(IBGEventEmitter.getListeners(IBGConstants.ON_INVOKE_HANDLER).length).toEqual(1); + expect(emitter.listenerCount(NativeEvents.ON_INVOKE_HANDLER)).toBe(1); expect(callback).toHaveBeenCalled(); }); @@ -151,12 +156,12 @@ describe('Testing BugReporting Module', () => { const callback = jest.fn(); BugReporting.onSDKDismissedHandler(callback); - IBGEventEmitter.emit(IBGConstants.ON_SDK_DISMISSED_HANDLER, { + emitter.emit(NativeEvents.ON_DISMISS_HANDLER, { dismissType: dismissType, reportType: reportType, }); - expect(IBGEventEmitter.getListeners(IBGConstants.ON_SDK_DISMISSED_HANDLER).length).toEqual(1); + expect(emitter.listenerCount(NativeEvents.ON_DISMISS_HANDLER)).toBe(1); expect(callback).toBeCalledTimes(1); expect(callback).toBeCalledWith(dismissType, reportType); }); @@ -192,12 +197,10 @@ describe('Testing BugReporting Module', () => { Platform.OS = 'android'; BugReporting.setDidSelectPromptOptionHandler(jest.fn()); - IBGEventEmitter.emit(IBGConstants.DID_SELECT_PROMPT_OPTION_HANDLER, {}); + emitter.emit(NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, {}); expect(NativeBugReporting.setDidSelectPromptOptionHandler).not.toBeCalled(); - expect( - IBGEventEmitter.getListeners(IBGConstants.DID_SELECT_PROMPT_OPTION_HANDLER).length, - ).toEqual(0); + expect(emitter.listenerCount(NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER)).toBe(0); }); it('should call setDidSelectPromptOptionHandler event listener when platform is iOS', () => { @@ -206,10 +209,9 @@ describe('Testing BugReporting Module', () => { const payload = { promptOption: 'bug' }; BugReporting.setDidSelectPromptOptionHandler(callback); - IBGEventEmitter.emit(IBGConstants.DID_SELECT_PROMPT_OPTION_HANDLER, payload); + emitter.emit(NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, payload); - const listeners = IBGEventEmitter.getListeners(IBGConstants.DID_SELECT_PROMPT_OPTION_HANDLER); - expect(listeners.length).toBe(1); + expect(emitter.listenerCount(NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER)).toBe(1); expect(callback).toBeCalledTimes(1); expect(callback).toBeCalledWith(payload.promptOption); }); diff --git a/test/modules/Instabug.spec.ts b/test/modules/Instabug.spec.ts index ef7c525c7..7f2dd3767 100644 --- a/test/modules/Instabug.spec.ts +++ b/test/modules/Instabug.spec.ts @@ -6,7 +6,7 @@ import waitForExpect from 'wait-for-expect'; import Report from '../../src/models/Report'; import * as Instabug from '../../src/modules/Instabug'; -import { NativeInstabug } from '../../src/native/NativeInstabug'; +import { NativeEvents, NativeInstabug, emitter } from '../../src/native/NativeInstabug'; import { ColorTheme, InvocationEvent, @@ -16,11 +16,16 @@ import { StringKey, WelcomeMessageMode, } from '../../src/utils/Enums'; -import IBGEventEmitter from '../../src/utils/IBGEventEmitter'; -import IBGConstants from '../../src/utils/InstabugConstants'; import InstabugUtils from '../../src/utils/InstabugUtils'; describe('Instabug Module', () => { + beforeEach(() => { + const events = Object.values(NativeEvents); + events.forEach((event) => { + emitter.removeAllListeners(event); + }); + }); + it('should call the native method setEnabled', () => { Instabug.setEnabled(true); @@ -636,9 +641,9 @@ describe('Instabug Module', () => { done(); }; Instabug.onReportSubmitHandler(callback); - IBGEventEmitter.emit(IBGConstants.PRESENDING_HANDLER, report); + emitter.emit(NativeEvents.PRESENDING_HANDLER, report); - expect(IBGEventEmitter.getListeners(IBGConstants.PRESENDING_HANDLER).length).toEqual(1); + expect(emitter.listenerCount(NativeEvents.PRESENDING_HANDLER)).toBe(1); }); it('should invoke the native method callPrivateApi', () => { diff --git a/test/modules/NetworkLogger.spec.ts b/test/modules/NetworkLogger.spec.ts index b536e50ce..de121c636 100644 --- a/test/modules/NetworkLogger.spec.ts +++ b/test/modules/NetworkLogger.spec.ts @@ -7,8 +7,6 @@ import waitForExpect from 'wait-for-expect'; import * as NetworkLogger from '../../src/modules/NetworkLogger'; import { NativeAPM } from '../../src/native/NativeAPM'; import { NativeInstabug } from '../../src/native/NativeInstabug'; -import IBGEventEmitter from '../../src/utils/IBGEventEmitter'; -import IBGConstants from '../../src/utils/InstabugConstants'; import Interceptor from '../../src/utils/XhrNetworkInterceptor'; const clone = (obj: any) => { @@ -110,9 +108,6 @@ describe('NetworkLogger Module', () => { }); NetworkLogger.setEnabled(true); - expect( - IBGEventEmitter.getListeners(IBGConstants.NETWORK_DATA_OBFUSCATION_HANDLER_EVENT).length, - ).toEqual(1); await waitForExpect(() => { const newData = clone(network); newData.requestHeaders.token = randomString; @@ -132,9 +127,6 @@ describe('NetworkLogger Module', () => { }); NetworkLogger.setEnabled(true); - expect( - IBGEventEmitter.getListeners(IBGConstants.NETWORK_DATA_OBFUSCATION_HANDLER_EVENT).length, - ).toEqual(1); await waitForExpect(() => { const newData = clone(network); newData.requestHeaders.token = randomString; diff --git a/test/modules/Replies.spec.ts b/test/modules/Replies.spec.ts index 8db4a578f..26fc49b1d 100644 --- a/test/modules/Replies.spec.ts +++ b/test/modules/Replies.spec.ts @@ -1,11 +1,16 @@ import { Platform } from 'react-native'; import * as Replies from '../../src/modules/Replies'; -import { NativeReplies } from '../../src/native/NativeReplies'; -import IBGEventEmitter from '../../src/utils/IBGEventEmitter'; -import IBGConstants from '../../src/utils/InstabugConstants'; +import { NativeEvents, NativeReplies, emitter } from '../../src/native/NativeReplies'; describe('Replies Module', () => { + beforeEach(() => { + const events = Object.values(NativeEvents); + events.forEach((event) => { + emitter.removeAllListeners(event); + }); + }); + it('should call the native method setRepliesEnabled', () => { Replies.setEnabled(true); @@ -39,9 +44,9 @@ describe('Replies Module', () => { it('should invoke callback on emitting the event IBGOnNewReplyReceivedCallback', () => { const callback = jest.fn(); Replies.setOnNewReplyReceivedHandler(callback); - IBGEventEmitter.emit(IBGConstants.ON_REPLY_RECEIVED_HANDLER); + emitter.emit(NativeEvents.ON_REPLY_RECEIVED_HANDLER); - expect(IBGEventEmitter.getListeners(IBGConstants.ON_REPLY_RECEIVED_HANDLER).length).toEqual(1); + expect(emitter.listenerCount(NativeEvents.ON_REPLY_RECEIVED_HANDLER)).toBe(1); expect(callback).toHaveBeenCalled(); }); diff --git a/test/modules/Surveys.spec.ts b/test/modules/Surveys.spec.ts index 71c1bda49..d55760752 100644 --- a/test/modules/Surveys.spec.ts +++ b/test/modules/Surveys.spec.ts @@ -1,11 +1,16 @@ import { Platform } from 'react-native'; import * as Surveys from '../../src/modules/Surveys'; -import { NativeSurveys } from '../../src/native/NativeSurveys'; -import IBGEventEmitter from '../../src/utils/IBGEventEmitter'; -import IBGConstants from '../../src/utils/InstabugConstants'; +import { NativeEvents, NativeSurveys, emitter } from '../../src/native/NativeSurveys'; describe('Surveys Module', () => { + beforeEach(() => { + const events = Object.values(NativeEvents); + events.forEach((event) => { + emitter.removeAllListeners(event); + }); + }); + it('should call the native method setSurveysEnabled', () => { Surveys.setEnabled(true); @@ -60,9 +65,9 @@ describe('Surveys Module', () => { it('should invoke callback on emitting the event IBGWillShowSurvey', () => { const callback = jest.fn(); Surveys.setOnShowHandler(callback); - IBGEventEmitter.emit(IBGConstants.WILL_SHOW_SURVEY_HANDLER); + emitter.emit(NativeEvents.WILL_SHOW_SURVEY_HANDLER); - expect(IBGEventEmitter.getListeners(IBGConstants.WILL_SHOW_SURVEY_HANDLER).length).toEqual(1); + expect(emitter.listenerCount(NativeEvents.WILL_SHOW_SURVEY_HANDLER)).toBe(1); expect(callback).toHaveBeenCalled(); }); @@ -77,9 +82,9 @@ describe('Surveys Module', () => { it('should invoke callback on emitting the event IBGDidDismissSurvey', () => { const callback = jest.fn(); Surveys.setOnDismissHandler(callback); - IBGEventEmitter.emit(IBGConstants.DID_DISMISS_SURVEY_HANDLER); + emitter.emit(NativeEvents.DID_DISMISS_SURVEY_HANDLER); - expect(IBGEventEmitter.getListeners(IBGConstants.DID_DISMISS_SURVEY_HANDLER).length).toEqual(1); + expect(emitter.listenerCount(NativeEvents.DID_DISMISS_SURVEY_HANDLER)).toBe(1); expect(callback).toHaveBeenCalled(); }); diff --git a/test/setup.ts b/test/setup.ts index 62dcf6a7c..f18030d28 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -7,15 +7,11 @@ import 'react-native/Libraries/Network/fetch'; import nock from 'nock'; import XHR from 'xhr2'; -import IBGEventEmitter from '../src/utils/IBGEventEmitter'; - global.XMLHttpRequest = XHR; nock.disableNetConnect(); beforeEach(() => { - IBGEventEmitter.removeAllListeners(); - jest.spyOn(Platform, 'constants', 'get').mockReturnValue({ isTesting: true, reactNativeVersion: {