Browse Source

Fixed modalDismissed event being emitted with wrong id

Event was emitted with parent id instead of the id provided by the user, which could be an id on one of the modals children.

fixes #4693
Guy Carmeli 5 years ago
parent
commit
aef7745cd3

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/modal/ModalStack.java View File

@@ -62,8 +62,8 @@ public class ModalStack {
62 62
             CommandListenerAdapter onDismiss = new CommandListenerAdapter(listener) {
63 63
                 @Override
64 64
                 public void onSuccess(String childId) {
65
-                    eventEmitter.emitModalDismissed(toDismiss.getId(), 1);
66
-                    super.onSuccess(childId);
65
+                    eventEmitter.emitModalDismissed(componentId, 1);
66
+                    super.onSuccess(componentId);
67 67
                 }
68 68
             };
69 69
             if (isDismissingTopModal) {

+ 5
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerBuilder.java View File

@@ -11,8 +11,7 @@ import com.reactnativenavigation.viewcontrollers.ViewController;
11 11
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
12 12
 import com.reactnativenavigation.views.element.ElementTransitionManager;
13 13
 
14
-import java.util.ArrayList;
15
-import java.util.List;
14
+import java.util.*;
16 15
 
17 16
 public class StackControllerBuilder {
18 17
     private Activity activity;
@@ -32,6 +31,10 @@ public class StackControllerBuilder {
32 31
         animator = new NavigationAnimator(activity, new ElementTransitionManager());
33 32
     }
34 33
 
34
+    public StackControllerBuilder setChildren(ViewController... children) {
35
+        return setChildren(Arrays.asList(children));
36
+    }
37
+
35 38
     public StackControllerBuilder setChildren(List<ViewController> children) {
36 39
         this.children = children;
37 40
         return this;

+ 21
- 2
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/modal/ModalStackTest.java View File

@@ -4,7 +4,7 @@ import android.app.Activity;
4 4
 import android.view.ViewGroup;
5 5
 import android.widget.FrameLayout;
6 6
 
7
-import com.reactnativenavigation.BaseTest;
7
+import com.reactnativenavigation.*;
8 8
 import com.reactnativenavigation.anim.ModalAnimator;
9 9
 import com.reactnativenavigation.mocks.SimpleViewController;
10 10
 import com.reactnativenavigation.parse.Options;
@@ -13,6 +13,7 @@ import com.reactnativenavigation.utils.CommandListener;
13 13
 import com.reactnativenavigation.utils.CommandListenerAdapter;
14 14
 import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
15 15
 import com.reactnativenavigation.viewcontrollers.ViewController;
16
+import com.reactnativenavigation.viewcontrollers.stack.*;
16 17
 
17 18
 import org.junit.Test;
18 19
 import org.mockito.Mockito;
@@ -34,16 +35,20 @@ public class ModalStackTest extends BaseTest {
34 35
     private static final String MODAL_ID_1 = "modalId1";
35 36
     private static final String MODAL_ID_2 = "modalId2";
36 37
     private static final String MODAL_ID_3 = "modalId3";
38
+    private static final String MODAL_ID_4 = "modalId4";
37 39
 
38 40
     private ModalStack uut;
39 41
     private ViewController modal1;
40 42
     private ViewController modal2;
41 43
     private ViewController modal3;
44
+    private ViewController modal4;
45
+    private StackController stack;
42 46
     private Activity activity;
43 47
     private ChildControllersRegistry childRegistry;
44 48
     private ModalPresenter presenter;
45 49
     private ModalAnimator animator;
46 50
     private ViewController root;
51
+    private EventEmitter emitter;
47 52
 
48 53
     @Override
49 54
     public void beforeEach() {
@@ -63,10 +68,15 @@ public class ModalStackTest extends BaseTest {
63 68
         uut = new ModalStack(presenter);
64 69
         uut.setModalsLayout(modalsLayout);
65 70
         uut.setRootLayout(rootLayout);
66
-        uut.setEventEmitter(Mockito.mock(EventEmitter.class));
71
+        emitter = Mockito.mock(EventEmitter.class);
72
+        uut.setEventEmitter(emitter);
67 73
         modal1 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_1, new Options()));
68 74
         modal2 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_2, new Options()));
69 75
         modal3 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_3, new Options()));
76
+        modal4 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_4, new Options()));
77
+        stack = TestUtils.newStackController(activity)
78
+                .setChildren(modal4)
79
+                .build();
70 80
     }
71 81
 
72 82
     @Test
@@ -99,6 +109,15 @@ public class ModalStackTest extends BaseTest {
99 109
         verify(listener).onSuccess(modal1.getId());
100 110
     }
101 111
 
112
+    @Test
113
+    public void dismissModal_listenerAndEmitterAreInvokedWithGivenId() {
114
+        uut.showModal(stack, root, new CommandListenerAdapter());
115
+        CommandListener listener = spy(new CommandListenerAdapter());
116
+        uut.dismissModal(modal4.getId(), root, listener);
117
+        verify(listener).onSuccess(modal4.getId());
118
+        verify(emitter).emitModalDismissed(modal4.getId(), 1);
119
+    }
120
+
102 121
     @SuppressWarnings("Convert2Lambda")
103 122
     @Test
104 123
     public void dismissModal_rejectIfModalNotFound() {