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 6 years ago
parent
commit
aef7745cd3

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

62
             CommandListenerAdapter onDismiss = new CommandListenerAdapter(listener) {
62
             CommandListenerAdapter onDismiss = new CommandListenerAdapter(listener) {
63
                 @Override
63
                 @Override
64
                 public void onSuccess(String childId) {
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
             if (isDismissingTopModal) {
69
             if (isDismissingTopModal) {

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

11
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
11
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
12
 import com.reactnativenavigation.views.element.ElementTransitionManager;
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
 public class StackControllerBuilder {
16
 public class StackControllerBuilder {
18
     private Activity activity;
17
     private Activity activity;
32
         animator = new NavigationAnimator(activity, new ElementTransitionManager());
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
     public StackControllerBuilder setChildren(List<ViewController> children) {
38
     public StackControllerBuilder setChildren(List<ViewController> children) {
36
         this.children = children;
39
         this.children = children;
37
         return this;
40
         return this;

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

4
 import android.view.ViewGroup;
4
 import android.view.ViewGroup;
5
 import android.widget.FrameLayout;
5
 import android.widget.FrameLayout;
6
 
6
 
7
-import com.reactnativenavigation.BaseTest;
7
+import com.reactnativenavigation.*;
8
 import com.reactnativenavigation.anim.ModalAnimator;
8
 import com.reactnativenavigation.anim.ModalAnimator;
9
 import com.reactnativenavigation.mocks.SimpleViewController;
9
 import com.reactnativenavigation.mocks.SimpleViewController;
10
 import com.reactnativenavigation.parse.Options;
10
 import com.reactnativenavigation.parse.Options;
13
 import com.reactnativenavigation.utils.CommandListenerAdapter;
13
 import com.reactnativenavigation.utils.CommandListenerAdapter;
14
 import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
14
 import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
15
 import com.reactnativenavigation.viewcontrollers.ViewController;
15
 import com.reactnativenavigation.viewcontrollers.ViewController;
16
+import com.reactnativenavigation.viewcontrollers.stack.*;
16
 
17
 
17
 import org.junit.Test;
18
 import org.junit.Test;
18
 import org.mockito.Mockito;
19
 import org.mockito.Mockito;
34
     private static final String MODAL_ID_1 = "modalId1";
35
     private static final String MODAL_ID_1 = "modalId1";
35
     private static final String MODAL_ID_2 = "modalId2";
36
     private static final String MODAL_ID_2 = "modalId2";
36
     private static final String MODAL_ID_3 = "modalId3";
37
     private static final String MODAL_ID_3 = "modalId3";
38
+    private static final String MODAL_ID_4 = "modalId4";
37
 
39
 
38
     private ModalStack uut;
40
     private ModalStack uut;
39
     private ViewController modal1;
41
     private ViewController modal1;
40
     private ViewController modal2;
42
     private ViewController modal2;
41
     private ViewController modal3;
43
     private ViewController modal3;
44
+    private ViewController modal4;
45
+    private StackController stack;
42
     private Activity activity;
46
     private Activity activity;
43
     private ChildControllersRegistry childRegistry;
47
     private ChildControllersRegistry childRegistry;
44
     private ModalPresenter presenter;
48
     private ModalPresenter presenter;
45
     private ModalAnimator animator;
49
     private ModalAnimator animator;
46
     private ViewController root;
50
     private ViewController root;
51
+    private EventEmitter emitter;
47
 
52
 
48
     @Override
53
     @Override
49
     public void beforeEach() {
54
     public void beforeEach() {
63
         uut = new ModalStack(presenter);
68
         uut = new ModalStack(presenter);
64
         uut.setModalsLayout(modalsLayout);
69
         uut.setModalsLayout(modalsLayout);
65
         uut.setRootLayout(rootLayout);
70
         uut.setRootLayout(rootLayout);
66
-        uut.setEventEmitter(Mockito.mock(EventEmitter.class));
71
+        emitter = Mockito.mock(EventEmitter.class);
72
+        uut.setEventEmitter(emitter);
67
         modal1 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_1, new Options()));
73
         modal1 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_1, new Options()));
68
         modal2 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_2, new Options()));
74
         modal2 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_2, new Options()));
69
         modal3 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_3, new Options()));
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
     @Test
82
     @Test
99
         verify(listener).onSuccess(modal1.getId());
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
     @SuppressWarnings("Convert2Lambda")
121
     @SuppressWarnings("Convert2Lambda")
103
     @Test
122
     @Test
104
     public void dismissModal_rejectIfModalNotFound() {
123
     public void dismissModal_rejectIfModalNotFound() {