Browse Source

redesigning new API

Daniel Zlotin 7 years ago
parent
commit
102edbd67e

+ 0
- 108
lib/src/commands/Examples.js View File

1
-const passProps = {
2
-  strProp: 'string prop',
3
-  numProp: 12345,
4
-  objProp: { inner: { foo: 'bar' } },
5
-  fnProp: () => 'Hello from a function'
6
-};
7
-
8
-const topBarOptions = {
9
-  topBar: {
10
-    title: 'Hello1'
11
-  }
12
-};
13
-
14
-const singleComponent = {
15
-  component: {
16
-    name: 'MyReactComponent'
17
-  }
18
-};
19
-
20
-const stackOfComponentsWithTopBar = {
21
-  stack: [
22
-    {
23
-      component: {
24
-        name: 'MyReactComponent1'
25
-      }
26
-    },
27
-
28
-    {
29
-      component: {
30
-        name: 'MyReactComponent2',
31
-        options: topBarOptions // optional
32
-      }
33
-    }
34
-  ]
35
-};
36
-
37
-const bottomTabs = {
38
-  bottomTabs: [
39
-    stackOfComponentsWithTopBar,
40
-    stackOfComponentsWithTopBar,
41
-    {
42
-      component: {
43
-        name: 'MyReactComponent1'
44
-      }
45
-    }
46
-  ]
47
-};
48
-
49
-const sideMenu = {
50
-  sideMenu: {
51
-    left: singleComponent,
52
-    center: stackOfComponentsWithTopBar,
53
-    right: singleComponent
54
-  }
55
-};
56
-
57
-const topTabs = {
58
-  topTabs: [
59
-    singleComponent,
60
-    singleComponent,
61
-    singleComponent,
62
-    singleComponent,
63
-    stackOfComponentsWithTopBar
64
-  ]
65
-};
66
-
67
-const complexLayout = {
68
-  sideMenu: {
69
-    left: singleComponent,
70
-    center: {
71
-      bottomTabs: [
72
-        stackOfComponentsWithTopBar,
73
-        stackOfComponentsWithTopBar,
74
-        {
75
-          stack: [
76
-            {
77
-              topTabs: [
78
-                stackOfComponentsWithTopBar,
79
-                stackOfComponentsWithTopBar,
80
-                {
81
-                  topTabs: [
82
-                    singleComponent,
83
-                    singleComponent,
84
-                    singleComponent,
85
-                    singleComponent,
86
-                    stackOfComponentsWithTopBar
87
-                  ],
88
-                  options: topBarOptions
89
-                }
90
-              ]
91
-            }
92
-          ]
93
-        }
94
-      ]
95
-    }
96
-  }
97
-};
98
-
99
-module.exports = {
100
-  passProps,
101
-  topBarOptions,
102
-  singleComponent,
103
-  stackOfComponentsWithTopBar,
104
-  bottomTabs,
105
-  sideMenu,
106
-  topTabs,
107
-  complexLayout
108
-};

+ 125
- 0
lib/src/commands/LayoutExamples.js View File

