-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathNavigationModuleTest.java
More file actions
119 lines (103 loc) · 4.4 KB
/
Copy pathNavigationModuleTest.java
File metadata and controls
119 lines (103 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.reactnativenavigation.react;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import com.reactnativenavigation.BaseTest;
import com.reactnativenavigation.NavigationActivity;
import com.reactnativenavigation.options.LayoutFactory;
import com.reactnativenavigation.options.LayoutNode;
import com.reactnativenavigation.options.parsers.JSONParser;
import com.reactnativenavigation.viewcontrollers.navigator.Navigator;
import com.reactnativenavigation.viewcontrollers.viewcontroller.ViewController;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.robolectric.annotation.LooperMode;
import org.robolectric.shadows.ShadowLooper;
@LooperMode(LooperMode.Mode.PAUSED)
public class NavigationModuleTest extends BaseTest {
private NavigationModule uut;
private Navigator navigator;
private JSONParser jsonParser;
private NavigationActivity activity;
private ReactApplicationContext reactApplicationContext;
private LayoutFactory layoutFactory;
@Override
public void beforeEach() {
jsonParser = mock(JSONParser.class);
navigator = mock(Navigator.class);
activity = mockActivity();
reactApplicationContext = mock(ReactApplicationContext.class);
layoutFactory = mock(LayoutFactory.class);
uut = spy(new NavigationModule(
reactApplicationContext,
jsonParser,
layoutFactory
));
}
@Test
public void setRoot_delegatesToNavigator() throws JSONException {
when(reactApplicationContext.getCurrentActivity()).thenReturn(activity);
ReadableMap root = mock(ReadableMap.class);
when(jsonParser.parse(root)).thenReturn(rootJson());
ViewController rootViewController = mock(ViewController.class);
when(layoutFactory.create(any(LayoutNode.class))).thenReturn(rootViewController);
uut.setRoot("1", root, mock(Promise.class));
ShadowLooper.idleMainLooper();
verify(navigator).setRoot(eq(rootViewController), any());
}
@Test
public void postCommandsOnMainThread_doesNotCrashIfActivityIsNull() {
NavigationModule spy = spy(uut);
when(spy.activity()).thenReturn(mock(NavigationActivity.class));
Runnable runnable = mock(Runnable.class);
spy.handle(runnable);
ShadowLooper.idleMainLooper();
verify(runnable).run();
when(spy.activity()).thenReturn(null);
Runnable dontRun = mock(Runnable.class);
spy.handle(dontRun);
ShadowLooper.idleMainLooper();
verify(dontRun, times(0)).run();
}
@Test
public void foreignActivity_isIgnoredByCommandsLifecycleAndInvalidate() {
when(reactApplicationContext.getCurrentActivity()).thenReturn(newActivity());
Runnable command = mock(Runnable.class);
uut.handle(command);
ShadowLooper.idleMainLooper();
verify(command, times(0)).run();
ArgumentCaptor<LifecycleEventListener> listener = ArgumentCaptor.forClass(LifecycleEventListener.class);
verify(reactApplicationContext).addLifecycleEventListener(listener.capture());
listener.getValue().onHostResume();
listener.getValue().onHostPause();
ShadowLooper.idleMainLooper();
uut.invalidate();
}
private NavigationActivity mockActivity() {
NavigationActivity activity = mock(NavigationActivity.class);
when(activity.getNavigator()).thenReturn(navigator);
return activity;
}
private JSONObject rootJson() throws JSONException {
JSONObject root = new JSONObject();
root.put("root", componentJson());
return root;
}
private JSONObject componentJson() throws JSONException {
JSONObject component = new JSONObject();
component.put("id", "Component1");
component.put("type", "Component");
component.put("data", new JSONObject().put("name", "mockComponent"));
return component;
}
}