Browse Source

JS:showModal

Daniel Zlotin 8 years ago
parent
commit
07a66e5db9

+ 5
- 0
playground/e2e/app.test.js View File

38
     elementByLabel('Pop').tap();
38
     elementByLabel('Pop').tap();
39
     expect(elementByLabel('React Native Navigation!')).toBeVisible();
39
     expect(elementByLabel('React Native Navigation!')).toBeVisible();
40
   });
40
   });
41
+
42
+  it('show modal', () => {
43
+    elementByLabel('Show Modal').tap();
44
+    expect(elementByLabel('Modal screen')).toBeVisible();
45
+  });
41
 });
46
 });
42
 
47
 
43
 function elementByLabel(label) {
48
 function elementByLabel(label) {

+ 10
- 0
playground/src/containers/WelcomeScreen.js View File

17
         <Button title="Switch to app with side menus" onPress={this.onClickSwitchToSideMenus} />
17
         <Button title="Switch to app with side menus" onPress={this.onClickSwitchToSideMenus} />
18
         <Button title="Switch to lifecycle screen" onPress={this.onClickLifecycleScreen} />
18
         <Button title="Switch to lifecycle screen" onPress={this.onClickLifecycleScreen} />
19
         <Button title="Push" onPress={this.onClickPush} />
19
         <Button title="Push" onPress={this.onClickPush} />
20
+        <Button title="Show Modal" onPress={this.onClickShowModal} />
20
         <Text style={styles.footer}>{`this.props.id = ${this.props.id}`}</Text>
21
         <Text style={styles.footer}>{`this.props.id = ${this.props.id}`}</Text>
21
       </View>
22
       </View>
22
     );
23
     );
102
       }
103
       }
103
     });
104
     });
104
   }
105
   }
106
+
107
+  onClickShowModal() {
108
+    Navigation.showModal({
109
+      name: 'navigation.playground.SimpleScreen',
110
+      passProps: {
111
+        text: 'Modal screen'
112
+      }
113
+    });
114
+  }
105
 }
115
 }
106
 
116
 
107
 export default WelcomeScreen;
117
 export default WelcomeScreen;

+ 4
- 0
src/Navigation.js View File

34
     return this.appCommands.setRoot(params);
34
     return this.appCommands.setRoot(params);
35
   }
35
   }
36
 
36
 
37
+  showModal(params) {
38
+    return this.appCommands.showModal(params);
39
+  }
40
+
37
   events() {
41
   events() {
38
     return this.publicEventsRegistry;
42
     return this.publicEventsRegistry;
39
   }
43
   }

+ 9
- 0
src/Navigation.test.js View File

30
     expect(Navigation.appCommands.setRoot).toHaveBeenCalledWith(params);
30
     expect(Navigation.appCommands.setRoot).toHaveBeenCalledWith(params);
31
   });
31
   });
32
 
32
 
33
+  it('showModal delegates to AppCommands', async () => {
34
+    Navigation.appCommands.showModal.mockReturnValue(Promise.resolve('result'));
35
+    const params = {};
36
+    const result = await Navigation.showModal(params);
37
+    expect(result).toEqual('result');
38
+    expect(Navigation.appCommands.showModal).toHaveBeenCalledTimes(1);
39
+    expect(Navigation.appCommands.showModal).toHaveBeenCalledWith(params);
40
+  });
41
+
33
   it('events return public events', () => {
42
   it('events return public events', () => {
34
     const cb = jest.fn();
43
     const cb = jest.fn();
35
     Navigation.events().onAppLaunched(cb);
44
     Navigation.events().onAppLaunched(cb);

+ 5
- 0
src/adapters/NativeCommandsSender.js View File

19
     this.nativeCommandsModule.pop(containerId);
19
     this.nativeCommandsModule.pop(containerId);
20
     return Promise.resolve(containerId);
20
     return Promise.resolve(containerId);
21
   }
21
   }
22
+
23
+  showModal(layout) {
24
+    this.nativeCommandsModule.showModal(layout);
25
+    return Promise.resolve(layout);
26
+  }
22
 }
27
 }