1
+const passProps = {
2
+  strProp: 'string prop',
3
+  numProp: 12345,
4
+  objProp: { inner: { foo: 'bar' } },
5
+  fnProp: () => 'Hello from a function'
6
+};
7
+
8
+const options = {
9
+  topBar: {
10
+    title: 'Hello1'
11
+  }
12
+};
13
+
14
+const singleComponent = {
15
+  component: {
16
+    name: 'MyReactComponent',
17
+    options,
18
+    passProps
19
+  }
20
+};
21
+
22
+const stackWithTopBar = {
23
+  stack: {
24
+    children: [
25
+      {
26
+        component: {
27
+          name: 'MyReactComponent1'
28
+        }
29
+      },
30
+      {
31
+        component: {
32
+          name: 'MyReactComponent2',
33
+          options
34
+        }
35
+      }
36
+    ],
37
+    options
38
+  }
39
+};
40
+
41
+const bottomTabs = {
42
+  bottomTabs: {
43
+    children: [
44
+      stackWithTopBar,
45
+      stackWithTopBar,
46
+      {
47
+        component: {
48
+          name: 'MyReactComponent1'
49
+        }
50
+      }
51
+    ]
52
+  }
53
+};
54
+
55
+const sideMenu = {
56
+  sideMenu: {
57
+    left: singleComponent,
58
+    center: stackWithTopBar,
59
+    right: singleComponent
60
+  }
61
+};
62
+
63
+const topTabs = {
64
+  topTabs: {
65
+    children: [
66
+      singleComponent,
67
+      singleComponent,
68
+      singleComponent,
69
+      singleComponent,
70
+      stackWithTopBar
71
+    ],
72
+    options
73
+  }
74
+};
75
+
76
+const complexLayout = {
77
+  sideMenu: {
78
+    left: singleComponent,
79
+    center: {
80
+      bottomTabs: {
81
+        children: [
82
+          stackWithTopBar,
83
+          stackWithTopBar,
84
+          {
85
+            stack: {
86
+              children: [
87
+                {
88
+                  topTabs: {
89
+                    children: [
90
+                      stackWithTopBar,
91
+                      stackWithTopBar,
92
+                      {
93
+                        topTabs: {
94
+                          options,
95
+                          children: [
96
+                            singleComponent,
97
+                            singleComponent,
98
+                            singleComponent,
99
+                            singleComponent,
100
+                            stackWithTopBar
101
+                          ]
102
+                        }
103
+                      }
104
+                    ]
105
+                  }
106
+                }
107
+              ]
108
+            }
109
+          }
110
+        ]
111
+      }
112
+    }
113
+  }
114
+};
115
+
116
+module.exports = {
117
+  passProps,
118
+  options,
119
+  singleComponent,
120
+  stackWithTopBar,
121
+  bottomTabs,
122
+  sideMenu,
123
+  topTabs,
124
+  complexLayout
125
+};

+ 10
- 10
lib/src/commands/LayoutTreeParser.js View File

27
   _topTabs(api) {
27
   _topTabs(api) {
28
     return {
28
     return {
29
       type: LayoutTypes.TopTabs,
29
       type: LayoutTypes.TopTabs,
30
-      data: {},
31
-      children: _.map(api, this.parse)
30
+      data: { options: api.options },
31
+      children: _.map(api.children, this.parse)
32
     };
32
     };
33
   }
33
   }
34
 
34
 
35
   _sideMenu(api) {
35
   _sideMenu(api) {
36
     return {
36
     return {
37
       type: LayoutTypes.SideMenuRoot,
37
       type: LayoutTypes.SideMenuRoot,
38
-      data: {},
38
+      data: { options: api.options },
39
       children: this._sideMenuChildren(api)
39
       children: this._sideMenuChildren(api)
40
     };
40
     };
41
   }
41
   }
70
   _bottomTabs(api) {
70
   _bottomTabs(api) {
71
     return {
71
     return {
72
       type: LayoutTypes.BottomTabs,
72
       type: LayoutTypes.BottomTabs,
73
-      data: {},
74
-      children: _.map(api, this.parse)
73
+      data: { options: api.options },
74
+      children: _.map(api.children, this.parse)
75
     };
75
     };
76
   }
76
   }
77
 
77
 
78
   _stack(api) {
78
   _stack(api) {
79
     return {
79
     return {
80
-      type: LayoutTypes.ComponentStack,
81
-      data: {},
82
-      children: _.map(api, this.parse)
80
+      type: LayoutTypes.Stack,
81
+      data: { name: api.name, options: api.options },
82
+      children: _.map(api.children, this.parse)
83
     };
83
     };
84
   }
84
   }
85
 
85
 
86
   _component(api) {
86
   _component(api) {
87
-    return { 
87
+    return {
88
       type: LayoutTypes.Component,
88
       type: LayoutTypes.Component,
89
-      data: api,
89
+      data: { name: api.name, options: api.options, passProps: api.passProps },
90
       children: []
90
       children: []
91
     };
91
     };
92
   }
92
   }

+ 25
- 14
lib/src/commands/LayoutTreeParser.test.js View File

1
 const LayoutTreeParser = require('./LayoutTreeParser');
1
 const LayoutTreeParser = require('./LayoutTreeParser');
2
-const Examples = require('./Examples');
3
 const LayoutTypes = require('./LayoutTypes');
