浏览代码

Reject promise when trying to push two children with same id (#5855)

Reject promise when trying to push two children with the same if into the same stack.

Related to #5821
Closes #5689

Co-authored-by: Yogev Ben David <yogevbd@wix.com>
Guy Carmeli 5 年前
父节点
当前提交
27ceea8fb9

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java 查看文件

@@ -146,6 +146,10 @@ public class StackController extends ParentController<StackLayout> {
146 146
     }
147 147
 
148 148
     public void push(ViewController child, CommandListener listener) {
149
+        if (findController(child.getId()) != null) {
150
+            listener.onError("A stack can't contain two children with the same id");
151
+            return;
152
+        }
149 153
         final ViewController toRemove = stack.peek();
150 154
         if (size() > 0) backButtonHelper.addToPushedChild(child);
151 155
         child.setParentController(this);

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsControllerTest.java 查看文件

@@ -325,7 +325,7 @@ public class BottomTabsControllerTest extends BaseTest {
325 325
         uut.selectTab(3);
326 326
 
327 327
         SimpleViewController stackChild = new SimpleViewController(activity, childRegistry, "stackChild", new Options());
328
-        SimpleViewController stackChild2 = new SimpleViewController(activity, childRegistry, "stackChild", new Options());
328
+        SimpleViewController stackChild2 = new SimpleViewController(activity, childRegistry, "stackChild2", new Options());
329 329
 
330 330
         disablePushAnimation(stackChild, stackChild2);
331 331
         hideBackButton(stackChild2);

+ 12
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerTest.java 查看文件

@@ -244,6 +244,18 @@ public class StackControllerTest extends BaseTest {
244 244
         inOrder.verify(backListener).onSuccess(any());
245 245
     }
246 246
 
247
+    @Test
248
+    public void push_rejectIfStackContainsChildWithId() {
249
+        disablePushAnimation(child1);
250
+        uut.push(child1, new CommandListenerAdapter());
251
+        assertThat(uut.size()).isEqualTo(1);
252
+
253
+        CommandListenerAdapter listener = spy(new CommandListenerAdapter());
254
+        uut.push(child1a, listener);
255
+        verify(listener).onError(any());
256
+        assertThat(uut.size()).isEqualTo(1);
257
+    }
258
+
247 259
     @Test
248 260
     public void animateSetRoot() {
249 261
         disablePushAnimation(child1, child2, child3);