23
 
28
 

+ 8
- 1
src/adapters/NativeCommandsSender.test.js View File

8
     mockNativeModule = {
8
     mockNativeModule = {
9
       setRoot: jest.fn(),
9
       setRoot: jest.fn(),
10
       push: jest.fn(),
10
       push: jest.fn(),
11
-      pop: jest.fn()
11
+      pop: jest.fn(),
12
+      showModal: jest.fn()
12
     };
13
     };
13
     NativeModules.RNNBridgeModule = mockNativeModule;
14
     NativeModules.RNNBridgeModule = mockNativeModule;
14
     uut = new NativeCommandsSender();
15
     uut = new NativeCommandsSender();
36
     expect(mockNativeModule.pop).toHaveBeenCalledTimes(1);
37
     expect(mockNativeModule.pop).toHaveBeenCalledTimes(1);
37
     expect(result).toBeDefined();
38
     expect(result).toBeDefined();
38
   });
39
   });
40
+
41
+  it('showModal sends to native', async () => {
42
+    const result = await uut.showModal({});
43
+    expect(mockNativeModule.showModal).toHaveBeenCalledTimes(1);
44
+    expect(result).toBeDefined();
45
+  });
39
 });
46
 });

+ 7
- 0
src/commands/AppCommands.js View File

13
     this.layoutTreeCrawler.crawl(layout);
13
     this.layoutTreeCrawler.crawl(layout);
14
     return this.nativeCommandsSender.setRoot(layout);
14
     return this.nativeCommandsSender.setRoot(layout);
15
   }
15
   }
16
+
17
+  showModal(simpleApi) {
18
+    const input = _.cloneDeep(simpleApi);
19
+    const layout = this.layoutTreeParser.createContainer(input);
20
+    this.layoutTreeCrawler.crawl(layout);
21
+    return this.nativeCommandsSender.showModal(layout);
22
+  }
16
 }
23
 }
17
 
24
 

+ 39
- 0
src/commands/AppCommands.test.js View File

62
       expect(result).toEqual('the resolved layout');
62
       expect(result).toEqual('the resolved layout');
63
     });
63
     });
64
   });
64
   });
65
+
66
+
67
+  describe('showModal', () => {
68
+    it('sends command to native after parsing into a correct layout tree', () => {
69
+      uut.showModal({
70
+        name: 'com.example.MyScreen'
71
+      });
72
+      expect(mockCommandsSender.showModal).toHaveBeenCalledTimes(1);
73
+      expect(mockCommandsSender.showModal).toHaveBeenCalledWith({
74
+        type: 'Container',
75
+        id: 'Container+UNIQUE_ID',
76
+        children: [],
77
+        data: {
78
+          name: 'com.example.MyScreen'
79
+        }
80
+      });
81
+    });
82
+
83
+    it('deep clones input to avoid mutation errors', () => {
84
+      const obj = {};
85
+      uut.showModal({ inner: obj });
86
+      expect(mockCommandsSender.showModal.mock.calls[0][0].data.inner).not.toBe(obj);
87
+    });
88
+
89
+    it('passProps into containers', () => {
90
+      expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual({});
91
+      uut.showModal({
92
+        name: 'com.example.MyScreen',
93
+        passProps: SimpleLayouts.passProps
94
+      });
95
+      expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual(SimpleLayouts.passProps);
96
+    });
97
+
98
+    it('returns a promise with the resolved layout', async () => {
99
+      mockCommandsSender.showModal.mockReturnValue(Promise.resolve('the resolved layout'));
100
+      const result = await uut.showModal({ container: { name: 'com.example.MyScreen' } });
101
+      expect(result).toEqual('the resolved layout');
102
+    });
103
+  });
65
 });
104
 });