소스 검색

Consume currentTabId and currentTabIndex after they are applied (#6169)

If currentTabId or currentTabIndex were declared in default options - the specified tab was reselected each time options were applied.

Co-authored-by: Yogev Ben David <yogev132@gmail.com>
Guy Carmeli 4 년 전
부모
커밋
3e5be29af8
No account linked to committer's email address

+ 8
- 2
lib/android/app/src/main/java/com/reactnativenavigation/presentation/BottomTabsPresenter.java 파일 보기

@@ -124,12 +124,18 @@ public class BottomTabsPresenter {
124 124
         bottomTabs.setBackgroundColor(bottomTabsOptions.backgroundColor.get(Color.WHITE));
125 125
         if (bottomTabsOptions.currentTabIndex.hasValue()) {
126 126
             int tabIndex = bottomTabsOptions.currentTabIndex.get();
127
-            if (tabIndex >= 0) tabSelector.selectTab(tabIndex);
127
+            if (tabIndex >= 0) {
128
+                bottomTabsOptions.currentTabIndex.consume();
129
+                tabSelector.selectTab(tabIndex);
130
+            }
128 131
         }
129 132
         if (bottomTabsOptions.testId.hasValue()) bottomTabs.setTag(bottomTabsOptions.testId.get());
130 133
         if (bottomTabsOptions.currentTabId.hasValue()) {
131 134
             int tabIndex = bottomTabFinder.findByControllerId(bottomTabsOptions.currentTabId.get());
132
-            if (tabIndex >= 0) tabSelector.selectTab(tabIndex);
135
+            if (tabIndex >= 0) {
136
+                bottomTabsOptions.currentTabId.consume();
137
+                tabSelector.selectTab(tabIndex);
138
+            }
133 139
         }
134 140
         if (bottomTabsOptions.visible.isTrueOrUndefined()) {
135 141
             if (bottomTabsOptions.animate.isTrueOrUndefined()) {

+ 32
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/BottomTabsPresenterTest.java 파일 보기

@@ -8,6 +8,8 @@ import com.reactnativenavigation.mocks.SimpleViewController;
8 8
 import com.reactnativenavigation.parse.Options;
9 9
 import com.reactnativenavigation.parse.params.Bool;
10 10
 import com.reactnativenavigation.parse.params.Colour;
11
+import com.reactnativenavigation.parse.params.Number;
12
+import com.reactnativenavigation.parse.params.Text;
11 13
 import com.reactnativenavigation.presentation.BottomTabsPresenter;
12 14
 import com.reactnativenavigation.viewcontrollers.bottomtabs.TabSelector;
13 15
 import com.reactnativenavigation.views.BottomTabs;
@@ -20,6 +22,7 @@ import java.util.List;
20 22
 
21 23
 import static org.assertj.core.api.Java6Assertions.assertThat;
22 24
 import static org.mockito.ArgumentMatchers.any;
25
+import static org.mockito.Mockito.mock;
23 26
 import static org.mockito.Mockito.spy;
24 27
 import static org.mockito.Mockito.times;
25 28
 import static org.mockito.Mockito.verify;
@@ -30,6 +33,7 @@ public class BottomTabsPresenterTest extends BaseTest {
30 33
     private BottomTabsPresenter uut;
31 34
     private BottomTabs bottomTabs;
32 35
     private BottomTabsAnimator animator;
36
+    private TabSelector tabSelector;
33 37
 
34 38
     @Override
35 39
     public void beforeEach() {
@@ -41,7 +45,8 @@ public class BottomTabsPresenterTest extends BaseTest {
41 45
         uut = new BottomTabsPresenter(tabs, new Options());
42 46
         bottomTabs = Mockito.mock(BottomTabs.class);
43 47
         animator = spy(new BottomTabsAnimator(bottomTabs));
44
-        uut.bindView(bottomTabs, Mockito.mock(TabSelector.class), animator);
48
+        tabSelector = mock(TabSelector.class);
49
+        uut.bindView(bottomTabs, tabSelector, animator);
45 50
     }
46 51
 
47 52
     @Test
@@ -71,4 +76,30 @@ public class BottomTabsPresenterTest extends BaseTest {
71 76
         uut.mergeChildOptions(options, tabs.get(0));
72 77
         verify(animator).hide(any());
73 78
     }
79
+
80
+    @Test
81
+    public void applyChildOptions_currentTabIndexIsConsumedAfterApply() {
82
+        Options defaultOptions = new Options();
83
+        defaultOptions.bottomTabsOptions.currentTabIndex = new Number(1);
84
+        uut.setDefaultOptions(defaultOptions);
85
+
86
+        uut.applyChildOptions(Options.EMPTY, tabs.get(0));
87
+        verify(tabSelector).selectTab(1);
88
+
89
+        uut.applyChildOptions(Options.EMPTY, tabs.get(0));
90
+        verifyNoMoreInteractions(tabSelector);
91
+    }
92
+
93
+    @Test
94
+    public void applyChildOptions_currentTabIdIsConsumedAfterApply() {
95
+        Options defaultOptions = new Options();
96
+        defaultOptions.bottomTabsOptions.currentTabId = new Text(tabs.get(1).getId());
97
+        uut.setDefaultOptions(defaultOptions);
98
+
99
+        uut.applyChildOptions(Options.EMPTY, tabs.get(0));
100
+        verify(tabSelector).selectTab(1);
101
+
102
+        uut.applyChildOptions(Options.EMPTY, tabs.get(0));
103
+        verifyNoMoreInteractions(tabSelector);
104
+    }
74 105
 }