From a443362b1c58cc07912ed1ca9f0f7e43b7c096b7 Mon Sep 17 00:00:00 2001 From: Ahmed Mahmoud Date: Sun, 19 Feb 2023 12:39:58 +0200 Subject: [PATCH 1/9] Remove event emitter from network logger Eliminates the use of event emitters in `NetworkLogger` by adding the callback in a global `_networkDataObfuscationHandler` variable and invoking it if it exists before logging the request --- ios/RNInstabug/InstabugReactBridge.m | 5 +-- src/modules/NetworkLogger.ts | 57 +++++++++------------------- src/utils/InstabugConstants.ts | 1 - test/modules/NetworkLogger.spec.ts | 8 ---- 4 files changed, 18 insertions(+), 53 deletions(-) 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/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/utils/InstabugConstants.ts b/src/utils/InstabugConstants.ts index cd42e77c5..2c56a45aa 100644 --- a/src/utils/InstabugConstants.ts +++ b/src/utils/InstabugConstants.ts @@ -1,5 +1,4 @@ enum InstabugConstants { - NETWORK_DATA_OBFUSCATION_HANDLER_EVENT = 'IBGSetNetworkDataObfuscationHandler', PRESENDING_HANDLER = 'IBGpreSendingHandler', ON_INVOKE_HANDLER = 'IBGpreInvocationHandler', ON_SDK_DISMISSED_HANDLER = 'IBGpostInvocationHandler', 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; From 6172f09af5a42c32981559ca7643aa022bdb9715 Mon Sep 17 00:00:00 2001 From: Ahmed Mahmoud Date: Sun, 19 Feb 2023 14:21:40 +0200 Subject: [PATCH 2/9] Migrate Bug Reporting to `NativeEventEmitter` --- .../RNInstabugBugReportingModule.java | 22 ++++++---- .../utils/EventEmitterModule.java | 32 ++++++++++++++ src/modules/BugReporting.ts | 42 +++++++++++-------- src/utils/InstabugConstants.ts | 3 -- test/modules/BugReporting.spec.ts | 41 ++++++++++++------ 5 files changed, 100 insertions(+), 40 deletions(-) create mode 100644 android/src/main/java/com/instabug/reactlibrary/utils/EventEmitterModule.java 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/utils/EventEmitterModule.java b/android/src/main/java/com/instabug/reactlibrary/utils/EventEmitterModule.java new file mode 100644 index 000000000..eae34e0ce --- /dev/null +++ b/android/src/main/java/com/instabug/reactlibrary/utils/EventEmitterModule.java @@ -0,0 +1,32 @@ +package com.instabug.reactlibrary.utils; + +import androidx.annotation.Nullable; + +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); + } + + protected 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/src/modules/BugReporting.ts b/src/modules/BugReporting.ts index 2708ff622..1acc0acc7 100644 --- a/src/modules/BugReporting.ts +++ b/src/modules/BugReporting.ts @@ -1,4 +1,4 @@ -import { Platform } from 'react-native'; +import { NativeEventEmitter, Platform } from 'react-native'; import { NativeBugReporting } from '../native/NativeBugReporting'; import { @@ -19,11 +19,25 @@ import type { RecordingButtonPosition, ReportType, } from '../utils/Enums'; -import IBGEventEmitter from '../utils/IBGEventEmitter'; -import InstabugConstants from '../utils/InstabugConstants'; export { invocationEvent, extendedBugReportMode, reportType, option, position }; +/** + * @internal You shouldn't use this enum since you never emit or listen + * for native events in your code. + */ +export enum $NativeEvents { + ON_INVOKE_HANDLER = 'IBGpreInvocationHandler', + ON_DISMISS_HANDLER = 'IBGpostInvocationHandler', + DID_SELECT_PROMPT_OPTION_HANDLER = 'IBGDidSelectPromptOptionHandler', +} + +/** + * @internal You shouldn't use this since you never emit or listen for native + * events in your code. + */ +export const $emitter = new NativeEventEmitter(NativeBugReporting); + /** * Enables and disables manual invocation and prompt options for bug and feedback. * @param isEnabled @@ -57,7 +71,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 +84,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 +203,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/utils/InstabugConstants.ts b/src/utils/InstabugConstants.ts index 2c56a45aa..24fd63d28 100644 --- a/src/utils/InstabugConstants.ts +++ b/src/utils/InstabugConstants.ts @@ -1,11 +1,8 @@ enum InstabugConstants { 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..89e2c8eee 100644 --- a/test/modules/BugReporting.spec.ts +++ b/test/modules/BugReporting.spec.ts @@ -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(BugReporting.$NativeEvents); + events.forEach((event) => { + BugReporting.$emitter.removeAllListeners(event); + }); + }); + it('should call the native method setBugReportingEnabled', () => { BugReporting.setEnabled(true); @@ -131,9 +136,11 @@ 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); + BugReporting.$emitter.emit(BugReporting.$NativeEvents.ON_INVOKE_HANDLER); - expect(IBGEventEmitter.getListeners(IBGConstants.ON_INVOKE_HANDLER).length).toEqual(1); + expect(BugReporting.$emitter.listenerCount(BugReporting.$NativeEvents.ON_INVOKE_HANDLER)).toBe( + 1, + ); expect(callback).toHaveBeenCalled(); }); @@ -151,12 +158,14 @@ describe('Testing BugReporting Module', () => { const callback = jest.fn(); BugReporting.onSDKDismissedHandler(callback); - IBGEventEmitter.emit(IBGConstants.ON_SDK_DISMISSED_HANDLER, { + BugReporting.$emitter.emit(BugReporting.$NativeEvents.ON_DISMISS_HANDLER, { dismissType: dismissType, reportType: reportType, }); - expect(IBGEventEmitter.getListeners(IBGConstants.ON_SDK_DISMISSED_HANDLER).length).toEqual(1); + expect(BugReporting.$emitter.listenerCount(BugReporting.$NativeEvents.ON_DISMISS_HANDLER)).toBe( + 1, + ); expect(callback).toBeCalledTimes(1); expect(callback).toBeCalledWith(dismissType, reportType); }); @@ -192,12 +201,14 @@ describe('Testing BugReporting Module', () => { Platform.OS = 'android'; BugReporting.setDidSelectPromptOptionHandler(jest.fn()); - IBGEventEmitter.emit(IBGConstants.DID_SELECT_PROMPT_OPTION_HANDLER, {}); + BugReporting.$emitter.emit(BugReporting.$NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, {}); expect(NativeBugReporting.setDidSelectPromptOptionHandler).not.toBeCalled(); expect( - IBGEventEmitter.getListeners(IBGConstants.DID_SELECT_PROMPT_OPTION_HANDLER).length, - ).toEqual(0); + BugReporting.$emitter.listenerCount( + BugReporting.$NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, + ), + ).toBe(0); }); it('should call setDidSelectPromptOptionHandler event listener when platform is iOS', () => { @@ -206,10 +217,16 @@ describe('Testing BugReporting Module', () => { const payload = { promptOption: 'bug' }; BugReporting.setDidSelectPromptOptionHandler(callback); - IBGEventEmitter.emit(IBGConstants.DID_SELECT_PROMPT_OPTION_HANDLER, payload); + BugReporting.$emitter.emit( + BugReporting.$NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, + payload, + ); - const listeners = IBGEventEmitter.getListeners(IBGConstants.DID_SELECT_PROMPT_OPTION_HANDLER); - expect(listeners.length).toBe(1); + expect( + BugReporting.$emitter.listenerCount( + BugReporting.$NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, + ), + ).toBe(1); expect(callback).toBeCalledTimes(1); expect(callback).toBeCalledWith(payload.promptOption); }); From 7751520cc88c200f113e0cca1ec7edecbdcbe8b7 Mon Sep 17 00:00:00 2001 From: Ahmed Mahmoud Date: Mon, 20 Feb 2023 10:47:07 +0200 Subject: [PATCH 3/9] Migrate Instabug module to `NativeEventEmitter` --- .../RNInstabugReactnativeModule.java | 50 ++++++++----------- src/modules/Instabug.ts | 20 ++++++-- src/utils/InstabugConstants.ts | 1 - test/modules/Instabug.spec.ts | 13 +++-- 4 files changed, 46 insertions(+), 38 deletions(-) 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/src/modules/Instabug.ts b/src/modules/Instabug.ts index 77c082171..ac982d582 100644 --- a/src/modules/Instabug.ts +++ b/src/modules/Instabug.ts @@ -1,5 +1,5 @@ import type React from 'react'; -import { Platform, findNodeHandle, processColor } from 'react-native'; +import { NativeEventEmitter, Platform, findNodeHandle, processColor } from 'react-native'; import type { NavigationState as NavigationStateV5 } from '@react-navigation/native'; import type { ComponentDidAppearEvent } from 'react-native-navigation'; @@ -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'; @@ -56,6 +54,20 @@ export { strings, }; +/** + * @internal You shouldn't use this enum since you never emit or listen + * for native events in your code. + */ +export enum $NativeEvents { + PRESENDING_HANDLER = 'IBGpreSendingHandler', +} + +/** + * @internal You shouldn't use this since you never emit or listen for native + * events in your code. + */ +export const $emitter = new NativeEventEmitter(NativeInstabug); + /** * Enables or disables Instabug functionality. * @param isEnabled A boolean to enable/disable Instabug. @@ -533,7 +545,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/utils/InstabugConstants.ts b/src/utils/InstabugConstants.ts index 24fd63d28..9af2089b7 100644 --- a/src/utils/InstabugConstants.ts +++ b/src/utils/InstabugConstants.ts @@ -1,5 +1,4 @@ enum InstabugConstants { - PRESENDING_HANDLER = 'IBGpreSendingHandler', ON_REPLY_RECEIVED_HANDLER = 'IBGOnNewReplyReceivedCallback', WILL_SHOW_SURVEY_HANDLER = 'IBGWillShowSurvey', DID_DISMISS_SURVEY_HANDLER = 'IBGDidDismissSurvey', diff --git a/test/modules/Instabug.spec.ts b/test/modules/Instabug.spec.ts index ef7c525c7..955c2dd19 100644 --- a/test/modules/Instabug.spec.ts +++ b/test/modules/Instabug.spec.ts @@ -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(Instabug.$NativeEvents); + events.forEach((event) => { + Instabug.$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); + Instabug.$emitter.emit(Instabug.$NativeEvents.PRESENDING_HANDLER, report); - expect(IBGEventEmitter.getListeners(IBGConstants.PRESENDING_HANDLER).length).toEqual(1); + expect(Instabug.$emitter.listenerCount(Instabug.$NativeEvents.PRESENDING_HANDLER)).toBe(1); }); it('should invoke the native method callPrivateApi', () => { From f3d71511c59be95a08ec3666e1b650571e8e31ba Mon Sep 17 00:00:00 2001 From: Ahmed Mahmoud Date: Mon, 20 Feb 2023 11:06:44 +0200 Subject: [PATCH 4/9] Migrate Surveys to `NativeEventEmitter` --- .../reactlibrary/RNInstabugSurveysModule.java | 21 ++++++++----- src/modules/Surveys.ts | 31 +++++++++++-------- src/utils/InstabugConstants.ts | 2 -- test/modules/Surveys.spec.ts | 19 ++++++++---- 4 files changed, 45 insertions(+), 28 deletions(-) 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/src/modules/Surveys.ts b/src/modules/Surveys.ts index 41386a38e..3d3cced90 100644 --- a/src/modules/Surveys.ts +++ b/src/modules/Surveys.ts @@ -1,12 +1,25 @@ -import { Platform } from 'react-native'; +import { NativeEventEmitter, Platform } from 'react-native'; import { NativeSurveys } from '../native/NativeSurveys'; import type { Survey } from '../native/NativeSurveys'; -import IBGEventEmitter from '../utils/IBGEventEmitter'; -import InstabugConstants from '../utils/InstabugConstants'; export type { Survey }; +/** + * @internal You shouldn't use this enum since you never emit or listen + * for native events in your code. + */ +export enum $NativeEvents { + WILL_SHOW_SURVEY_HANDLER = 'IBGWillShowSurvey', + DID_DISMISS_SURVEY_HANDLER = 'IBGDidDismissSurvey', +} + +/** + * @internal You shouldn't use this since you never emit or listen for native + * events in your code. + */ +export const $emitter = new NativeEventEmitter(NativeSurveys); + /** * Sets whether surveys are enabled or not. * If you disable surveys on the SDK but still have active surveys on your Instabug dashboard, @@ -55,11 +68,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 +80,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/utils/InstabugConstants.ts b/src/utils/InstabugConstants.ts index 9af2089b7..bbe81a3bb 100644 --- a/src/utils/InstabugConstants.ts +++ b/src/utils/InstabugConstants.ts @@ -1,7 +1,5 @@ enum InstabugConstants { ON_REPLY_RECEIVED_HANDLER = 'IBGOnNewReplyReceivedCallback', - WILL_SHOW_SURVEY_HANDLER = 'IBGWillShowSurvey', - DID_DISMISS_SURVEY_HANDLER = 'IBGDidDismissSurvey', GRAPHQL_HEADER = 'ibg-graphql-header', } diff --git a/test/modules/Surveys.spec.ts b/test/modules/Surveys.spec.ts index 71c1bda49..862dda577 100644 --- a/test/modules/Surveys.spec.ts +++ b/test/modules/Surveys.spec.ts @@ -2,10 +2,15 @@ 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'; describe('Surveys Module', () => { + beforeEach(() => { + const events = Object.values(Surveys.$NativeEvents); + events.forEach((event) => { + Surveys.$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); + Surveys.$emitter.emit(Surveys.$NativeEvents.WILL_SHOW_SURVEY_HANDLER); - expect(IBGEventEmitter.getListeners(IBGConstants.WILL_SHOW_SURVEY_HANDLER).length).toEqual(1); + expect(Surveys.$emitter.listenerCount(Surveys.$NativeEvents.WILL_SHOW_SURVEY_HANDLER)).toBe(1); expect(callback).toHaveBeenCalled(); }); @@ -77,9 +82,11 @@ 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); + Surveys.$emitter.emit(Surveys.$NativeEvents.DID_DISMISS_SURVEY_HANDLER); - expect(IBGEventEmitter.getListeners(IBGConstants.DID_DISMISS_SURVEY_HANDLER).length).toEqual(1); + expect(Surveys.$emitter.listenerCount(Surveys.$NativeEvents.DID_DISMISS_SURVEY_HANDLER)).toBe( + 1, + ); expect(callback).toHaveBeenCalled(); }); From bbf32bb7e64581ca52329e38a6e0e12254405725 Mon Sep 17 00:00:00 2001 From: Ahmed Mahmoud Date: Mon, 20 Feb 2023 12:48:31 +0200 Subject: [PATCH 5/9] Migrate Replies to `NativeEventEmitter` --- .../reactlibrary/RNInstabugRepliesModule.java | 20 +++++++---- src/modules/Replies.ts | 20 ++++++++--- src/utils/IBGEventEmitter.ts | 34 ------------------- src/utils/InstabugConstants.ts | 1 - test/modules/Replies.spec.ts | 13 ++++--- test/setup.ts | 4 --- 6 files changed, 38 insertions(+), 54 deletions(-) delete mode 100644 src/utils/IBGEventEmitter.ts 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/src/modules/Replies.ts b/src/modules/Replies.ts index dfd21fdda..2c50922bd 100644 --- a/src/modules/Replies.ts +++ b/src/modules/Replies.ts @@ -1,8 +1,20 @@ -import { Platform } from 'react-native'; +import { NativeEventEmitter, Platform } from 'react-native'; import { NativeReplies } from '../native/NativeReplies'; -import IBGEventEmitter from '../utils/IBGEventEmitter'; -import InstabugConstants from '../utils/InstabugConstants'; + +/** + * @internal You shouldn't use this enum since you never emit or listen + * for native events in your code. + */ +export enum $NativeEvents { + ON_REPLY_RECEIVED_HANDLER = 'IBGOnNewReplyReceivedCallback', +} + +/** + * @internal You shouldn't use this since you never emit or listen for native + * events in your code. + */ +export const $emitter = new NativeEventEmitter(NativeReplies); /** * Enables and disables everything related to receiving replies. @@ -32,7 +44,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/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 bbe81a3bb..c7b498877 100644 --- a/src/utils/InstabugConstants.ts +++ b/src/utils/InstabugConstants.ts @@ -1,5 +1,4 @@ enum InstabugConstants { - ON_REPLY_RECEIVED_HANDLER = 'IBGOnNewReplyReceivedCallback', GRAPHQL_HEADER = 'ibg-graphql-header', } diff --git a/test/modules/Replies.spec.ts b/test/modules/Replies.spec.ts index 8db4a578f..861c7c646 100644 --- a/test/modules/Replies.spec.ts +++ b/test/modules/Replies.spec.ts @@ -2,10 +2,15 @@ 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'; describe('Replies Module', () => { + beforeEach(() => { + const events = Object.values(Replies.$NativeEvents); + events.forEach((event) => { + Replies.$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); + Replies.$emitter.emit(Replies.$NativeEvents.ON_REPLY_RECEIVED_HANDLER); - expect(IBGEventEmitter.getListeners(IBGConstants.ON_REPLY_RECEIVED_HANDLER).length).toEqual(1); + expect(Replies.$emitter.listenerCount(Replies.$NativeEvents.ON_REPLY_RECEIVED_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: { From c262ae4235e443cfc0b5d471560cb279af55148f Mon Sep 17 00:00:00 2001 From: Ahmed Mahmoud Date: Mon, 20 Feb 2023 14:01:21 +0200 Subject: [PATCH 6/9] Fix Android tests --- .../reactlibrary/utils/EventEmitterModule.java | 4 +++- .../RNInstabugBugReportingModuleTest.java | 10 ++++------ .../reactlibrary/RNInstabugSurveysModuleTest.java | 14 ++++---------- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/android/src/main/java/com/instabug/reactlibrary/utils/EventEmitterModule.java b/android/src/main/java/com/instabug/reactlibrary/utils/EventEmitterModule.java index eae34e0ce..d2226cc49 100644 --- a/android/src/main/java/com/instabug/reactlibrary/utils/EventEmitterModule.java +++ b/android/src/main/java/com/instabug/reactlibrary/utils/EventEmitterModule.java @@ -1,6 +1,7 @@ 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; @@ -14,7 +15,8 @@ public EventEmitterModule(ReactApplicationContext context) { super(context); } - protected void sendEvent(String event, @Nullable WritableMap params) { + @VisibleForTesting + public void sendEvent(String event, @Nullable WritableMap params) { if (listenerCount > 0) { getReactApplicationContext() .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) 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 From e31122cc6358416ad254686b6ac798443f5bf461 Mon Sep 17 00:00:00 2001 From: Ahmed Mahmoud Date: Thu, 23 Feb 2023 12:56:29 +0200 Subject: [PATCH 7/9] Remove `InstabugUtil.sendEvent` --- .../com/instabug/reactlibrary/utils/InstabugUtil.java | 8 -------- 1 file changed, 8 deletions(-) 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 * From df0dedf9e26cc93500ec75c0c24486d2be8d6a58 Mon Sep 17 00:00:00 2001 From: Ahmed Mahmoud Date: Wed, 1 Mar 2023 16:38:48 +0200 Subject: [PATCH 8/9] Move emitters to native directory --- src/modules/BugReporting.ts | 26 +++++----------------- src/modules/Instabug.ts | 20 +++-------------- src/modules/Replies.ts | 20 +++-------------- src/modules/Surveys.ts | 23 ++++--------------- src/native/NativeBugReporting.ts | 10 ++++++++- src/native/NativeInstabug.ts | 8 ++++++- src/native/NativeReplies.ts | 8 ++++++- src/native/NativeSurveys.ts | 9 +++++++- test/modules/BugReporting.spec.ts | 37 +++++++++---------------------- test/modules/Instabug.spec.ts | 10 ++++----- test/modules/Replies.spec.ts | 10 ++++----- test/modules/Surveys.spec.ts | 16 ++++++------- 12 files changed, 74 insertions(+), 123 deletions(-) diff --git a/src/modules/BugReporting.ts b/src/modules/BugReporting.ts index 1acc0acc7..8f9b0eb64 100644 --- a/src/modules/BugReporting.ts +++ b/src/modules/BugReporting.ts @@ -1,6 +1,6 @@ -import { NativeEventEmitter, Platform } from 'react-native'; +import { Platform } from 'react-native'; -import { NativeBugReporting } from '../native/NativeBugReporting'; +import { NativeBugReporting, NativeEvents, emitter } from '../native/NativeBugReporting'; import { dismissType, extendedBugReportMode, @@ -22,22 +22,6 @@ import type { export { invocationEvent, extendedBugReportMode, reportType, option, position }; -/** - * @internal You shouldn't use this enum since you never emit or listen - * for native events in your code. - */ -export enum $NativeEvents { - ON_INVOKE_HANDLER = 'IBGpreInvocationHandler', - ON_DISMISS_HANDLER = 'IBGpostInvocationHandler', - DID_SELECT_PROMPT_OPTION_HANDLER = 'IBGDidSelectPromptOptionHandler', -} - -/** - * @internal You shouldn't use this since you never emit or listen for native - * events in your code. - */ -export const $emitter = new NativeEventEmitter(NativeBugReporting); - /** * Enables and disables manual invocation and prompt options for bug and feedback. * @param isEnabled @@ -71,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) => { - $emitter.addListener($NativeEvents.ON_INVOKE_HANDLER, handler); + emitter.addListener(NativeEvents.ON_INVOKE_HANDLER, handler); NativeBugReporting.setOnInvokeHandler(handler); }; @@ -84,7 +68,7 @@ export const onInvokeHandler = (handler: () => void) => { export const onSDKDismissedHandler = ( handler: (dismissType: dismissType | DismissType, reportType: reportType | ReportType) => void, ) => { - $emitter.addListener($NativeEvents.ON_DISMISS_HANDLER, (payload) => { + emitter.addListener(NativeEvents.ON_DISMISS_HANDLER, (payload) => { handler(payload.dismissType, payload.reportType); }); NativeBugReporting.setOnSDKDismissedHandler(handler); @@ -203,7 +187,7 @@ export const setDidSelectPromptOptionHandler = (handler: (promptOption: string) if (Platform.OS === 'android') { return; } - $emitter.addListener($NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, (payload) => { + 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 ac982d582..fa9ef7acb 100644 --- a/src/modules/Instabug.ts +++ b/src/modules/Instabug.ts @@ -1,5 +1,5 @@ import type React from 'react'; -import { NativeEventEmitter, Platform, findNodeHandle, processColor } from 'react-native'; +import { Platform, findNodeHandle, processColor } from 'react-native'; import type { NavigationState as NavigationStateV5 } from '@react-navigation/native'; import type { ComponentDidAppearEvent } from 'react-native-navigation'; @@ -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, @@ -54,20 +54,6 @@ export { strings, }; -/** - * @internal You shouldn't use this enum since you never emit or listen - * for native events in your code. - */ -export enum $NativeEvents { - PRESENDING_HANDLER = 'IBGpreSendingHandler', -} - -/** - * @internal You shouldn't use this since you never emit or listen for native - * events in your code. - */ -export const $emitter = new NativeEventEmitter(NativeInstabug); - /** * Enables or disables Instabug functionality. * @param isEnabled A boolean to enable/disable Instabug. @@ -545,7 +531,7 @@ export const show = () => { }; export const onReportSubmitHandler = (handler?: (report: Report) => void) => { - $emitter.addListener($NativeEvents.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/Replies.ts b/src/modules/Replies.ts index 2c50922bd..26d253d50 100644 --- a/src/modules/Replies.ts +++ b/src/modules/Replies.ts @@ -1,20 +1,6 @@ -import { NativeEventEmitter, Platform } from 'react-native'; +import { Platform } from 'react-native'; -import { NativeReplies } from '../native/NativeReplies'; - -/** - * @internal You shouldn't use this enum since you never emit or listen - * for native events in your code. - */ -export enum $NativeEvents { - ON_REPLY_RECEIVED_HANDLER = 'IBGOnNewReplyReceivedCallback', -} - -/** - * @internal You shouldn't use this since you never emit or listen for native - * events in your code. - */ -export const $emitter = new NativeEventEmitter(NativeReplies); +import { NativeEvents, NativeReplies, emitter } from '../native/NativeReplies'; /** * Enables and disables everything related to receiving replies. @@ -44,7 +30,7 @@ export const show = () => { * @param handler A callback that gets executed when a new message is received. */ export const setOnNewReplyReceivedHandler = (handler: () => void) => { - $emitter.addListener($NativeEvents.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 3d3cced90..790cf4fb0 100644 --- a/src/modules/Surveys.ts +++ b/src/modules/Surveys.ts @@ -1,25 +1,10 @@ -import { NativeEventEmitter, Platform } from 'react-native'; +import { Platform } from 'react-native'; -import { NativeSurveys } from '../native/NativeSurveys'; +import { NativeEvents, NativeSurveys, emitter } from '../native/NativeSurveys'; import type { Survey } from '../native/NativeSurveys'; export type { Survey }; -/** - * @internal You shouldn't use this enum since you never emit or listen - * for native events in your code. - */ -export enum $NativeEvents { - WILL_SHOW_SURVEY_HANDLER = 'IBGWillShowSurvey', - DID_DISMISS_SURVEY_HANDLER = 'IBGDidDismissSurvey', -} - -/** - * @internal You shouldn't use this since you never emit or listen for native - * events in your code. - */ -export const $emitter = new NativeEventEmitter(NativeSurveys); - /** * Sets whether surveys are enabled or not. * If you disable surveys on the SDK but still have active surveys on your Instabug dashboard, @@ -68,7 +53,7 @@ export const setAutoShowingEnabled = (autoShowingSurveysEnabled: boolean) => { * presenting the survey's UI. */ export const setOnShowHandler = (onShowHandler: () => void) => { - $emitter.addListener($NativeEvents.WILL_SHOW_SURVEY_HANDLER, onShowHandler); + emitter.addListener(NativeEvents.WILL_SHOW_SURVEY_HANDLER, onShowHandler); NativeSurveys.setOnShowHandler(onShowHandler); }; @@ -80,7 +65,7 @@ export const setOnShowHandler = (onShowHandler: () => void) => { * the survey's UI is dismissed. */ export const setOnDismissHandler = (onDismissHandler: () => void) => { - $emitter.addListener($NativeEvents.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/test/modules/BugReporting.spec.ts b/test/modules/BugReporting.spec.ts index 89e2c8eee..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, @@ -13,9 +13,9 @@ import { describe('Testing BugReporting Module', () => { beforeEach(() => { - const events = Object.values(BugReporting.$NativeEvents); + const events = Object.values(NativeEvents); events.forEach((event) => { - BugReporting.$emitter.removeAllListeners(event); + emitter.removeAllListeners(event); }); }); @@ -136,11 +136,9 @@ describe('Testing BugReporting Module', () => { it('should invoke callback on emitting the event IBGpreInvocationHandler', () => { const callback = jest.fn(); BugReporting.onInvokeHandler(callback); - BugReporting.$emitter.emit(BugReporting.$NativeEvents.ON_INVOKE_HANDLER); + emitter.emit(NativeEvents.ON_INVOKE_HANDLER); - expect(BugReporting.$emitter.listenerCount(BugReporting.$NativeEvents.ON_INVOKE_HANDLER)).toBe( - 1, - ); + expect(emitter.listenerCount(NativeEvents.ON_INVOKE_HANDLER)).toBe(1); expect(callback).toHaveBeenCalled(); }); @@ -158,14 +156,12 @@ describe('Testing BugReporting Module', () => { const callback = jest.fn(); BugReporting.onSDKDismissedHandler(callback); - BugReporting.$emitter.emit(BugReporting.$NativeEvents.ON_DISMISS_HANDLER, { + emitter.emit(NativeEvents.ON_DISMISS_HANDLER, { dismissType: dismissType, reportType: reportType, }); - expect(BugReporting.$emitter.listenerCount(BugReporting.$NativeEvents.ON_DISMISS_HANDLER)).toBe( - 1, - ); + expect(emitter.listenerCount(NativeEvents.ON_DISMISS_HANDLER)).toBe(1); expect(callback).toBeCalledTimes(1); expect(callback).toBeCalledWith(dismissType, reportType); }); @@ -201,14 +197,10 @@ describe('Testing BugReporting Module', () => { Platform.OS = 'android'; BugReporting.setDidSelectPromptOptionHandler(jest.fn()); - BugReporting.$emitter.emit(BugReporting.$NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, {}); + emitter.emit(NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, {}); expect(NativeBugReporting.setDidSelectPromptOptionHandler).not.toBeCalled(); - expect( - BugReporting.$emitter.listenerCount( - BugReporting.$NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, - ), - ).toBe(0); + expect(emitter.listenerCount(NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER)).toBe(0); }); it('should call setDidSelectPromptOptionHandler event listener when platform is iOS', () => { @@ -217,16 +209,9 @@ describe('Testing BugReporting Module', () => { const payload = { promptOption: 'bug' }; BugReporting.setDidSelectPromptOptionHandler(callback); - BugReporting.$emitter.emit( - BugReporting.$NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, - payload, - ); + emitter.emit(NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, payload); - expect( - BugReporting.$emitter.listenerCount( - BugReporting.$NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, - ), - ).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 955c2dd19..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, @@ -20,9 +20,9 @@ import InstabugUtils from '../../src/utils/InstabugUtils'; describe('Instabug Module', () => { beforeEach(() => { - const events = Object.values(Instabug.$NativeEvents); + const events = Object.values(NativeEvents); events.forEach((event) => { - Instabug.$emitter.removeAllListeners(event); + emitter.removeAllListeners(event); }); }); @@ -641,9 +641,9 @@ describe('Instabug Module', () => { done(); }; Instabug.onReportSubmitHandler(callback); - Instabug.$emitter.emit(Instabug.$NativeEvents.PRESENDING_HANDLER, report); + emitter.emit(NativeEvents.PRESENDING_HANDLER, report); - expect(Instabug.$emitter.listenerCount(Instabug.$NativeEvents.PRESENDING_HANDLER)).toBe(1); + expect(emitter.listenerCount(NativeEvents.PRESENDING_HANDLER)).toBe(1); }); it('should invoke the native method callPrivateApi', () => { diff --git a/test/modules/Replies.spec.ts b/test/modules/Replies.spec.ts index 861c7c646..26fc49b1d 100644 --- a/test/modules/Replies.spec.ts +++ b/test/modules/Replies.spec.ts @@ -1,13 +1,13 @@ import { Platform } from 'react-native'; import * as Replies from '../../src/modules/Replies'; -import { NativeReplies } from '../../src/native/NativeReplies'; +import { NativeEvents, NativeReplies, emitter } from '../../src/native/NativeReplies'; describe('Replies Module', () => { beforeEach(() => { - const events = Object.values(Replies.$NativeEvents); + const events = Object.values(NativeEvents); events.forEach((event) => { - Replies.$emitter.removeAllListeners(event); + emitter.removeAllListeners(event); }); }); @@ -44,9 +44,9 @@ describe('Replies Module', () => { it('should invoke callback on emitting the event IBGOnNewReplyReceivedCallback', () => { const callback = jest.fn(); Replies.setOnNewReplyReceivedHandler(callback); - Replies.$emitter.emit(Replies.$NativeEvents.ON_REPLY_RECEIVED_HANDLER); + emitter.emit(NativeEvents.ON_REPLY_RECEIVED_HANDLER); - expect(Replies.$emitter.listenerCount(Replies.$NativeEvents.ON_REPLY_RECEIVED_HANDLER)).toBe(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 862dda577..d55760752 100644 --- a/test/modules/Surveys.spec.ts +++ b/test/modules/Surveys.spec.ts @@ -1,13 +1,13 @@ import { Platform } from 'react-native'; import * as Surveys from '../../src/modules/Surveys'; -import { NativeSurveys } from '../../src/native/NativeSurveys'; +import { NativeEvents, NativeSurveys, emitter } from '../../src/native/NativeSurveys'; describe('Surveys Module', () => { beforeEach(() => { - const events = Object.values(Surveys.$NativeEvents); + const events = Object.values(NativeEvents); events.forEach((event) => { - Surveys.$emitter.removeAllListeners(event); + emitter.removeAllListeners(event); }); }); @@ -65,9 +65,9 @@ describe('Surveys Module', () => { it('should invoke callback on emitting the event IBGWillShowSurvey', () => { const callback = jest.fn(); Surveys.setOnShowHandler(callback); - Surveys.$emitter.emit(Surveys.$NativeEvents.WILL_SHOW_SURVEY_HANDLER); + emitter.emit(NativeEvents.WILL_SHOW_SURVEY_HANDLER); - expect(Surveys.$emitter.listenerCount(Surveys.$NativeEvents.WILL_SHOW_SURVEY_HANDLER)).toBe(1); + expect(emitter.listenerCount(NativeEvents.WILL_SHOW_SURVEY_HANDLER)).toBe(1); expect(callback).toHaveBeenCalled(); }); @@ -82,11 +82,9 @@ describe('Surveys Module', () => { it('should invoke callback on emitting the event IBGDidDismissSurvey', () => { const callback = jest.fn(); Surveys.setOnDismissHandler(callback); - Surveys.$emitter.emit(Surveys.$NativeEvents.DID_DISMISS_SURVEY_HANDLER); + emitter.emit(NativeEvents.DID_DISMISS_SURVEY_HANDLER); - expect(Surveys.$emitter.listenerCount(Surveys.$NativeEvents.DID_DISMISS_SURVEY_HANDLER)).toBe( - 1, - ); + expect(emitter.listenerCount(NativeEvents.DID_DISMISS_SURVEY_HANDLER)).toBe(1); expect(callback).toHaveBeenCalled(); }); From 7c4f2e86ef4c6a209cb8d5d7a320c03a6aff7908 Mon Sep 17 00:00:00 2001 From: Ahmed Mahmoud Date: Thu, 2 Mar 2023 13:38:16 +0200 Subject: [PATCH 9/9] Export `NetworkDataObfuscationHandler` type --- src/index.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) 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;