Skip to content

Commit 87b3348

Browse files
authored
Feat/8011 rn77 na android enable native unit tests (#8031)
* Implemented first instrumentation test for the nav library * Fixed more unit tests * Fixed more unit tests * Fixed more unit tests * Revert "Implemented first instrumentation test for the nav library" This reverts commit 10ea533. * Replace activity with context when possible
1 parent ad2075f commit 87b3348

25 files changed

+117
-122
lines changed

lib/android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ dependencies {
105105
if ("Playground".toLowerCase() == rootProject.name.toLowerCase()) {
106106
// tests only for our playground
107107
testImplementation 'junit:junit:4.13.2'
108-
testImplementation "org.robolectric:robolectric:4.7.2"
108+
testImplementation "org.robolectric:robolectric:4.14.1"
109109
testImplementation 'org.assertj:assertj-core:3.11.1'
110110
testImplementation 'org.mockito:mockito-core:4.0.0'
111111
testImplementation 'com.squareup.assertj:assertj-android:1.2.0'
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.reactnativenavigation.react;
22

3-
import android.app.Activity;
3+
import android.content.Context;
44

5-
import com.facebook.react.ReactInstanceManager;
65
import com.reactnativenavigation.viewcontrollers.viewcontroller.ReactViewCreator;
76

87
public class ReactComponentViewCreator implements ReactViewCreator {
98
@Override
10-
public ReactView create(final Activity activity, final String componentId, final String componentName) {
11-
return new ReactView(activity, componentId, componentName);
9+
public ReactView create(final Context context, final String componentId, final String componentName) {
10+
return new ReactView(context, componentId, componentName);
1211
}
1312
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.reactnativenavigation.viewcontrollers.viewcontroller;
22

3-
import android.app.Activity;
3+
import android.content.Context;
44

55
public interface ReactViewCreator {
66

7-
IReactView create(Activity activity, String componentId, String componentName);
7+
IReactView create(Context context, String componentId, String componentName);
88
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package com.reactnativenavigation.views.component;
22

3-
import android.app.Activity;
3+
import android.content.Context;
44

5-
import com.facebook.react.ReactInstanceManager;
6-
import com.reactnativenavigation.viewcontrollers.viewcontroller.IReactView;
7-
import com.reactnativenavigation.viewcontrollers.viewcontroller.ReactViewCreator;
85
import com.reactnativenavigation.react.ReactComponentViewCreator;
96
import com.reactnativenavigation.react.ReactView;
7+
import com.reactnativenavigation.viewcontrollers.viewcontroller.IReactView;
8+
import com.reactnativenavigation.viewcontrollers.viewcontroller.ReactViewCreator;
109

1110
public class ComponentViewCreator implements ReactViewCreator {
1211
@Override
13-
public IReactView create(Activity activity, String componentId, String componentName) {
14-
ReactView reactView = new ReactComponentViewCreator().create(activity, componentId, componentName);
15-
return new ComponentLayout(activity, reactView);
12+
public IReactView create(Context context, String componentId, String componentName) {
13+
ReactView reactView = new ReactComponentViewCreator().create(context, componentId, componentName);
14+
return new ComponentLayout(context, reactView);
1615
}
1716
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.reactnativenavigation.views.stack.topbar;
22

3-
import android.app.Activity;
3+
import android.content.Context;
44

5-
import com.facebook.react.ReactInstanceManager;
65
import com.reactnativenavigation.viewcontrollers.viewcontroller.ReactViewCreator;
76

87
public class TopBarBackgroundViewCreator implements ReactViewCreator {
98

109
@Override
11-
public TopBarBackgroundView create(Activity activity, String componentId, String componentName) {
12-
return new TopBarBackgroundView(activity, componentId, componentName);
10+
public TopBarBackgroundView create(Context context, String componentId, String componentName) {
11+
return new TopBarBackgroundView(context, componentId, componentName);
1312
}
1413
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.reactnativenavigation.views.stack.topbar.titlebar;
22

3-
import android.app.Activity;
3+
import android.content.Context;
44

5-
import com.facebook.react.ReactInstanceManager;
65
import com.reactnativenavigation.viewcontrollers.viewcontroller.ReactViewCreator;
76

87
public class TitleBarReactViewCreator implements ReactViewCreator {
98

109
@Override
11-
public TitleBarReactView create(Activity activity, String componentId, String componentName) {
12-
return new TitleBarReactView(activity, componentId, componentName);
10+
public TitleBarReactView create(Context context, String componentId, String componentName) {
11+
return new TitleBarReactView(context, componentId, componentName);
1312
}
1413
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.reactnativenavigation
2+
3+
import android.content.Context
4+
import android.content.res.Configuration
5+
import android.content.res.Resources
6+
import org.junit.Before
7+
import org.junit.runner.RunWith
8+
import org.mockito.ArgumentMatchers
9+
import org.mockito.Mockito.mock
10+
import org.mockito.kotlin.any
11+
import org.mockito.kotlin.whenever
12+
import org.robolectric.RobolectricTestRunner
13+
import org.robolectric.RuntimeEnvironment
14+
import org.robolectric.annotation.Config
15+
16+
17+
@RunWith(RobolectricTestRunner::class)
18+
@Config(application = TestApplication::class)
19+
abstract class BaseRobolectricTest {
20+
21+
val context: Context = RuntimeEnvironment.getApplication()
22+
lateinit var mockConfiguration: Configuration
23+
24+
25+
@Before
26+
open fun beforeEach() {
27+
NavigationApplication.instance = mock(NavigationApplication::class.java)
28+
mockConfiguration = mock(Configuration::class.java)
29+
val res: Resources = mock(Resources::class.java)
30+
mockConfiguration.uiMode = Configuration.UI_MODE_NIGHT_NO
31+
whenever(res.getConfiguration()).thenReturn(mockConfiguration)
32+
whenever(NavigationApplication.instance.resources).thenReturn(res)
33+
whenever(res.getColor(ArgumentMatchers.anyInt())).thenReturn(0x00000)
34+
whenever(res.getColor(ArgumentMatchers.anyInt(), any())).thenReturn(0x00000)
35+
}
36+
}

lib/android/app/src/test/java/com/reactnativenavigation/EnvironmentTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010

1111
import static org.assertj.core.api.Java6Assertions.*;
1212

13-
import com.reactnativenavigation.R;
14-
15-
@Ignore("New architecture - WIP")
16-
public class EnvironmentTest extends BaseTest {
13+
public class EnvironmentTest extends BaseRobolectricTest {
1714
@Test
1815
public void assertJ() {
1916
assertThat(1 + 2).isEqualTo(3).isGreaterThan(2).isLessThan(4).isNotNegative().isPositive().isNotZero();
@@ -41,6 +38,6 @@ public void androidR() {
4138

4239
@Test
4340
public void ableToLoadApplication() throws Exception {
44-
assertThat(RuntimeEnvironment.application).isNotNull();
41+
assertThat(getContext()).isNotNull();
4542
}
4643
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.reactnativenavigation.mocks;
22

3-
import android.app.Activity;
3+
import static org.mockito.Mockito.spy;
4+
5+
import android.content.Context;
46

57
import com.reactnativenavigation.react.ReactView;
68
import com.reactnativenavigation.viewcontrollers.viewcontroller.ReactViewCreator;
79
import com.reactnativenavigation.views.component.ComponentLayout;
810
import com.reactnativenavigation.views.component.ReactComponent;
911

10-
import static org.mockito.Mockito.spy;
11-
1212
public class TestComponentViewCreator implements ReactViewCreator {
1313
@Override
14-
public ReactComponent create(final Activity activity, final String componentId, final String componentName) {
15-
ReactView reactView = spy(new TestReactView(activity));
16-
return new ComponentLayout(activity, reactView);
14+
public ReactComponent create(final Context context, final String componentId, final String componentName) {
15+
ReactView reactView = spy(new TestReactView(context));
16+
return new ComponentLayout(context, reactView);
1717
}
1818
}

lib/android/app/src/test/java/com/reactnativenavigation/options/LayoutNodeParserTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
import static org.assertj.core.api.Java6Assertions.*;
1010

11-
@Ignore("New architecture - WIP")
12-
public class LayoutNodeParserTest extends BaseTest {
11+
public class LayoutNodeParserTest extends BaseRobolectricTest {
1312
@Test
1413
public void dto() throws Exception {
1514
LayoutNode node = new LayoutNode("the id", LayoutNode.Type.Component);

0 commit comments

Comments
 (0)