2
 const LayoutTypes = require('./LayoutTypes');
4
 const _ = require('lodash');
3
 const _ = require('lodash');
4
+const Examples = require('./LayoutExamples');
5
 
5
 
6
 describe('LayoutTreeParser', () => {
6
 describe('LayoutTreeParser', () => {
7
   let uut;
7
   let uut;
18
     it('single component', () => {
18
     it('single component', () => {
19
       expect(uut.parse(Examples.singleComponent)).toEqual({
19
       expect(uut.parse(Examples.singleComponent)).toEqual({
20
         type: LayoutTypes.Component,
20
         type: LayoutTypes.Component,
21
-        data: { name: 'MyReactComponent' },
21
+        data: { name: 'MyReactComponent', options: Examples.options, passProps: Examples.passProps },
22
         children: []
22
         children: []
23
       });
23
       });
24
     });
24
     });
40
     });
40
     });
41
 
41
 
42
     it('stack of components with top bar', () => {
42
     it('stack of components with top bar', () => {
43
-      expect(uut.parse(Examples.stackOfComponentsWithTopBar)).toEqual({
44
-        type: LayoutTypes.ComponentStack,
45
-        data: {},
43
+      expect(uut.parse(Examples.stackWithTopBar)).toEqual({
44
+        type: LayoutTypes.Stack,
45
+        data: {
46
+          options: Examples.options
47
+        },
46
         children: [
48
         children: [
47
           {
49
           {
48
             type: LayoutTypes.Component,
50
             type: LayoutTypes.Component,
51
           },
53
           },
52
           {
54
           {
53
             type: LayoutTypes.Component,
55
             type: LayoutTypes.Component,
54
-            data: { name: 'MyReactComponent2', options: Examples.topBarOptions },
56
+            data: { name: 'MyReactComponent2', options: Examples.options },
55
             children: []
57
             children: []
56
           }
58
           }
57
         ]
59
         ]
64
       expect(result.type).toEqual(LayoutTypes.BottomTabs);
66
       expect(result.type).toEqual(LayoutTypes.BottomTabs);
65
       expect(result.data).toEqual({});
67
       expect(result.data).toEqual({});
66
       expect(result.children.length).toEqual(3);
68
       expect(result.children.length).toEqual(3);
67
-      expect(result.children[0].type).toEqual(LayoutTypes.ComponentStack);
68
-      expect(result.children[1].type).toEqual(LayoutTypes.ComponentStack);
69
+      expect(result.children[0].type).toEqual(LayoutTypes.Stack);
70
+      expect(result.children[1].type).toEqual(LayoutTypes.Stack);
69
       expect(result.children[2].type).toEqual(LayoutTypes.Component);
71
       expect(result.children[2].type).toEqual(LayoutTypes.Component);
70
     });
72
     });
71
 
73
 
81
       expect(result.children[0].children.length).toEqual(1);
83
       expect(result.children[0].children.length).toEqual(1);
82
       expect(result.children[0].children[0].type).toEqual(LayoutTypes.Component);
84
       expect(result.children[0].children[0].type).toEqual(LayoutTypes.Component);
83
       expect(result.children[1].children.length).toEqual(1);
85
       expect(result.children[1].children.length).toEqual(1);
84
-      expect(result.children[1].children[0].type).toEqual(LayoutTypes.ComponentStack);
86
+      expect(result.children[1].children[0].type).toEqual(LayoutTypes.Stack);
85
       expect(result.children[2].children.length).toEqual(1);
87
       expect(result.children[2].children.length).toEqual(1);
86
       expect(result.children[2].children[0].type).toEqual(LayoutTypes.Component);
88
       expect(result.children[2].children[0].type).toEqual(LayoutTypes.Component);
87
     });
89
     });
94
       const result = uut.parse(Examples.topTabs);
96
       const result = uut.parse(Examples.topTabs);
95
       expect(_.keys(result)).toEqual(['type', 'data', 'children']);
97
       expect(_.keys(result)).toEqual(['type', 'data', 'children']);
96
       expect(result.type).toEqual(LayoutTypes.TopTabs);
98
       expect(result.type).toEqual(LayoutTypes.TopTabs);
