Browse Source

passProps works

Daniel Zlotin 8 years ago
parent
commit
7c4a9c7dbe

+ 2
- 2
playground/e2e/app.test.js View File

9
 
9
 
10
   it('switch to tab based app', () => {
10
   it('switch to tab based app', () => {
11
     elementByLabel('Switch to tab based app').tap();
11
     elementByLabel('Switch to tab based app').tap();
12
-    expect(elementByLabel('This is a tab screen')).toBeVisible();
12
+    expect(elementByLabel('This is tab 1')).toBeVisible();
13
   });
13
   });
14
 
14
 
15
   xit('switch to tabs with side menus', () => {
15
   xit('switch to tabs with side menus', () => {
16
     elementByLabel('Switch to tab based app with side menus').tap();
16
     elementByLabel('Switch to tab based app with side menus').tap();
17
-    expect(elementByLabel('This is a tab screen')).toBeVisible();
17
+    // expect(elementByLabel('This is a tab screen')).toBeVisible();
18
   });
18
   });
19
 });
19
 });
20
 
20
 

playground/src/containers/SimpleTabScreen.js → playground/src/containers/SimpleScreen.js View File

1
 import React, { Component } from 'react';
1
 import React, { Component } from 'react';
2
 import { View, Text } from 'react-native';
2
 import { View, Text } from 'react-native';
3
 
3
 
4
-class SimpleTabScreen extends Component {
4
+class SimpleScreen extends Component {
5
   render() {
5
   render() {
6
+    const text = this.props.text || 'Simple Screen';
6
     return (
7
     return (
7
       <View style={styles.root}>
8
       <View style={styles.root}>
8
-        <Text style={styles.h1}>{`This is a tab screen`}</Text>
9
+        <Text style={styles.h1}>{text}</Text>
9
       </View>
10
       </View>
10
     );
11
     );
11
   }
12
   }
12
 }
13
 }
13
-export default SimpleTabScreen;
14
+export default SimpleScreen;
14
 
15
 
15
 const styles = {
16
 const styles = {
16
   root: {
17
   root: {

+ 14
- 14
playground/src/containers/WelcomeScreen.js View File

12
     return (
12
     return (
13
       <View style={styles.root}>
13
       <View style={styles.root}>
14
         <Text style={styles.h1}>{`React Native Navigation!`}</Text>
14
         <Text style={styles.h1}>{`React Native Navigation!`}</Text>
15
-        <Button
16
-          title="Switch to tab based app"
17
-          onPress={this.onClickSwitchToTabs}
18
-        />
19
-        <Button
20
-          title="Switch to tab based app with side menus"
21
-          onPress={this.onClickSwitchToTabsWithSideMenus}
22
-        />
15
+        <Button title="Switch to tab based app" onPress={this.onClickSwitchToTabs} />
16
+        <Button title="Switch to tab based app with side menus" onPress={this.onClickSwitchToTabsWithSideMenus} />
23
       </View>
17
       </View>
24
     );
18
     );
25
   }
19
   }
29
       tabs: [
23
       tabs: [
30
         {
24
         {
31
           container: {
25
           container: {
32
-            name: 'com.example.SimpleTabScreen'
26
+            name: 'com.example.SimpleScreen',
27
+            passProps: {
28
+              text: 'This is tab 1'
29
+            }
33
           }
30
           }
34
         },
31
         },
35
         {
32
         {
36
           container: {
33
           container: {
37
-            name: 'com.example.WelcomeScreen'
34
+            name: 'com.example.SimpleScreen',
35
+            passProps: {
36
+              text: 'This is tab 2'
37
+            }
38
           }
38
           }
39
         }
39
         }
40
       ]
40
       ]
46
       tabs: [
46
       tabs: [
47
         {
47
         {
48
           container: {
48
           container: {
49
-            name: 'com.example.SimpleTabScreen'
49
+            name: 'com.example.SimpleScreen'
50
           }
50
           }
51
         },
51
         },
52
         {
52
         {
56
         },
56
         },
57
         {
57
         {
58
           container: {
58
           container: {
59
-            name: 'com.example.SimpleTabScreen'
59
+            name: 'com.example.SimpleScreen'
60
           }
60
           }
61
         }
61
         }
62
       ],
62
       ],
63
       sideMenu: {
63
       sideMenu: {
64
         left: {
64
         left: {
65
-          name: 'com.example.SimpleTabScreen'
65
+          name: 'com.example.SimpleScreen'
66
         },
66
         },
67
         right: {
67
         right: {
68
-          name: 'com.example.SimpleTabScreen'
68
+          name: 'com.example.SimpleScreen'
69
         }
69
         }
70
       }
70
       }
71
     });
