From 27ceea8fb92506fdd756e45768e7235e3e7babc6 Mon Sep 17 00:00:00 2001 From: Guy Carmeli Date: Mon, 20 Jan 2020 15:19:53 +0200 Subject: [PATCH] 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 --- .../viewcontrollers/stack/StackController.java | 4 ++++ .../bottomtabs/BottomTabsControllerTest.java | 2 +- .../viewcontrollers/stack/StackControllerTest.java | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java index 2092fbb74de..f7bd7e3a404 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java +++ b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java @@ -146,6 +146,10 @@ public void onChildDestroyed(ViewController child) { } public void push(ViewController child, CommandListener listener) { + if (findController(child.getId()) != null) { + listener.onError("A stack can't contain two children with the same id"); + return; + } final ViewController toRemove = stack.peek(); if (size() > 0) backButtonHelper.addToPushedChild(child); child.setParentController(this); diff --git a/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsControllerTest.java b/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsControllerTest.java index 14e79aba166..c28d44378ac 100644 --- a/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsControllerTest.java +++ b/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsControllerTest.java @@ -325,7 +325,7 @@ public void push() { uut.selectTab(3); SimpleViewController stackChild = new SimpleViewController(activity, childRegistry, "stackChild", new Options()); - SimpleViewController stackChild2 = new SimpleViewController(activity, childRegistry, "stackChild", new Options()); + SimpleViewController stackChild2 = new SimpleViewController(activity, childRegistry, "stackChild2", new Options()); disablePushAnimation(stackChild, stackChild2); hideBackButton(stackChild2); diff --git a/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerTest.java b/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerTest.java index 0cc7f850582..60ef5d2030e 100644 --- a/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerTest.java +++ b/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerTest.java @@ -244,6 +244,18 @@ public void push_backPressedDuringPushAnimationDestroysPushedScreenImmediately() inOrder.verify(backListener).onSuccess(any()); } + @Test + public void push_rejectIfStackContainsChildWithId() { + disablePushAnimation(child1); + uut.push(child1, new CommandListenerAdapter()); + assertThat(uut.size()).isEqualTo(1); + + CommandListenerAdapter listener = spy(new CommandListenerAdapter()); + uut.push(child1a, listener); + verify(listener).onError(any()); + assertThat(uut.size()).isEqualTo(1); + } + @Test public void animateSetRoot() { disablePushAnimation(child1, child2, child3);