Browse Source

redesigning new API

Daniel Zlotin 7 years ago
parent
commit
102edbd67e

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

@@ -1,108 +0,0 @@
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

@@ -0,0 +1,125 @@
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,15 +27,15 @@ class LayoutTreeParser {
27 27
   _topTabs(api) {
28 28
     return {
29 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 35
   _sideMenu(api) {
36 36
     return {
37 37
       type: LayoutTypes.SideMenuRoot,
38
-      data: {},
38
+      data: { options: api.options },
39 39
       children: this._sideMenuChildren(api)
40 40
     };
41 41
   }
@@ -70,23 +70,23 @@ class LayoutTreeParser {
70 70
   _bottomTabs(api) {
71 71
     return {
72 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 78
   _stack(api) {
79 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 86
   _component(api) {
87
-    return { 
87
+    return {
88 88
       type: LayoutTypes.Component,
89
-      data: api,
89
+      data: { name: api.name, options: api.options, passProps: api.passProps },
90 90
       children: []
91 91
     };
92 92
   }

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

@@ -1,7 +1,7 @@
1 1
 const LayoutTreeParser = require('./LayoutTreeParser');
2
-const Examples = require('./Examples');
3 2
 const LayoutTypes = require('./LayoutTypes');
4 3
 const _ = require('lodash');
4
+const Examples = require('./LayoutExamples');
5 5
 
6 6
 describe('LayoutTreeParser', () => {
7 7
   let uut;
@@ -18,7 +18,7 @@ describe('LayoutTreeParser', () => {
18 18
     it('single component', () => {
19 19
       expect(uut.parse(Examples.singleComponent)).toEqual({
20 20
         type: LayoutTypes.Component,
21
-        data: { name: 'MyReactComponent' },
21
+        data: { name: 'MyReactComponent', options: Examples.options, passProps: Examples.passProps },
22 22
         children: []
23 23
       });
24 24
     });
@@ -40,9 +40,11 @@ describe('LayoutTreeParser', () => {
40 40
     });
41 41
 
42 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 48
         children: [
47 49
           {
48 50
             type: LayoutTypes.Component,
@@ -51,7 +53,7 @@ describe('LayoutTreeParser', () => {
51 53
           },
52 54
           {
53 55
             type: LayoutTypes.Component,
54
-            data: { name: 'MyReactComponent2', options: Examples.topBarOptions },
56
+            data: { name: 'MyReactComponent2', options: Examples.options },
55 57
             children: []
56 58
           }
57 59
         ]
@@ -64,8 +66,8 @@ describe('LayoutTreeParser', () => {
64 66
       expect(result.type).toEqual(LayoutTypes.BottomTabs);
65 67
       expect(result.data).toEqual({});
66 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 71
       expect(result.children[2].type).toEqual(LayoutTypes.Component);
70 72
     });
71 73
 
@@ -81,7 +83,7 @@ describe('LayoutTreeParser', () => {
81 83
       expect(result.children[0].children.length).toEqual(1);
82 84
       expect(result.children[0].children[0].type).toEqual(LayoutTypes.Component);
83 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 87
       expect(result.children[2].children.length).toEqual(1);
86 88
       expect(result.children[2].children[0].type).toEqual(LayoutTypes.Component);
87 89
     });
@@ -94,13 +96,13 @@ describe('LayoutTreeParser', () => {
94 96
       const result = uut.parse(Examples.topTabs);
95 97
       expect(_.keys(result)).toEqual(['type', 'data', 'children']);
96 98
       expect(result.type).toEqual(LayoutTypes.TopTabs);
97
-      expect(result.data).toEqual({});
99
+      expect(result.data).toEqual({ options: Examples.options });
98 100
       expect(result.children.length).toEqual(5);
99 101
       expect(result.children[0].type).toEqual(LayoutTypes.Component);
100 102
       expect(result.children[1].type).toEqual(LayoutTypes.Component);
101 103
       expect(result.children[2].type).toEqual(LayoutTypes.Component);
102 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 108
     it('complex layout example', () => {
@@ -108,11 +110,20 @@ describe('LayoutTreeParser', () => {
108 110
       expect(result.type).toEqual('SideMenuRoot');
109 111
       expect(result.children[1].type).toEqual('SideMenuCenter');
110 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 114
       expect(result.children[1].children[0].children[2].children[0].type).toEqual('TopTabs');
113 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,11 +1,10 @@
1 1
 module.exports = {
2 2
   Component: 'Component',
3
-  ComponentStack: 'ComponentStack',
3
+  Stack: 'Stack',
4 4
   BottomTabs: 'BottomTabs',
5 5
   SideMenuRoot: 'SideMenuRoot',
6 6
   SideMenuCenter: 'SideMenuCenter',
7 7
   SideMenuLeft: 'SideMenuLeft',
8 8
   SideMenuRight: 'SideMenuRight',
9
-  TopTabs: 'TopTabs',
10
-  TopTab: 'TopTab'
9
+  TopTabs: 'TopTabs'
11 10
 };