9
9
import com .facebook .react .bridge .JavaOnlyArray ;
10
10
import com .facebook .react .bridge .JavaOnlyMap ;
11
11
import com .facebook .react .bridge .Promise ;
12
+ import com .facebook .react .bridge .ReactApplicationContext ;
12
13
import com .facebook .react .bridge .WritableArray ;
13
14
import com .facebook .react .bridge .WritableMap ;
14
15
import com .instabug .library .Feature ;
19
20
import com .instabug .library .ReproConfigurations ;
20
21
import com .instabug .library .ReproMode ;
21
22
import com .instabug .library .internal .module .InstabugLocale ;
23
+ import com .instabug .library .networkDiagnostics .model .NetworkDiagnosticsCallback ;
22
24
import com .instabug .library .ui .onboarding .WelcomeMessage ;
25
+ import com .instabug .reactlibrary .util .GlobalMocks ;
26
+ import com .instabug .reactlibrary .util .MockReflected ;
23
27
import com .instabug .reactlibrary .utils .MainThreadHandler ;
24
28
25
29
import org .junit .After ;
37
41
import java .lang .reflect .Field ;
38
42
import java .lang .reflect .Method ;
39
43
import java .util .ArrayList ;
44
+ import java .util .Date ;
40
45
import java .util .HashMap ;
41
46
import java .util .List ;
42
47
import java .util .Locale ;
45
50
import java .util .concurrent .Executors ;
46
51
import java .util .concurrent .ScheduledExecutorService ;
47
52
53
+ import static com .instabug .reactlibrary .util .GlobalMocks .reflected ;
48
54
import static org .mockito .ArgumentMatchers .anyInt ;
49
55
import static org .mockito .Matchers .any ;
50
56
import static org .mockito .Mockito .mock ;
51
57
import static org .mockito .Mockito .mockConstruction ;
52
58
import static org .mockito .Mockito .mockStatic ;
59
+ import static org .mockito .Mockito .spy ;
53
60
import static org .mockito .Mockito .times ;
54
61
import static org .mockito .Mockito .verify ;
55
62
import static org .mockito .Mockito .when ;
56
63
57
64
58
65
public class RNInstabugReactnativeModuleTest {
59
- private RNInstabugReactnativeModule rnModule = new RNInstabugReactnativeModule (null );
66
+ private RNInstabugReactnativeModule rnModule ;
67
+ private ReactApplicationContext mReactContext = mock (ReactApplicationContext .class );
60
68
61
69
private final static ScheduledExecutorService mainThread = Executors .newSingleThreadScheduledExecutor ();
62
70
@@ -66,7 +74,9 @@ public class RNInstabugReactnativeModuleTest {
66
74
private MockedStatic <Instabug > mockInstabug ;
67
75
68
76
@ Before
69
- public void mockMainThreadHandler () throws Exception {
77
+ public void setUp () throws Exception {
78
+ rnModule = spy (new RNInstabugReactnativeModule (mReactContext ));
79
+
70
80
// Mock static functions
71
81
mockInstabug = mockStatic (Instabug .class );
72
82
mockLooper = mockStatic (Looper .class );
@@ -86,13 +96,19 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
86
96
};
87
97
Mockito .doAnswer (handlerPostAnswer ).when (MainThreadHandler .class );
88
98
MainThreadHandler .runOnMainThread (any (Runnable .class ));
99
+
100
+ // Set up global mocks
101
+ GlobalMocks .setUp ();
89
102
}
90
103
@ After
91
104
public void tearDown () {
92
105
// Remove static mocks
93
106
mockLooper .close ();
94
107
mockMainThreadHandler .close ();
95
108
mockInstabug .close ();
109
+
110
+ // Remove global mocks
111
+ GlobalMocks .close ();
96
112
}
97
113
98
114
/********Instabug*********/
@@ -496,28 +512,17 @@ public void testIdentifyUserWithId() {
496
512
}
497
513
498
514
@ Test
499
- public void givenString$reportCurrentViewChange_whenQuery_thenShouldCallNativeApiWithString () throws Exception {
500
- // when
515
+ public void testReportCurrentViewChange () {
501
516
rnModule .reportCurrentViewChange ("screen" );
502
- Method privateStringMethod = getMethod (Class .forName ("com.instabug.library.Instabug" ), "reportCurrentViewChange" , String .class );
503
- privateStringMethod .setAccessible (true );
504
517
505
- // then
506
- verify (Instabug .class , VerificationModeFactory .times (1 ));
507
- privateStringMethod .invoke ("reportCurrentViewChange" ,"screen" );
518
+ reflected .verify (() -> MockReflected .reportCurrentViewChange ("screen" ), times (1 ));
508
519
}
509
520
510
521
@ Test
511
- public void givenString$reportScreenChange_whenQuery_thenShouldCallNativeApiWithString () throws Exception {
512
- // when
522
+ public void testReportScreenChange () {
513
523
rnModule .reportScreenChange ("screen" );
514
- Method privateStringMethod = getMethod (Class .forName ("com.instabug.library.Instabug" ), "reportScreenChange" , Bitmap .class , String .class );
515
- privateStringMethod .setAccessible (true );
516
-
517
- // then
518
- verify (Instabug .class , VerificationModeFactory .times (1 ));
519
- privateStringMethod .invoke ("reportScreenChange" , null ,"screen" );
520
524
525
+ reflected .verify (() -> MockReflected .reportScreenChange (null , "screen" ), times (1 ));
521
526
}
522
527
523
528
@ Test
@@ -567,4 +572,33 @@ public void testIdentifyUserWithId() {
567
572
verify (Instabug .class ,times (1 ));
568
573
Instabug .clearAllExperiments ();
569
574
}
575
+
576
+ @ Test
577
+ public void testSetOnNetworkDiagnosticsHandler () {
578
+ String date = new Date ().toString ();
579
+ int successOrderCount = 2 ;
580
+ int failureCount = 1 ;
581
+
582
+ MockedStatic <Arguments > mockArgument = mockStatic (Arguments .class );
583
+ mockArgument .when (Arguments ::createMap ).thenReturn (new JavaOnlyMap ());
584
+
585
+ reflected
586
+ .when (() -> MockReflected .setNetworkDiagnosticsCallback (any (NetworkDiagnosticsCallback .class )))
587
+ .thenAnswer ((InvocationOnMock invocation ) -> {
588
+ NetworkDiagnosticsCallback callback = invocation .getArgument (0 );
589
+ callback .onReady (date , successOrderCount , failureCount );
590
+ return null ;
591
+ });
592
+
593
+ rnModule .setOnNetworkDiagnosticsHandler ();
594
+
595
+ WritableMap params = new JavaOnlyMap ();
596
+ params .putString ("date" , date );
597
+ params .putInt ("totalRequestCount" , successOrderCount );
598
+ params .putInt ("failureCount" , failureCount );
599
+
600
+ verify (rnModule ).sendEvent (Constants .IBG_NETWORK_DIAGNOSTICS_HANDLER , params );
601
+
602
+ mockArgument .close ();
603
+ }
570
604
}
0 commit comments