Explorar el Código

passProps works

Daniel Zlotin hace 8 años
padre
commit
7c4a9c7dbe

+ 2
- 2
playground/e2e/app.test.js Ver fichero

@@ -9,12 +9,12 @@ describe('app', () => {
9 9
 
10 10
   it('switch to tab based app', () => {
11 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 15
   xit('switch to tabs with side menus', () => {
16 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 Ver fichero

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

+ 14
- 14
playground/src/containers/WelcomeScreen.js Ver fichero

@@ -12,14 +12,8 @@ class WelcomeScreen extends Component {
12 12
     return (
13 13
       <View style={styles.root}>
14 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 17
       </View>
24 18
     );
25 19
   }
@@ -29,12 +23,18 @@ class WelcomeScreen extends Component {
29 23
       tabs: [
30 24
         {
31 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 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,7 +46,7 @@ class WelcomeScreen extends Component {
46 46
       tabs: [
47 47
         {
48 48
           container: {
49
-            name: 'com.example.SimpleTabScreen'
49
+            name: 'com.example.SimpleScreen'
50 50
           }
51 51
         },
52 52
         {
@@ -56,16 +56,16 @@ class WelcomeScreen extends Component {
56 56
         },
57 57
         {
58 58
           container: {
59
-            name: 'com.example.SimpleTabScreen'
59
+            name: 'com.example.SimpleScreen'
60 60
           }
61 61
         }
62 62
       ],
63 63
       sideMenu: {
64 64
         left: {
65
-          name: 'com.example.SimpleTabScreen'
65
+          name: 'com.example.SimpleScreen'
66 66
         },
67 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 Ver fichero

@@ -1,9 +1,9 @@
1 1
 import Navigation from 'react-native-navigation';
2 2
 
3 3
 import WelcomeScreen from './WelcomeScreen';
4
-import SimpleTabScreen from './SimpleTabScreen';
4
+import SimpleScreen from './SimpleScreen';
5 5
 
6 6
 export function registerContainers() {
7 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 Ver fichero

@@ -12,7 +12,7 @@ class Navigation {
12 12
     this.nativeEventsReceiver = new NativeEventsReceiver();
13 13
     this.uniqueIdProvider = new UniqueIdProvider();
14 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 18
   registerContainer(containerName, getContainerFunc) {

+ 13
- 2
src/commands/Commands.js Ver fichero

@@ -1,12 +1,23 @@
1
+import * as _ from 'lodash';
1 2
 import LayoutTreeParser from './LayoutTreeParser';
2 3
 
3 4
 export default class Commands {
4
-  constructor(nativeCommandsSender, uniqueIdProvider) {
5
+  constructor(nativeCommandsSender, uniqueIdProvider, store) {
5 6
     this.nativeCommandsSender = nativeCommandsSender;
6 7
     this.layoutTreeParser = new LayoutTreeParser(uniqueIdProvider);
8
+    this.store = store;
7 9
   }
8 10
 
9 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 Ver fichero

@@ -1,15 +1,19 @@
1
+import * as SimpleLayouts from './SimpleLayouts';
2
+
1 3
 describe('Commands', () => {
2 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 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 15
     const Commands = require('./Commands').default;
12
-    uut = new Commands(mockCommandsSender, mockIdProvider);
16
+    uut = new Commands(mockCommandsSender, mockIdProvider, store);
13 17
   });
14 18
 
15 19
   describe('setRoot', () => {
@@ -22,11 +26,12 @@ describe('Commands', () => {
22 26
       expect(mockCommandsSender.setRoot).toHaveBeenCalledTimes(1);
23 27
       expect(mockCommandsSender.setRoot).toHaveBeenCalledWith({
24 28
         type: 'ContainerStack',
25
-        id: 'ContainerStackUNIQUE_ID',
29
+        id: 'ContainerStack+UNIQUE_ID',
30
+        data: {},
26 31
         children: [
27 32
           {
28 33
             type: 'Container',
29
-            id: 'ContainerUNIQUE_ID',
34
+            id: 'Container+UNIQUE_ID',
30 35
             children: [],
31 36
             data: {
32 37
               name: 'com.example.MyScreen'
@@ -35,5 +40,11 @@ describe('Commands', () => {
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 Ver fichero

@@ -29,6 +29,7 @@ export default class LayoutTreeParser {
29 29
   _node(node) {
30 30
     node.id = this.uniqueIdProvider.generate(node.type);
31 31
     node.children = node.children || [];
32
+    node.data = node.data || {};
32 33
     return node;
33 34
   }
34 35
 

+ 33
- 10
src/commands/LayoutTreeParser.test.js Ver fichero

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

+ 7
- 8
src/commands/SimpleLayouts.js Ver fichero

@@ -4,18 +4,17 @@ export const singleScreenApp = {
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 14
 export const singleScreenWithAditionalParams = {
10 15
   container: {
11 16
     name: 'com.example.MyScreen',
12
-    passProps: {
13
-      foo: {
14
-        number: 1,
15
-        string: 'Hello!'
16
-      },
17
-      bar: passedFunction
18
-    },
17
+    passProps: passProps,
19 18
     style: {},
20 19
     buttons: {}
21 20
   }