11package com .instabug .reactlibrary ;
22
33import android .os .Looper ;
4+
5+ import com .facebook .react .bridge .Arguments ;
46import com .facebook .react .bridge .JavaOnlyArray ;
7+ import com .facebook .react .bridge .JavaOnlyMap ;
8+ import com .facebook .react .bridge .ReactApplicationContext ;
9+ import com .facebook .react .bridge .WritableMap ;
510import com .instabug .bug .BugReporting ;
611import com .instabug .library .Feature ;
12+ import com .instabug .library .OnSdkDismissCallback ;
713import com .instabug .library .extendedbugreport .ExtendedBugReport ;
814import com .instabug .library .invocation .InstabugInvocationEvent ;
15+ import com .instabug .library .invocation .OnInvokeCallback ;
916import com .instabug .library .invocation .util .InstabugVideoRecordingButtonPosition ;
17+ import com .instabug .reactlibrary .utils .InstabugUtil ;
1018import com .instabug .reactlibrary .utils .MainThreadHandler ;
1119
1220
1321import org .junit .After ;
1422import org .junit .Before ;
1523import org .junit .Test ;
16- import org .junit .runner .RunWith ;
1724import org .mockito .Matchers ;
1825import org .mockito .MockedStatic ;
19- import org .mockito .Mockito ;
2026import org .mockito .internal .verification .VerificationModeFactory ;
2127import org .mockito .invocation .InvocationOnMock ;
2228import org .mockito .stubbing .Answer ;
2733import java .util .concurrent .ScheduledExecutorService ;
2834
2935import static org .mockito .ArgumentMatchers .any ;
36+ import static org .mockito .ArgumentMatchers .eq ;
3037import static org .mockito .Mockito .doAnswer ;
3138import static org .mockito .Mockito .mock ;
3239import static org .mockito .Mockito .mockStatic ;
3340import static org .mockito .Mockito .verify ;
3441import static org .mockito .Mockito .when ;
3542
36-
37-
3843public class RNInstabugBugReportingModuleTest {
3944
40- private RNInstabugBugReportingModule bugReportingModule = new RNInstabugBugReportingModule (null );
45+ private RNInstabugBugReportingModule bugReportingModule = new RNInstabugBugReportingModule (mock ( ReactApplicationContext . class ) );
4146 private final static ScheduledExecutorService mainThread = Executors .newSingleThreadScheduledExecutor ();
4247
4348 // Mock Objects
4449 private MockedStatic <Looper > mockLooper ;
4550 private MockedStatic <MainThreadHandler > mockMainThreadHandler ;
4651 private MockedStatic <BugReporting > mockBugReporting ;
52+ private MockedStatic <InstabugUtil > mockInstabugUtil ;
4753
4854 @ Before
4955 public void mockMainThreadHandler () throws Exception {
5056 // Mock static functions
5157 mockBugReporting = mockStatic (BugReporting .class );
5258 mockLooper = mockStatic (Looper .class );
5359 mockMainThreadHandler = mockStatic (MainThreadHandler .class );
60+ mockInstabugUtil = mockStatic (InstabugUtil .class );
5461
5562 // Mock Looper class
5663 Looper mockMainThreadLooper = mock (Looper .class );
@@ -73,6 +80,7 @@ public void tearDown() {
7380 mockLooper .close ();
7481 mockMainThreadHandler .close ();
7582 mockBugReporting .close ();
83+ mockInstabugUtil .close ();
7684 }
7785
7886 /********BugReporting*********/
@@ -222,68 +230,52 @@ public void tearDown() {
222230 BugReporting .setOptions (option2 );
223231 }
224232
225- // @Test
226- // public void given$setOnInvokeHandler_whenQuery_thenShouldSetNativeCallback() {
227- //
228- // try {
229- // // given
230- //
231- // mockStatic(InstabugUtil.class);
232- // // when
233- // doAnswer(new Answer<Object>() {
234- // @Override
235- // public Object answer(InvocationOnMock invocation) {
236- // ((OnInvokeCallback) invocation.getArguments()[0]).onInvoke();
237- // return null;
238- // }
239- // }).when(BugReporting.class);
240- // BugReporting.setOnDismissCallback(anyObject());
241- // bugReportingModule.setOnInvokeHandler(null);
242- // // then
243- // verify(InstabugUtil.class,VerificationModeFactory.times(1));
244- //
245- // InstabugUtil.sendEvent(any(ReactApplicationContext.class), eq(Constants.IBG_PRE_INVOCATION_HANDLER), Matchers.isNull(WritableMap.class));
246- //
247- //
248- // } catch (Exception e) {
249- // e.printStackTrace();
250- // }
251- // }
252- //
253-
254- // @Test
255- // public void given$setOnSDKDismissedHandler_whenQuery_thenShouldSetNativeCallback() {
256- // try {
257- // // given
258- // mockStatic(Arguments.class);
259- //
260- // // when
261- // when(Arguments.createMap()).thenReturn(new JavaOnlyMap());
262- // doAnswer(new Answer<Object>() {
263- // @Override
264- // public Object answer(InvocationOnMock invocation) {
265- // mockStatic(InstabugUtil.class);
266- // doNothing().when(InstabugUtil.class);
267- // InstabugUtil.sendEvent(any(),any(),any());
268- // ((OnSdkDismissCallback) invocation.getArguments()[0])
269- // .call(OnSdkDismissCallback.DismissType.CANCEL, OnSdkDismissCallback.ReportType.BUG);
270- // return null;
271- // }
272- // }).when(BugReporting.class);
273- // BugReporting.setOnDismissCallback(anyObject());
274- // bugReportingModule.setOnSDKDismissedHandler(null);
275- // // then
276- // WritableMap params = new JavaOnlyMap();
277- // params.putString("dismissType", OnSdkDismissCallback.DismissType.CANCEL.toString());
278- // params.putString("reportType", OnSdkDismissCallback.ReportType.BUG.toString());
279- // verify(InstabugUtil.class,VerificationModeFactory.times(1));
280- //
281- // InstabugUtil.sendEvent(any(ReactApplicationContext.class), eq(Constants.IBG_POST_INVOCATION_HANDLER), eq(params));
282- //
283- // } catch (Exception e) {
284- // e.printStackTrace();
285- // }
286- // }
233+ @ Test
234+ public void given$setOnInvokeHandler_whenQuery_thenShouldSetNativeCallback () {
235+ // when
236+ mockBugReporting .when (() -> BugReporting .setOnInvokeCallback (any (OnInvokeCallback .class )))
237+ .thenAnswer (new Answer <Object >() {
238+ @ Override
239+ public Object answer (InvocationOnMock invocation ) {
240+ ((OnInvokeCallback ) invocation .getArguments ()[0 ]).onInvoke ();
241+ return null ;
242+ }
243+ });
244+
245+ bugReportingModule .setOnInvokeHandler (null );
246+
247+ // then
248+ verify (InstabugUtil .class ,VerificationModeFactory .times (1 ));
249+ InstabugUtil .sendEvent (any (ReactApplicationContext .class ), eq (Constants .IBG_PRE_INVOCATION_HANDLER ), Matchers .isNull (WritableMap .class ));
250+ }
251+
252+
253+ @ Test
254+ public void given$setOnSDKDismissedHandler_whenQuery_thenShouldSetNativeCallback () {
255+ // given
256+ MockedStatic mockArgument = mockStatic (Arguments .class );
257+ MockedStatic mockReactApplicationContext = mockStatic (ReactApplicationContext .class );
258+
259+ // when
260+ when (Arguments .createMap ()).thenReturn (new JavaOnlyMap ());
261+ mockBugReporting .when (() -> BugReporting .setOnDismissCallback (any (OnSdkDismissCallback .class ))).thenAnswer (new Answer () {
262+ public Object answer (InvocationOnMock invocation ) {
263+ InstabugUtil .sendEvent (any (),any (),any ());
264+ ((OnSdkDismissCallback ) invocation .getArguments ()[0 ])
265+ .call (OnSdkDismissCallback .DismissType .CANCEL , OnSdkDismissCallback .ReportType .BUG );
266+ return null ;
267+ }});
268+ bugReportingModule .setOnSDKDismissedHandler (null );
269+
270+ // then
271+ WritableMap params = new JavaOnlyMap ();
272+ params .putString ("dismissType" , OnSdkDismissCallback .DismissType .CANCEL .toString ());
273+ params .putString ("reportType" , OnSdkDismissCallback .ReportType .BUG .toString ());
274+ verify (InstabugUtil .class ,VerificationModeFactory .times (1 ));
275+ InstabugUtil .sendEvent (any (ReactApplicationContext .class ), eq (Constants .IBG_POST_INVOCATION_HANDLER ), eq (params ));
276+ mockArgument .close ();
277+ mockReactApplicationContext .close ();
278+ }
287279
288280 @ Test
289281 public void givenArray$setReportTypes_whenQuery_thenShouldCallNativeApiWithEnumArgs () {
0 commit comments