97
-      expect(result.data).toEqual({});
99
+      expect(result.data).toEqual({ options: Examples.options });
98
       expect(result.children.length).toEqual(5);
100
       expect(result.children.length).toEqual(5);
99
       expect(result.children[0].type).toEqual(LayoutTypes.Component);
101
       expect(result.children[0].type).toEqual(LayoutTypes.Component);
100
       expect(result.children[1].type).toEqual(LayoutTypes.Component);
102
       expect(result.children[1].type).toEqual(LayoutTypes.Component);
101
       expect(result.children[2].type).toEqual(LayoutTypes.Component);
103
       expect(result.children[2].type).toEqual(LayoutTypes.Component);
102
       expect(result.children[3].type).toEqual(LayoutTypes.Component);
104
       expect(result.children[3].type).toEqual(LayoutTypes.Component);
103
-      expect(result.children[4].type).toEqual(LayoutTypes.ComponentStack);
105
+      expect(result.children[4].type).toEqual(LayoutTypes.Stack);
104
     });
106
     });
105
 
107
 
106
     it('complex layout example', () => {
108
     it('complex layout example', () => {
108
       expect(result.type).toEqual('SideMenuRoot');
110
       expect(result.type).toEqual('SideMenuRoot');
109
       expect(result.children[1].type).toEqual('SideMenuCenter');
111
       expect(result.children[1].type).toEqual('SideMenuCenter');
110
       expect(result.children[1].children[0].type).toEqual('BottomTabs');
112
       expect(result.children[1].children[0].type).toEqual('BottomTabs');
111
-      expect(result.children[1].children[0].children[2].type).toEqual('ComponentStack');
113
+      expect(result.children[1].children[0].children[2].type).toEqual('Stack');
112
       expect(result.children[1].children[0].children[2].children[0].type).toEqual('TopTabs');
114
       expect(result.children[1].children[0].children[2].children[0].type).toEqual('TopTabs');
113
       expect(result.children[1].children[0].children[2].children[0].children[2].type).toEqual('TopTabs');
115
       expect(result.children[1].children[0].children[2].children[0].children[2].type).toEqual('TopTabs');
114
-      expect(result.children[1].children[0].children[2].children[0].children[2].children[4].type).toEqual('ComponentStack');
115
-      // expect(result.children[1].children[0].children[2].children[0].children[2].data).toEqual({ options: {} });
116
+      expect(result.children[1].children[0].children[2].children[0].children[2].children[4].type).toEqual('Stack');
117
+      expect(result.children[1].children[0].children[2].children[0].children[2].data).toEqual({ options: { topBar: { title: 'Hello1' } } });
116
     });
118
     });
117
   });
119
   });
120
+
121
+  it('options for all containing types', () => {
122
+    const options = {};
123
+    expect(uut.parse({ component: { options } }).data.options).toBe(options);
124
+    expect(uut.parse({ stack: { options } }).data.options).toBe(options);
125
+    expect(uut.parse({ bottomTabs: { options } }).data.options).toBe(options);
126
+    expect(uut.parse({ topTabs: { options } }).data.options).toBe(options);
127
+    expect(uut.parse({ sideMenu: { options, center: { component: {} } } }).data.options).toBe(options);
128
+  });
118
 });
129
 });

+ 2
- 3
lib/src/commands/LayoutTypes.js View File

1
 module.exports = {
1
 module.exports = {
2
   Component: 'Component',
2
   Component: 'Component',
3
-  ComponentStack: 'ComponentStack',
3
+  Stack: 'Stack',
4
   BottomTabs: 'BottomTabs',
4
   BottomTabs: 'BottomTabs',
5
   SideMenuRoot: 'SideMenuRoot',
5
   SideMenuRoot: 'SideMenuRoot',
6
   SideMenuCenter: 'SideMenuCenter',
6
   SideMenuCenter: 'SideMenuCenter',
7
   SideMenuLeft: 'SideMenuLeft',
7
   SideMenuLeft: 'SideMenuLeft',
8
   SideMenuRight: 'SideMenuRight',
8
   SideMenuRight: 'SideMenuRight',
9
-  TopTabs: 'TopTabs',
10
-  TopTab: 'TopTab'
9
+  TopTabs: 'TopTabs'
11
 };
10
 };