Daniel Zlotin 7 gadus atpakaļ
vecāks
revīzija
f30a1f5323

+ 0
- 125
lib/src/commands/LayoutExamples.js Parādīt failu

@@ -1,125 +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 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
-};

+ 154
- 37
lib/src/commands/LayoutTreeParser.test.ts Parādīt failu

@@ -1,7 +1,6 @@
1 1
 import * as  _ from 'lodash';
2 2
 import { LayoutTreeParser } from './LayoutTreeParser';
3
-import { LayoutTypes } from './LayoutTypes';
4
-import { Examples } from './LayoutExamples';
3
+import { LayoutType } from './values/LayoutType';
5 4
 
6 5
 describe('LayoutTreeParser', () => {
7 6
   let uut;
@@ -16,9 +15,9 @@ describe('LayoutTreeParser', () => {
16 15
     });
17 16
 
18 17
     it('single component', () => {
19
-      expect(uut.parse(Examples.singleComponent)).toEqual({
20
-        type: LayoutTypes.Component,
21
-        data: { name: 'MyReactComponent', options: Examples.options, passProps: Examples.passProps },
18
+      expect(uut.parse(LayoutExamples.singleComponent)).toEqual({
19
+        type: LayoutType.Component,
20
+        data: { name: 'MyReactComponent', options: LayoutExamples.options, passProps: LayoutExamples.passProps },
22 21
         children: []
23 22
       });
24 23
     });
@@ -27,33 +26,33 @@ describe('LayoutTreeParser', () => {
27 26
       const result = uut.parse({
28 27
         component: {
29 28
           name: 'MyScreen',
30
-          passProps: Examples.passProps
29
+          passProps: LayoutExamples.passProps
31 30
         }
32 31
       });
33 32
       expect(result).toEqual({
34
-        type: LayoutTypes.Component,
35
-        data: { name: 'MyScreen', passProps: Examples.passProps },
33
+        type: LayoutType.Component,
34
+        data: { name: 'MyScreen', passProps: LayoutExamples.passProps },
36 35
         children: []
37 36
       });
38
-      expect(result.data.passProps).toBe(Examples.passProps);
37
+      expect(result.data.passProps).toBe(LayoutExamples.passProps);
39 38
       expect(result.data.passProps.fnProp()).toEqual('Hello from a function');
40 39
     });
41 40
 
42 41
     it('stack of components with top bar', () => {
43
-      expect(uut.parse(Examples.stackWithTopBar)).toEqual({
44
-        type: LayoutTypes.Stack,
42
+      expect(uut.parse(LayoutExamples.stackWithTopBar)).toEqual({
43
+        type: LayoutType.Stack,
45 44
         data: {
46
-          options: Examples.options
45
+          options: LayoutExamples.options
47 46
         },
48 47
         children: [
49 48
           {
50
-            type: LayoutTypes.Component,
49
+            type: LayoutType.Component,
51 50
             data: { name: 'MyReactComponent1' },
52 51
             children: []
53 52
           },
54 53
           {
55
-            type: LayoutTypes.Component,
56
-            data: { name: 'MyReactComponent2', options: Examples.options },
54
+            type: LayoutType.Component,
55
+            data: { name: 'MyReactComponent2', options: LayoutExamples.options },
57 56
             children: []
58 57
           }
59 58
         ]
@@ -61,31 +60,31 @@ describe('LayoutTreeParser', () => {
61 60
     });
62 61
 
63 62
     it('bottom tabs', () => {
64
-      const result = uut.parse(Examples.bottomTabs);
63
+      const result = uut.parse(LayoutExamples.bottomTabs);
65 64
       expect(_.keys(result)).toEqual(['type', 'data', 'children']);
66
-      expect(result.type).toEqual(LayoutTypes.BottomTabs);
65
+      expect(result.type).toEqual(LayoutType.BottomTabs);
67 66
       expect(result.data).toEqual({});
68 67
       expect(result.children.length).toEqual(3);
69
-      expect(result.children[0].type).toEqual(LayoutTypes.Stack);
70
-      expect(result.children[1].type).toEqual(LayoutTypes.Stack);
71
-      expect(result.children[2].type).toEqual(LayoutTypes.Component);
68
+      expect(result.children[0].type).toEqual(LayoutType.Stack);
69
+      expect(result.children[1].type).toEqual(LayoutType.Stack);
70
+      expect(result.children[2].type).toEqual(LayoutType.Component);
72 71
     });
73 72
 
74 73
     it('side menus', () => {
75
-      const result = uut.parse(Examples.sideMenu);
74
+      const result = uut.parse(LayoutExamples.sideMenu);
76 75
       expect(_.keys(result)).toEqual(['type', 'data', 'children']);
77
-      expect(result.type).toEqual(LayoutTypes.SideMenuRoot);
76
+      expect(result.type).toEqual(LayoutType.SideMenuRoot);
78 77
       expect(result.data).toEqual({});
79 78
       expect(result.children.length).toEqual(3);
80
-      expect(result.children[0].type).toEqual(LayoutTypes.SideMenuLeft);
81
-      expect(result.children[1].type).toEqual(LayoutTypes.SideMenuCenter);
82
-      expect(result.children[2].type).toEqual(LayoutTypes.SideMenuRight);
79
+      expect(result.children[0].type).toEqual(LayoutType.SideMenuLeft);
80
+      expect(result.children[1].type).toEqual(LayoutType.SideMenuCenter);
81
+      expect(result.children[2].type).toEqual(LayoutType.SideMenuRight);
83 82
       expect(result.children[0].children.length).toEqual(1);
84
-      expect(result.children[0].children[0].type).toEqual(LayoutTypes.Component);
83
+      expect(result.children[0].children[0].type).toEqual(LayoutType.Component);
85 84
       expect(result.children[1].children.length).toEqual(1);
86
-      expect(result.children[1].children[0].type).toEqual(LayoutTypes.Stack);
85
+      expect(result.children[1].children[0].type).toEqual(LayoutType.Stack);
87 86
       expect(result.children[2].children.length).toEqual(1);
88
-      expect(result.children[2].children[0].type).toEqual(LayoutTypes.Component);
87
+      expect(result.children[2].children[0].type).toEqual(LayoutType.Component);
89 88
     });
90 89
 
91 90
     it('side menu center is require', () => {
@@ -93,20 +92,20 @@ describe('LayoutTreeParser', () => {
93 92
     });
94 93
 
95 94
     it('top tabs', () => {
96
-      const result = uut.parse(Examples.topTabs);
95
+      const result = uut.parse(LayoutExamples.topTabs);
97 96
       expect(_.keys(result)).toEqual(['type', 'data', 'children']);
98
-      expect(result.type).toEqual(LayoutTypes.TopTabs);
99
-      expect(result.data).toEqual({ options: Examples.options });
97
+      expect(result.type).toEqual(LayoutType.TopTabs);
98
+      expect(result.data).toEqual({ options: LayoutExamples.options });
100 99
       expect(result.children.length).toEqual(5);
101
-      expect(result.children[0].type).toEqual(LayoutTypes.Component);
102
-      expect(result.children[1].type).toEqual(LayoutTypes.Component);
103
-      expect(result.children[2].type).toEqual(LayoutTypes.Component);
104
-      expect(result.children[3].type).toEqual(LayoutTypes.Component);
105
-      expect(result.children[4].type).toEqual(LayoutTypes.Stack);
100
+      expect(result.children[0].type).toEqual(LayoutType.Component);
101
+      expect(result.children[1].type).toEqual(LayoutType.Component);
102
+      expect(result.children[2].type).toEqual(LayoutType.Component);
103
+      expect(result.children[3].type).toEqual(LayoutType.Component);
104
+      expect(result.children[4].type).toEqual(LayoutType.Stack);
106 105
     });
107 106
 
108 107
     it('complex layout example', () => {
109
-      const result = uut.parse(Examples.complexLayout);
108
+      const result = uut.parse(LayoutExamples.complexLayout);
110 109
       expect(result.type).toEqual('SideMenuRoot');
111 110
       expect(result.children[1].type).toEqual('SideMenuCenter');
112 111
       expect(result.children[1].children[0].type).toEqual('BottomTabs');
@@ -127,3 +126,121 @@ describe('LayoutTreeParser', () => {
127 126
     expect(uut.parse({ sideMenu: { options, center: { component: {} } } }).data.options).toBe(options);
128 127
   });
129 128
 });
129
+
130
+const LayoutExamples = {
131
+  passProps: {
132
+    strProp: 'string prop',
133
+    numProp: 12345,
134
+    objProp: { inner: { foo: 'bar' } },
135
+    fnProp: () => 'Hello from a function'
136
+  },
137
+
138
+  options: {
139
+    topBar: {
140
+      title: 'Hello1'
141
+    }
142
+  },
143
+
144
+  singleComponent: {
145
+    component: {
146
+      name: 'MyReactComponent',
147
+      options: this.options,
148
+      passProps: this.passProps
149
+    }
150
+  },
151
+
152
+  stackWithTopBar: {
153
+    stack: {
154
+      children: [
155
+        {
156
+          component: {
157
+            name: 'MyReactComponent1'
158
+          }
159
+        },
160
+        {
161
+          component: {
162
+            name: 'MyReactComponent2',
163
+            options: this.options
164
+          }
165
+        }
166
+      ],
167
+      options: this.options
168
+    }
169
+  },
170
+
171
+  bottomTabs: {
172
+    bottomTabs: {
173
+      children: [
174
+        { ...this.stackWithTopBar },
175
+        { ...this.stackWithTopBar },
176
+        {
177
+          component: {
178
+            name: 'MyReactComponent1'
179
+          }
180
+        }
181
+      ]
182
+    }
183
+  },
184
+
185
+  sideMenu: {
186
+    sideMenu: {
187
+      left: { ...this.singleComponent },
188
+      center: { ...this.stackWithTopBar },
189
+      right: { ...this.singleComponent }
190
+    }
191
+  },
192
+
193
+  topTabs: {
194
+    topTabs: {
195
+      children: [
196
+        { ...this.singleComponent },
197
+        { ...this.singleComponent },
198
+        { ...this.singleComponent },
199
+        { ...this.singleComponent },
200
+        { ...this.stackWithTopBar }
201
+      ],
202
+      options: this.options
203
+    }
204
+  },
205
+
206
+  complexLayout: {
207
+    sideMenu: {
208
+      left: { ...this.singleComponent },
209
+      center: {
210
+        bottomTabs: {
211
+          children: [
212
+            { ...this.stackWithTopBar },
213
+            { ...this.stackWithTopBar },
214
+            {
215
+              stack: {
216
+                children: [
217
+                  {
218
+                    topTabs: {
219
+                      children: [
220
+                        { ...this.stackWithTopBar },
221
+                        { ...this.stackWithTopBar },
222
+                        {
223
+                          topTabs: {
224
+                            options: this.options,
225
+                            children: [
226
+                              { ...this.singleComponent },
227
+                              { ...this.singleComponent },
228
+                              { ...this.singleComponent },
229
+                              { ...this.singleComponent },
230
+                              { ...this.stackWithTopBar }
231
+                            ]
232
+                          }
233
+                        }
234
+                      ]
235
+                    }
236
+                  }
237
+                ]
238
+              }
239
+            }
240
+          ]
241
+        }
242
+      }
243
+    }
244
+  }
245
+}
246
+

+ 9
- 9
lib/src/commands/LayoutTreeParser.ts Parādīt failu

@@ -1,5 +1,5 @@
1 1
 import * as _ from 'lodash';
2
-import { LayoutTypes } from './values/LayoutTypes';
2
+import { LayoutType } from './values/LayoutType';
3 3
 
4 4
 export class LayoutTreeParser {
5 5
   constructor() {
@@ -26,7 +26,7 @@ export class LayoutTreeParser {
26 26
 
27 27
   _topTabs(api) {
28 28
     return {
29
-      type: LayoutTypes.TopTabs,
29
+      type: LayoutType.TopTabs,
30 30
       data: { options: api.options },
31 31
       children: _.map(api.children, this.parse)
32 32
     };
@@ -34,7 +34,7 @@ export class LayoutTreeParser {
34 34
 
35 35
   _sideMenu(api) {
36 36
     return {
37
-      type: LayoutTypes.SideMenuRoot,
37
+      type: LayoutType.SideMenuRoot,
38 38
       data: { options: api.options },
39 39
       children: this._sideMenuChildren(api)
40 40
     };
@@ -47,19 +47,19 @@ export class LayoutTreeParser {
47 47
     const children = [];
48 48
     if (api.left) {
49 49
       children.push({
50
-        type: LayoutTypes.SideMenuLeft,
50
+        type: LayoutType.SideMenuLeft,
51 51
         data: {},
52 52
         children: [this.parse(api.left)]
53 53
       });
54 54
     }
55 55
     children.push({
56
-      type: LayoutTypes.SideMenuCenter,
56
+      type: LayoutType.SideMenuCenter,
57 57
       data: {},
58 58
       children: [this.parse(api.center)]
59 59
     });
60 60
     if (api.right) {
61 61
       children.push({
62
-        type: LayoutTypes.SideMenuRight,
62
+        type: LayoutType.SideMenuRight,
63 63
         data: {},
64 64
         children: [this.parse(api.right)]
65 65
       });
@@ -69,7 +69,7 @@ export class LayoutTreeParser {
69 69
 
70 70
   _bottomTabs(api) {
71 71
     return {
72
-      type: LayoutTypes.BottomTabs,
72
+      type: LayoutType.BottomTabs,
73 73
       data: { options: api.options },
74 74
       children: _.map(api.children, this.parse)
75 75
     };
@@ -77,7 +77,7 @@ export class LayoutTreeParser {
77 77
 
78 78
   _stack(api) {
79 79
     return {
80
-      type: LayoutTypes.Stack,
80
+      type: LayoutType.Stack,
81 81
       data: { name: api.name, options: api.options },
82 82
       children: _.map(api.children, this.parse)
83 83
     };
@@ -85,7 +85,7 @@ export class LayoutTreeParser {
85 85
 
86 86
   _component(api) {
87 87
     return {
88
-      type: LayoutTypes.Component,
88
+      type: LayoutType.Component,
89 89
       data: { name: api.name, options: api.options, passProps: api.passProps },
90 90
       children: []
91 91
     };

+ 16
- 2
lib/src/commands/values/LayoutNode.test.ts Parādīt failu

@@ -1,7 +1,21 @@
1 1
 import { LayoutNode } from './LayoutNode';
2
+import { LayoutType } from './LayoutType';
2 3
 
3 4
 describe('LayoutNode', () => {
4
-  it('value type', () => {
5
-    
5
+  it('convertable from same data structure', () => {
6
+    const x = {
7
+      id: 'theId',
8
+      type: LayoutType.Component,
9
+      data: {},
10
+      children: []
11
+    };
12
+
13
+    let got;
14
+    function expectingLayoutNode(param: LayoutNode) {
15
+      got = param;
16
+    }
17
+    expectingLayoutNode(x);
18
+
19
+    expect(got).toBe(x);
6 20
   });
7 21
 });

+ 6
- 2
lib/src/commands/values/LayoutNode.ts Parādīt failu

@@ -1,5 +1,9 @@
1
-import { LayoutTypes } from "./LayoutTypes";
1
+import { LayoutType } from "./LayoutType";
2 2
 
3 3
 export interface LayoutNode {
4
-  type: LayoutTypes
4
+  id: string;
5
+  type: LayoutType;
6
+  data: object;
7
+  children: Array<LayoutNode>;
5 8
 }
9
+

+ 11
- 16
lib/src/commands/values/LayoutType.test.ts Parādīt failu

@@ -1,26 +1,21 @@
1
-import { LayoutType } from './LayoutType';
1
+import { LayoutType, isLayoutType } from './LayoutType';
2 2
 
3
-describe('LayoutType', () => {
3
+describe.only('LayoutType', () => {
4 4
   it('is an enum', () => {
5 5
     expect(LayoutType.Component).toEqual('Component');
6 6
     expect(LayoutType.Stack).toEqual('Stack');
7
-  });
8 7
 
9
-  it('valueOf dynamic string value', () => {
10 8
     const name = 'Stack';
11 9
     expect(LayoutType[name]).toEqual(LayoutType.Stack);
12 10
     expect(LayoutType['asdasd']).toEqual(undefined);
13
-    expect(LayoutType.isValid('asdasd')).toBe(false);
14
-    expect(LayoutType.isValid('TopTabs')).toBe(true);
15
-    expect(LayoutType.isValid('isValid')).toBe(false);
16 11
   });
17
-});
18 12
 
19
-// Component: 'Component',
20
-// Stack: 'Stack',
21
-// BottomTabs: 'BottomTabs',
22
-// SideMenuRoot: 'SideMenuRoot',
23
-// SideMenuCenter: 'SideMenuCenter',
24
-// SideMenuLeft: 'SideMenuLeft',
25
-// SideMenuRight: 'SideMenuRight',
26
-// TopTabs: 'TopTabs',
13
+  it('isLayoutType', () => {
14
+    expect(isLayoutType(null)).toBe(false);
15
+    expect(isLayoutType(undefined)).toBe(false);
16
+    expect(isLayoutType('')).toBe(false);
17
+    expect(isLayoutType('asdasd')).toBe(false);
18
+    expect(isLayoutType('TopTabs')).toBe(true);
19
+    expect(isLayoutType('isLayoutType')).toBe(false);
20
+  });
21
+});

+ 3
- 5
lib/src/commands/values/LayoutType.ts Parādīt failu

@@ -1,6 +1,4 @@
1
-import * as _ from 'lodash';
2
-
3
-export const enum LayoutType {
1
+export enum LayoutType {
4 2
   Component = 'Component',
5 3
   Stack = 'Stack',
6 4
   BottomTabs = 'BottomTabs',
@@ -11,6 +9,6 @@ export const enum LayoutType {
11 9
   TopTabs = 'TopTabs'
12 10
 };
13 11
 
14
-export function isValid(name: string) {
15
-  return _.keys(LayoutType);
12
+export function isLayoutType(name: string): boolean {
13
+  return !!LayoutType[name];
16 14
 }

+ 0
- 11
lib/src/commands/values/LayoutTypes.test.ts Parādīt failu

@@ -1,11 +0,0 @@
1
-import { LayoutTypes } from './LayoutTypes';
2
-
3
-describe('LayoutTypes', () => {
4
-  it('enum isValid', () => {
5
-    expect(LayoutTypes.isValid('')).toBe(false);
6
-    expect(LayoutTypes.isValid('unknown type')).toBe(false);
7
-    expect(LayoutTypes.isValid('Component')).toBe(true);
8
-    expect(LayoutTypes.isValid('Stack')).toBe(true);
9
-    expect(LayoutTypes.isValid('Stack ')).toBe(false);
10
-  });
11
-});

+ 0
- 15
lib/src/commands/values/LayoutTypes.ts Parādīt failu

@@ -1,15 +0,0 @@
1
-export class LayoutTypes {
2
-  private static validTypes = [
3
-    'Component',
4
-    'Stack',
5
-    'BottomTabs',
6
-    'SideMenuRoot',
7
-    'SideMenuCenter',
8
-    'SideMenuLeft',
9
-    'SideMenuRight',
10
-    'TopTabs'];
11
-
12
-  public static isValid(type: string) {
13
-    return this.validTypes.includes(type);
14
-  }
15
-}