Browse Source

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 years ago
parent
commit
27ceea8fb9

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java View File

146
     }
146
     }
147
 
147
 
148
     public void push(ViewController child, CommandListener listener) {
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
         final ViewController toRemove = stack.peek();
153
         final ViewController toRemove = stack.peek();
150
         if (size() > 0) backButtonHelper.addToPushedChild(child);
154
         if (size() > 0) backButtonHelper.addToPushedChild(child);
151
         child.setParentController(this);
155
         child.setParentController(this);

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

325
         uut.selectTab(3);
325
         uut.selectTab(3);
326
 
326
 
327
         SimpleViewController stackChild = new SimpleViewController(activity, childRegistry, "stackChild", new Options());
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
         disablePushAnimation(stackChild, stackChild2);
330
         disablePushAnimation(stackChild, stackChild2);
331
         hideBackButton(stackChild2);
331
         hideBackButton(stackChild2);

+ 12
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerTest.java View File

244
         inOrder.verify(backListener).onSuccess(any());
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
     @Test
259
     @Test
248
     public void animateSetRoot() {
260
     public void animateSetRoot() {
249
         disablePushAnimation(child1, child2, child3);
261
         disablePushAnimation(child1, child2, child3);