Browse Source

Clear one time options in clearOptions

This resolves an issue where currentTabIndex, for example, was not cleared properly
if it was set in initialOptions as it was cloned each time clearOptions was invoked.
Guy Carmeli 6 years ago
parent
commit
4c48ad7c37

+ 2
- 1
lib/android/app/src/main/java/com/reactnativenavigation/parse/Options.java View File

@@ -139,8 +139,9 @@ public class Options {
139 139
         return this;
140 140
     }
141 141
 
142
-    public void clearOneTimeOptions() {
142
+    public Options clearOneTimeOptions() {
143 143
         bottomTabsOptions.currentTabId = new NullText();
144 144
         bottomTabsOptions.currentTabIndex = new NullNumber();
145
+        return this;
145 146
     }
146 147
 }

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ParentController.java View File

@@ -118,7 +118,7 @@ public abstract class ParentController<T extends ViewGroup> extends ChildControl
118 118
 	@CallSuper
119 119
     protected void clearOptions() {
120 120
 	    applyOnParentController(parent -> ((ParentController) parent).clearOptions());
121
-        options = initialOptions.copy();
121
+        options = initialOptions.copy().clearOneTimeOptions();
122 122
     }
123 123
 
124 124
     public void setupTopTabsWithViewPager(ViewPager viewPager) {

+ 16
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/BottomTabsControllerTest.java View File

@@ -47,6 +47,7 @@ public class BottomTabsControllerTest extends BaseTest {
47 47
 
48 48
     private Activity activity;
49 49
     private BottomTabsController uut;
50
+    private Options initialOptions = new Options();
50 51
     private ViewController child1;
51 52
     private ViewController child2;
52 53
     private ViewController child3;
@@ -258,6 +259,20 @@ public class BottomTabsControllerTest extends BaseTest {
258 259
                 .isEqualTo(((ViewGroup.MarginLayoutParams) child6.getView().getLayoutParams()).topMargin);
259 260
     }
260 261
 
262
+    @Test
263
+    public void oneTimeOptionsAreAppliedOnce() {
264
+        initialOptions.bottomTabsOptions.currentTabIndex = new Number(1);
265
+        BottomTabsController spy = spy(createBottomTabs());
266
+        spy.onViewAppeared();
267
+
268
+        assertThat(spy.getSelectedIndex()).isOne();
269
+        spy.selectTab(0);
270
+        tabs.get(0).onViewAppeared();
271
+        verify(spy).clearOptions();
272
+        assertThat(spy.getSelectedIndex()).isZero();
273
+        assertThat(spy.options.bottomTabsOptions.currentTabIndex.hasValue()).isFalse();
274
+    }
275
+
261 276
     @NonNull
262 277
     private List<ViewController> createTabs() {
263 278
         return Arrays.asList(child1, child2, child3, child4, child5);
@@ -282,7 +297,7 @@ public class BottomTabsControllerTest extends BaseTest {
282 297
                 eventEmitter,
283 298
                 imageLoaderMock,
284 299
                 "uut",
285
-                new Options(),
300
+                initialOptions,
286 301
                 new OptionsPresenter(activity, new Options()),
287 302
                 presenter,
288 303
                 new BottomTabOptionsPresenter(activity, tabs, new Options())) {