Skip to content

Commit 74b4cbf

Browse files
author
Ali Abdelfattah
authored
[MOB-8935] Fix failing event tests (#736)
* Fix event tests of the Bug Reporting Java module * Fix event tests of the Surveys Java module
1 parent 7a0a6f5 commit 74b4cbf

File tree

2 files changed

+108
-117
lines changed

2 files changed

+108
-117
lines changed

android/src/test/java/com/instabug/reactlibrary/RNInstabugBugReportingModuleTest.java

Lines changed: 59 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
package com.instabug.reactlibrary;
22

33
import android.os.Looper;
4+
5+
import com.facebook.react.bridge.Arguments;
46
import 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;
510
import com.instabug.bug.BugReporting;
611
import com.instabug.library.Feature;
12+
import com.instabug.library.OnSdkDismissCallback;
713
import com.instabug.library.extendedbugreport.ExtendedBugReport;
814
import com.instabug.library.invocation.InstabugInvocationEvent;
15+
import com.instabug.library.invocation.OnInvokeCallback;
916
import com.instabug.library.invocation.util.InstabugVideoRecordingButtonPosition;
17+
import com.instabug.reactlibrary.utils.InstabugUtil;
1018
import com.instabug.reactlibrary.utils.MainThreadHandler;
1119

1220

1321
import org.junit.After;
1422
import org.junit.Before;
1523
import org.junit.Test;
16-
import org.junit.runner.RunWith;
1724
import org.mockito.Matchers;
1825
import org.mockito.MockedStatic;
19-
import org.mockito.Mockito;
2026
import org.mockito.internal.verification.VerificationModeFactory;
2127
import org.mockito.invocation.InvocationOnMock;
2228
import org.mockito.stubbing.Answer;
@@ -27,30 +33,31 @@
2733
import java.util.concurrent.ScheduledExecutorService;
2834

2935
import static org.mockito.ArgumentMatchers.any;
36+
import static org.mockito.ArgumentMatchers.eq;
3037
import static org.mockito.Mockito.doAnswer;
3138
import static org.mockito.Mockito.mock;
3239
import static org.mockito.Mockito.mockStatic;
3340
import static org.mockito.Mockito.verify;
3441
import static org.mockito.Mockito.when;
3542

36-
37-
3843
public 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() {

android/src/test/java/com/instabug/reactlibrary/RNInstabugSurveysModuleTest.java

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
import com.facebook.react.bridge.Arguments;
77
import com.facebook.react.bridge.Callback;
88
import com.facebook.react.bridge.JavaOnlyArray;
9+
import com.facebook.react.bridge.ReactApplicationContext;
10+
import com.facebook.react.bridge.WritableMap;
911
import com.instabug.library.Feature;
1012
import com.instabug.reactlibrary.utils.InstabugUtil;
1113
import com.instabug.survey.Survey;
1214
import com.instabug.survey.Surveys;
1315
import com.instabug.reactlibrary.utils.MainThreadHandler;
16+
import com.instabug.survey.callbacks.OnDismissCallback;
17+
import com.instabug.survey.callbacks.OnShowCallback;
1418

1519

1620
import org.json.JSONArray;
@@ -19,6 +23,7 @@
1923
import org.junit.After;
2024
import org.junit.Before;
2125
import org.junit.Test;
26+
import org.mockito.Matchers;
2227
import org.mockito.MockedStatic;
2328
import org.mockito.Mockito;
2429
import org.mockito.invocation.InvocationOnMock;
@@ -29,6 +34,7 @@
2934
import java.util.concurrent.ScheduledExecutorService;
3035

3136
import static org.mockito.Matchers.any;
37+
import static org.mockito.ArgumentMatchers.eq;
3238
import static org.mockito.Mockito.doAnswer;
3339
import static org.mockito.Mockito.mock;
3440
import static org.mockito.Mockito.mockStatic;
@@ -38,21 +44,23 @@
3844

3945
public class RNInstabugSurveysModuleTest {
4046

41-
private RNInstabugSurveysModule surveysModule = new RNInstabugSurveysModule(null);
47+
private RNInstabugSurveysModule surveysModule = new RNInstabugSurveysModule(mock(ReactApplicationContext.class));
4248

4349
private final static ScheduledExecutorService mainThread = Executors.newSingleThreadScheduledExecutor();
4450

4551
// Mock Objects
4652
private MockedStatic<Looper> mockLooper;
4753
private MockedStatic <MainThreadHandler> mockMainThreadHandler;
4854
private MockedStatic <Surveys> mockSurveys;
55+
private MockedStatic <InstabugUtil> mockInstabugUtil;
4956

5057
@Before
5158
public void mockMainThreadHandler() throws Exception {
5259
// Mock static functions
5360
mockSurveys = mockStatic(Surveys.class);
5461
mockLooper = mockStatic(Looper.class);
5562
mockMainThreadHandler = mockStatic(MainThreadHandler.class);
63+
mockInstabugUtil = mockStatic(InstabugUtil.class);
5664

5765
// Mock Looper class
5866
Looper mockMainThreadLooper = mock(Looper.class);
@@ -75,6 +83,7 @@ public void tearDown() {
7583
mockLooper.close();
7684
mockMainThreadHandler.close();
7785
mockSurveys.close();
86+
mockInstabugUtil.close();
7887
}
7988

8089
/********Surveys*********/
@@ -111,7 +120,6 @@ public void tearDown() {
111120
// given
112121
MockedStatic mockClock = mockStatic(SystemClock.class);
113122
MockedStatic mockArgument =mockStatic(Arguments.class);
114-
mockStatic(InstabugUtil.class);
115123

116124
Callback callback = mock(Callback.class);
117125
JSONArray json = mock(JSONArray.class);
@@ -156,54 +164,45 @@ public JSONArray answer(InvocationOnMock invocation) throws Throwable {
156164
Surveys.setAutoShowingEnabled(true);
157165
}
158166

159-
// @Test
160-
// public void given$setOnShowSurveyHandler_whenQuery_thenShouldSetNativeCallback() {
161-
//
162-
// try {
163-
// // given
164-
// mockStatic(InstabugUtil.class);
165-
// // when
166-
// doAnswer(new Answer<Object>() {
167-
// @Override
168-
// public Object answer(InvocationOnMock invocation) {
169-
// ((OnShowCallback) invocation.getArguments()[0]).onShow();
170-
// return null;
171-
// }
172-
// }).when(Surveys.class);
173-
// Surveys.setOnShowCallback(Matchers.anyObject());
174-
// surveysModule.setOnShowHandler(null);
175-
// // then
176-
// verify(InstabugUtil.class,times(1));
177-
// InstabugUtil.sendEvent(any(ReactApplicationContext.class), eq(Constants.IBG_ON_SHOW_SURVEY_HANDLER), Matchers.isNull(WritableMap.class));
178-
//
179-
// } catch (Exception e) {
180-
// e.printStackTrace();
181-
// }
182-
// }
183-
184-
// @Test
185-
// public void given$setOnDismissSurveyHandler_whenQuery_thenShouldSetNativeCallback() {
186-
// try {
187-
// // given
188-
// mockStatic(InstabugUtil.class);
189-
// // when
190-
// doAnswer(new Answer<Object>() {
191-
// @Override
192-
// public Object answer(InvocationOnMock invocation) {
193-
// ((OnDismissCallback) invocation.getArguments()[0]).onDismiss();
194-
// return null;
195-
// }
196-
// }).when(Surveys.class);
197-
// Surveys.setOnDismissCallback(Matchers.anyObject());
198-
// surveysModule.setOnDismissHandler(null);
199-
// // then
200-
// verify(InstabugUtil.class,times(1));
201-
// InstabugUtil.sendEvent(any(ReactApplicationContext.class), eq(Constants.IBG_ON_DISMISS_SURVEY_HANDLER), Matchers.isNull(WritableMap.class));
202-
//
203-
// } catch (Exception e) {
204-
// e.printStackTrace();
205-
// }
206-
// }
167+
@Test
168+
public void given$setOnShowSurveyHandler_whenQuery_thenShouldSetNativeCallback() {
169+
// given
170+
171+
// when
172+
mockSurveys.when(() -> Surveys.setOnShowCallback(any(OnShowCallback.class)))
173+
.thenAnswer(new Answer<Object>() {
174+
@Override
175+
public Object answer(InvocationOnMock invocation) {
176+
((OnShowCallback) invocation.getArguments()[0]).onShow();
177+
return null;
178+
}
179+
});
180+
surveysModule.setOnShowHandler(null);
181+
182+
// then
183+
verify(InstabugUtil.class,times(1));
184+
InstabugUtil.sendEvent(any(ReactApplicationContext.class), eq(Constants.IBG_ON_SHOW_SURVEY_HANDLER), Matchers.isNull(WritableMap.class));
185+
}
186+
187+
@Test
188+
public void given$setOnDismissSurveyHandler_whenQuery_thenShouldSetNativeCallback() {
189+
// given
190+
191+
// when
192+
mockSurveys.when(() -> Surveys.setOnDismissCallback(any(OnDismissCallback.class)))
193+
.thenAnswer(new Answer<Object>() {
194+
@Override
195+
public Object answer(InvocationOnMock invocation) {
196+
((OnDismissCallback) invocation.getArguments()[0]).onDismiss();
197+
return null;
198+
}
199+
});
200+
surveysModule.setOnDismissHandler(null);
201+
202+
// then
203+
verify(InstabugUtil.class,times(1));
204+
InstabugUtil.sendEvent(any(ReactApplicationContext.class), eq(Constants.IBG_ON_DISMISS_SURVEY_HANDLER), Matchers.isNull(WritableMap.class));
205+
}
207206

208207
@Test
209208
public void givenString$showSurvey_whenQuery_thenShouldCallNativeApiWithString() {

0 commit comments

Comments
 (0)