71
     });

+ 2
- 2
playground/src/containers/index.js View File

1
 import Navigation from 'react-native-navigation';
1
 import Navigation from 'react-native-navigation';
2
 
2
 
3
 import WelcomeScreen from './WelcomeScreen';
3
 import WelcomeScreen from './WelcomeScreen';
4
-import SimpleTabScreen from './SimpleTabScreen';
4
+import SimpleScreen from './SimpleScreen';
5
 
5
 
6
 export function registerContainers() {
6
 export function registerContainers() {
7
   Navigation.registerContainer(`com.example.WelcomeScreen`, () => WelcomeScreen);
7
   Navigation.registerContainer(`com.example.WelcomeScreen`, () => WelcomeScreen);
8
-  Navigation.registerContainer(`com.example.SimpleTabScreen`, () => SimpleTabScreen);
8
+  Navigation.registerContainer(`com.example.SimpleScreen`, () => SimpleScreen);
9
 }
9
 }

+ 1
- 1
src/Navigation.js View File

12
     this.nativeEventsReceiver = new NativeEventsReceiver();
12
     this.nativeEventsReceiver = new NativeEventsReceiver();
13
     this.uniqueIdProvider = new UniqueIdProvider();
13
     this.uniqueIdProvider = new UniqueIdProvider();
14
     this.containerRegistry = new ContainerRegistry(this.store);
14
     this.containerRegistry = new ContainerRegistry(this.store);
15
-    this.commands = new Commands(new NativeCommandsSender(), this.uniqueIdProvider);
15
+    this.commands = new Commands(new NativeCommandsSender(), this.uniqueIdProvider, this.store);
16
   }
16
   }
17
 
17
 
