Explorar el Código

js:prepare dismissModal api

Daniel Zlotin hace 7 años
padre
commit
a70a8eb089

+ 5
- 0
playground/src/containers/SimpleScreen.js Ver fichero

@@ -8,6 +8,7 @@ class SimpleScreen extends Component {
8 8
     super(props);
9 9
     this.onClickPop = this.onClickPop.bind(this);
10 10
     this.onClickPush = this.onClickPush.bind(this);
11
+    this.onClickDismissModal = this.onClickDismissModal.bind(this);
11 12
   }
12 13
 
13 14
   render() {
@@ -18,6 +19,7 @@ class SimpleScreen extends Component {
18 19
         {this.renderTextFromFunctionInProps()}
19 20
         <Button title="Push" onPress={this.onClickPush} />
20 21
         <Button title="Pop" onPress={this.onClickPop} />
22
+        <Button title="Dismiss Modal" onPress={this.onClickDismissModal} />
21 23
       </View>
22 24
     );
23 25
   }
@@ -44,6 +46,9 @@ class SimpleScreen extends Component {
44 46
     });
45 47
   }
46 48
 
49
+  onClickDismissModal() {
50
+    Navigation.dismissModal(this.props.id);
51
+  }
47 52
 }
48 53
 export default SimpleScreen;
49 54
 

+ 4
- 0
src/Navigation.js Ver fichero

@@ -38,6 +38,10 @@ class Navigation {
38 38
     return this.appCommands.showModal(params);
39 39
   }
40 40
 
41
+  dismissModal(id) {
42
+    return this.appCommands.dismissModal(id);
43
+  }
44
+
41 45
   events() {
42 46
     return this.publicEventsRegistry;
43 47
   }

+ 9
- 0
src/Navigation.test.js Ver fichero

@@ -39,6 +39,15 @@ describe('Navigation', () => {
39 39
     expect(Navigation.appCommands.showModal).toHaveBeenCalledWith(params);
40 40
   });
41 41
 
42
+  it('dismissModal delegates to AppCommands', async () => {
43
+    Navigation.appCommands.dismissModal.mockReturnValue(Promise.resolve('result'));
44
+    const params = {};
45
+    const result = await Navigation.dismissModal(params);
46
+    expect(result).toEqual('result');
47
+    expect(Navigation.appCommands.dismissModal).toHaveBeenCalledTimes(1);
48
+    expect(Navigation.appCommands.dismissModal).toHaveBeenCalledWith(params);
49
+  });
50
+
42 51
   it('events return public events', () => {
43 52
     const cb = jest.fn();
44 53
     Navigation.events().onAppLaunched(cb);

+ 5
- 0
src/adapters/NativeCommandsSender.js Ver fichero

@@ -24,5 +24,10 @@ export default class NativeCommandsSender {
24 24
     this.nativeCommandsModule.showModal(layout);
25 25
     return Promise.resolve(layout);
26 26
   }
27
+
28
+  dismissModal(id) {
29
+    this.nativeCommandsModule.dismissModal(id);
30
+    return Promise.resolve(id);
31
+  }
27 32
 }
28 33
 

+ 8
- 1
src/adapters/NativeCommandsSender.test.js Ver fichero

@@ -9,7 +9,8 @@ describe('NativeCommandsSender', () => {
9 9
       setRoot: jest.fn(),
10 10
       push: jest.fn(),
11 11
       pop: jest.fn(),
12
-      showModal: jest.fn()
12
+      showModal: jest.fn(),
13
+      dismissModal: jest.fn()
13 14
     };
14 15
     NativeModules.RNNBridgeModule = mockNativeModule;
15 16
     uut = new NativeCommandsSender();
@@ -43,4 +44,10 @@ describe('NativeCommandsSender', () => {
43 44
     expect(mockNativeModule.showModal).toHaveBeenCalledTimes(1);
44 45
     expect(result).toBeDefined();
45 46
   });
47
+
48
+  it('dismissModal', async () => {
49
+    const result = await uut.dismissModal('id');
50
+    expect(mockNativeModule.dismissModal).toHaveBeenCalledTimes(1);
51
+    expect(result).toEqual('id');
52
+  });
46 53
 });

+ 4
- 0
src/commands/AppCommands.js Ver fichero

@@ -20,5 +20,9 @@ export default class AppCommands {
20 20
     this.layoutTreeCrawler.crawl(layout);
21 21
     return this.nativeCommandsSender.showModal(layout);
22 22
   }
23
+
24
+  dismissModal(id) {
25
+    return this.nativeCommandsSender.dismissModal(id);
26
+  }
23 27
 }
24 28
 

+ 14
- 0
src/commands/AppCommands.test.js Ver fichero

@@ -100,4 +100,18 @@ describe('AppCommands', () => {
100 100
       expect(result).toEqual('the resolved layout');
101 101
     });
102 102
   });
103
+
104
+  describe('dismissModal', () => {
105
+    it('sends command to native', () => {
106
+      uut.dismissModal('myUniqueId');
107
+      expect(mockCommandsSender.dismissModal).toHaveBeenCalledTimes(1);
108
+      expect(mockCommandsSender.dismissModal).toHaveBeenCalledWith('myUniqueId');
109
+    });
110
+
111
+    it('returns a promise with the id', async () => {
112
+      mockCommandsSender.dismissModal.mockReturnValue(Promise.resolve('the id'));
113
+      const result = await uut.dismissModal('myUniqueId');
114
+      expect(result).toEqual('the id');
115
+    });
116
+  });
103 117
 });