18
   registerContainer(containerName, getContainerFunc) {
18
   registerContainer(containerName, getContainerFunc) {

+ 13
- 2
src/commands/Commands.js View File

1
+import * as _ from 'lodash';
1
 import LayoutTreeParser from './LayoutTreeParser';
2
 import LayoutTreeParser from './LayoutTreeParser';
2
 
3
 
3
 export default class Commands {
4
 export default class Commands {
4
-  constructor(nativeCommandsSender, uniqueIdProvider) {
5
+  constructor(nativeCommandsSender, uniqueIdProvider, store) {
5
     this.nativeCommandsSender = nativeCommandsSender;
6
     this.nativeCommandsSender = nativeCommandsSender;
6
     this.layoutTreeParser = new LayoutTreeParser(uniqueIdProvider);
7
     this.layoutTreeParser = new LayoutTreeParser(uniqueIdProvider);
8
+    this.store = store;
7
   }
9
   }
8
 
10
 
9
   setRoot(simpleApi) {
11
   setRoot(simpleApi) {
10
-    this.nativeCommandsSender.setRoot(this.layoutTreeParser.parseFromSimpleJSON(simpleApi));
12
+    const layout = this.layoutTreeParser.parseFromSimpleJSON(simpleApi);
13
+    this._savePassProps(layout);
14
+    this.nativeCommandsSender.setRoot(layout);
15
+  }
16
+
17
+  _savePassProps(node) {
18
+    if (node.type === 'Container') {
19
+      this.store.setPropsForContainerId(node.id, node.data.passProps);
20
+    }
21
+    _.forEach(node.children, (child) => this._savePassProps(child));
11
   }
22
   }
12
 }
23
 }

+ 20
- 9
src/commands/Commands.test.js View File

1
+import * as SimpleLayouts from './SimpleLayouts';
2
+
1
 describe('Commands', () => {
3
 describe('Commands', () => {
2
   let uut;
4
   let uut;
3
-  const mockCommandsSender = {
4
-    setRoot: jest.fn()
5
-  };
6
-  const mockIdProvider = {
7
-    generate: (prefix) => `${prefix}UNIQUE_ID`
8
-  };
5
+  let mockCommandsSender;
6
+  let store;
9
 
7
 
10
   beforeEach(() => {
8
   beforeEach(() => {
9
+    const NativeCommandsSender = jest.genMockFromModule('../adapters/NativeCommandsSender').default;
10
+    mockCommandsSender = new NativeCommandsSender();
11
+    const mockIdProvider = { generate: (prefix) => `${prefix}+UNIQUE_ID` };
12
+    const Store = require('../containers/Store').default;
13
+    store = new Store();
14
+
11
     const Commands = require('./Commands').default;
15
     const Commands = require('./Commands').default;
12
-    uut = new Commands(mockCommandsSender, mockIdProvider);
16
+    uut = new Commands(mockCommandsSender, mockIdProvider, store);
13
   });
17
   });
14
 
18
 
15
   describe('setRoot', () => {
19
   describe('setRoot', () => {
22
       expect(mockCommandsSender.setRoot).toHaveBeenCalledTimes(1);
26
       expect(mockCommandsSender.setRoot).toHaveBeenCalledTimes(1);
23
       expect(mockCommandsSender.setRoot).toHaveBeenCalledWith({
27
       expect(mockCommandsSender.setRoot).toHaveBeenCalledWith({
24
         type: 'ContainerStack',
28
         type: 'ContainerStack',
25
-        id: 'ContainerStackUNIQUE_ID',
29
+        id: 'ContainerStack+UNIQUE_ID',
30
+        data: {},
26
         children: [
31
         children: [
27
           {
32
           {
28
             type: 'Container',
33
             type: 'Container',
29
-            id: 'ContainerUNIQUE_ID',
34
+            id: 'Container+UNIQUE_ID',
30
             children: [],
35
             children: [],
31
             data: {
36
             data: {
32
               name: 'com.example.MyScreen'
37
               name: 'com.example.MyScreen'
35
         ]
40
         ]
36
       });
41
       });
37
     });
42
     });
43
+
44
+    it('passProps into containers', () => {
45
+      expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual({});
46
+      uut.setRoot(SimpleLayouts.singleScreenWithAditionalParams);
47
+      expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual(SimpleLayouts.passProps);
48
+    });
38
   });
49
   });
39
 });
50
 });

+ 1
- 0
src/commands/LayoutTreeParser.js View File

29
   _node(node) {
29
   _node(node) {
30
     node.id = this.uniqueIdProvider.generate(node.type);
30
     node.id = this.uniqueIdProvider.generate(node.type);
31
     node.children = node.children || [];
31
     node.children = node.children || [];
32
+    node.data = node.data || {};
32
     return node;
33
     return node;
33
   }
34
   }
34
 
35
 

+ 33
- 10
src/commands/LayoutTreeParser.test.js View File

22
             data: {},
22
             data: {},
23
             children: []
23
             children: []
24
           }
24
           }
25
-        ]
25
+        ],
26
+        data: {}
26
       });
27
       });
27
   });
28
   });
28
 
29
 
37
       .toEqual({
38
       .toEqual({
38
         type: 'ContainerStack',
39
         type: 'ContainerStack',
39
         id: 'ContainerStack+UNIQUE_ID',
40
         id: 'ContainerStack+UNIQUE_ID',
41
+        data: {},
40
         children: [
42
         children: [
41
           {
43
           {
42
             type: 'Container',
44
             type: 'Container',
55
       .toEqual({
57
       .toEqual({
56
         type: 'ContainerStack',
58
         type: 'ContainerStack',
57
         id: 'ContainerStack+UNIQUE_ID',
59
         id: 'ContainerStack+UNIQUE_ID',
60
+        data: {},
58
         children: [
61
         children: [
59
           {
62
           {
60
             type: 'Container',
63
             type: 'Container',
62
             children: [],
65
             children: [],
63
             data: {
66
             data: {
64
               name: 'com.example.MyScreen',
67
               name: 'com.example.MyScreen',
65
-              passProps: {
66
-                foo: {
67
-                  number: 1,
68
-                  string: 'Hello!'
69
-                },
70
-                bar: SimpleLayouts.passedFunction
71
-              },
68
+              passProps: SimpleLayouts.passProps,
72
               style: {},
69
               style: {},
73
               buttons: {}
70
               buttons: {}
74
             }
71
             }
75
           }
72
           }
76
         ]
73
         ]
77
       });
74
       });
78
-    expect(uut.parseFromSimpleJSON(SimpleLayouts.singleScreenWithAditionalParams).children[0].data.passProps.bar).toBe(SimpleLayouts.passedFunction);
79
-    expect(uut.parseFromSimpleJSON(SimpleLayouts.singleScreenWithAditionalParams).children[0].data.passProps.bar()).toEqual('Hello from a function');
75
+    const parsedPropsFn = uut.parseFromSimpleJSON(SimpleLayouts.singleScreenWithAditionalParams)
76
+      .children[0].data.passProps.fnProp;
77
+    expect(parsedPropsFn).toBe(SimpleLayouts.passProps.fnProp);
78
+    expect(parsedPropsFn()).toEqual('Hello from a function');
80
   });
79
   });
81
 
80
 
82
   it('parses tab based', () => {
81
   it('parses tab based', () => {
84
       .toEqual({
83
       .toEqual({
85
         type: 'Tabs',
84
         type: 'Tabs',
86
         id: 'Tabs+UNIQUE_ID',
85
         id: 'Tabs+UNIQUE_ID',
86
+        data: {},
87
         children: [
87
         children: [
88
           {
88
           {
89
             type: 'ContainerStack',
89
             type: 'ContainerStack',
90
             id: 'ContainerStack+UNIQUE_ID',
90
             id: 'ContainerStack+UNIQUE_ID',
91
+            data: {},
91
             children: [
92
             children: [
92
               {
93
               {
93
                 type: 'Container',
94
                 type: 'Container',
102
           {
103
           {
103
             type: 'ContainerStack',
104
             type: 'ContainerStack',
104
             id: 'ContainerStack+UNIQUE_ID',
105
             id: 'ContainerStack+UNIQUE_ID',
106
+            data: {},
105
             children: [
107
             children: [
106
               {
108
               {
107
                 type: 'Container',
109
                 type: 'Container',
116
           {
118
           {
117
             type: 'ContainerStack',
119
             type: 'ContainerStack',
118
             id: 'ContainerStack+UNIQUE_ID',
120
             id: 'ContainerStack+UNIQUE_ID',
121
+            data: {},
119
             children: [
122
             children: [
120
               {
123
               {
121
                 type: 'Container',
124
                 type: 'Container',
136
       .toEqual({
139
       .toEqual({
137
         type: 'SideMenuRoot',
140
         type: 'SideMenuRoot',
138
         id: 'SideMenuRoot+UNIQUE_ID',
141
         id: 'SideMenuRoot+UNIQUE_ID',
142
+        data: {},
139
         children: [
143
         children: [
140
           {
144
           {
141
             type: 'SideMenuLeft',
145
             type: 'SideMenuLeft',
142
             id: 'SideMenuLeft+UNIQUE_ID',
146
             id: 'SideMenuLeft+UNIQUE_ID',
147
+            data: {},
143
             children: [
148
             children: [
144
               {
149
               {
145
                 type: 'Container',
150
                 type: 'Container',
154
           {
159
           {
155
             type: 'SideMenuCenter',
160
             type: 'SideMenuCenter',
156
             id: 'SideMenuCenter+UNIQUE_ID',
161
             id: 'SideMenuCenter+UNIQUE_ID',
162
+            data: {},
157
             children: [
163
             children: [
158
               {
164
               {
159
                 type: 'ContainerStack',
165
                 type: 'ContainerStack',
160
                 id: 'ContainerStack+UNIQUE_ID',
166
                 id: 'ContainerStack+UNIQUE_ID',
167
+                data: {},
161
                 children: [
168
                 children: [
162
                   {
169
                   {
163
                     type: 'Container',
170
                     type: 'Container',
180
       .toEqual({
187
       .toEqual({
181
         type: 'SideMenuRoot',
188
         type: 'SideMenuRoot',
182
         id: 'SideMenuRoot+UNIQUE_ID',
189
         id: 'SideMenuRoot+UNIQUE_ID',
190
+        data: {},
183
         children: [
191
         children: [
184
           {
192
           {
185
             type: 'SideMenuCenter',
193
             type: 'SideMenuCenter',
186
             id: 'SideMenuCenter+UNIQUE_ID',
194
             id: 'SideMenuCenter+UNIQUE_ID',
195
+            data: {},
187
             children: [
196
             children: [
188
               {
197
               {
189
                 type: 'ContainerStack',
198
                 type: 'ContainerStack',
190
                 id: 'ContainerStack+UNIQUE_ID',
199
                 id: 'ContainerStack+UNIQUE_ID',
200
+                data: {},
191
                 children: [
201
                 children: [
192
                   {
202
                   {
193
                     type: 'Container',
203
                     type: 'Container',
204
           {
214
           {
205
             type: 'SideMenuRight',
215
             type: 'SideMenuRight',
206
             id: 'SideMenuRight+UNIQUE_ID',
216
             id: 'SideMenuRight+UNIQUE_ID',
217
+            data: {},
207
             children: [
218
             children: [
208
               {
219
               {
209
                 type: 'Container',
220
                 type: 'Container',
224
       .toEqual({
235
       .toEqual({
225
         type: 'SideMenuRoot',
236
         type: 'SideMenuRoot',
226
         id: 'SideMenuRoot+UNIQUE_ID',
237
         id: 'SideMenuRoot+UNIQUE_ID',
238
+        data: {},
227
         children: [
239
         children: [
228
           {
240
           {
229
             type: 'SideMenuLeft',
241
             type: 'SideMenuLeft',
230
             id: 'SideMenuLeft+UNIQUE_ID',
242
             id: 'SideMenuLeft+UNIQUE_ID',
243
+            data: {},
231
             children: [
244
             children: [
232
               {
245
               {
233
                 type: 'Container',
246
                 type: 'Container',
242
           {
255
           {
243
             type: 'SideMenuCenter',
256
             type: 'SideMenuCenter',
244
             id: 'SideMenuCenter+UNIQUE_ID',
257
             id: 'SideMenuCenter+UNIQUE_ID',
258
+            data: {},
245
             children: [
259
             children: [
246
               {
260
               {
247
                 type: 'ContainerStack',
261
                 type: 'ContainerStack',
248
                 id: 'ContainerStack+UNIQUE_ID',
262
                 id: 'ContainerStack+UNIQUE_ID',
263
+                data: {},
249
                 children: [
264
                 children: [
250
                   {
265
                   {
251
                     type: 'Container',
266
                     type: 'Container',
262
           {
277
           {
263
             type: 'SideMenuRight',
278
             type: 'SideMenuRight',
264
             id: 'SideMenuRight+UNIQUE_ID',
279
             id: 'SideMenuRight+UNIQUE_ID',
280
+            data: {},
265
             children: [
281
             children: [
266
               {
282
               {
267
                 type: 'Container',
283
                 type: 'Container',
282
       .toEqual({
298
       .toEqual({
283
         type: 'SideMenuRoot',
299
         type: 'SideMenuRoot',
284
         id: 'SideMenuRoot+UNIQUE_ID',
300
         id: 'SideMenuRoot+UNIQUE_ID',
301
+        data: {},
285
         children: [
302
         children: [
286
           {
303
           {
287
             type: 'SideMenuLeft',
304
             type: 'SideMenuLeft',
288
             id: 'SideMenuLeft+UNIQUE_ID',
305
             id: 'SideMenuLeft+UNIQUE_ID',
306
+            data: {},
289
             children: [
307
             children: [
290
               {
308
               {
291
                 type: 'Container',
309
                 type: 'Container',
300
           {
318
           {
301
             type: 'SideMenuCenter',
319
             type: 'SideMenuCenter',
302
             id: 'SideMenuCenter+UNIQUE_ID',
320
             id: 'SideMenuCenter+UNIQUE_ID',
321
+            data: {},
303
             children: [
322
             children: [
304
               {
323
               {
305
                 type: 'Tabs',
324
                 type: 'Tabs',
306
                 id: 'Tabs+UNIQUE_ID',
325
                 id: 'Tabs+UNIQUE_ID',
326
+                data: {},
307
                 children: [
327
                 children: [
308
                   {
328
                   {
309
                     type: 'ContainerStack',
329
                     type: 'ContainerStack',
310
                     id: 'ContainerStack+UNIQUE_ID',
330
                     id: 'ContainerStack+UNIQUE_ID',
331
+                    data: {},
311
                     children: [
332
                     children: [
312
                       {
333
                       {
313
                         type: 'Container',
334
                         type: 'Container',
322
                   {
343
                   {
323
                     type: 'ContainerStack',
344
                     type: 'ContainerStack',
324
                     id: 'ContainerStack+UNIQUE_ID',
345
                     id: 'ContainerStack+UNIQUE_ID',
346
+                    data: {},
325
                     children: [
347
                     children: [
326
                       {
348
                       {
327
                         type: 'Container',
349
                         type: 'Container',
340
           {
362
           {
341
             type: 'SideMenuRight',
363
             type: 'SideMenuRight',
342
             id: 'SideMenuRight+UNIQUE_ID',
364
             id: 'SideMenuRight+UNIQUE_ID',
365
+            data: {},
343
             children: [
366
             children: [
344
               {
367
               {
345
                 type: 'Container',
368
                 type: 'Container',

+ 7
- 8
src/commands/SimpleLayouts.js View File

4
   }
4
   }
5
 };
5
 };
6
 
6
 
7
-export const passedFunction = () => 'Hello from a function';
7
+export const passProps = {
8
+  strProp: 'string prop',
9
+  numProp: 12345,
10
+  objProp: { inner: { foo: 'bar' } },
11
+  fnProp: () => 'Hello from a function'
12
+};
8
 
13
 
9
 export const singleScreenWithAditionalParams = {
14
 export const singleScreenWithAditionalParams = {
10
   container: {
15
   container: {
11
     name: 'com.example.MyScreen',
16
     name: 'com.example.MyScreen',
12
-    passProps: {
13
-      foo: {
14
-        number: 1,
15
-        string: 'Hello!'
16
-      },
17
-      bar: passedFunction
18
-    },
17
+    passProps: passProps,
19
     style: {},
18
     style: {},
20
     buttons: {}
19
     buttons: {}
21
   }
20
   }