Daniel Zlotin 6 years ago
parent
commit
110eb4394c

+ 16
- 16
lib/src/commands/Commands.js View File

10
 
10
 
11
   setRoot(simpleApi) {
11
   setRoot(simpleApi) {
12
     const input = _.cloneDeep(simpleApi);
12
     const input = _.cloneDeep(simpleApi);
13
-    const layout = this.layoutTreeParser.parseFromSimpleJSON(input);
13
+    const layout = this.layoutTreeParser.parse(input);
14
     this.layoutTreeCrawler.crawl(layout);
14
     this.layoutTreeCrawler.crawl(layout);
15
     return this.nativeCommandsSender.setRoot(layout);
15
     return this.nativeCommandsSender.setRoot(layout);
16
   }
16
   }
29
 
29
 
30
   showModal(simpleApi) {
30
   showModal(simpleApi) {
31
     const input = _.cloneDeep(simpleApi);
31
     const input = _.cloneDeep(simpleApi);
32
-    const layout = this.layoutTreeParser.parseFromSimpleJSON(input);
32
+    const layout = this.layoutTreeParser.parse(input);
33
     this.layoutTreeCrawler.crawl(layout);
33
     this.layoutTreeCrawler.crawl(layout);
34
     return this.nativeCommandsSender.showModal(layout);
34
     return this.nativeCommandsSender.showModal(layout);
35
   }
35
   }
45
   push(onContainerId, containerData) {
45
   push(onContainerId, containerData) {
46
     const input = _.cloneDeep(containerData);
46
     const input = _.cloneDeep(containerData);
47
     OptionsProcessor.processOptions(input);
47
     OptionsProcessor.processOptions(input);
48
-    const layout = this.layoutTreeParser.parseFromSimpleJSON(input);
48
+    const layout = this.layoutTreeParser.parse(input);
49
     this.layoutTreeCrawler.crawl(layout);
49
     this.layoutTreeCrawler.crawl(layout);
50
     return this.nativeCommandsSender.push(onContainerId, layout);
50
     return this.nativeCommandsSender.push(onContainerId, layout);
51
   }
51
   }
62
     return this.nativeCommandsSender.popToRoot(containerId);
62
     return this.nativeCommandsSender.popToRoot(containerId);
63
   }
63
   }
64
 
64
 
65
-  showOverlay(type, options) {
66
-    let promise;
67
-    if (type === 'custom') {
68
-      const layout = this.layoutTreeParser.createDialogContainer({ name: options });
69
-      this.layoutTreeCrawler.crawl(layout);
70
-      promise = this.nativeCommandsSender.showOverlay(type, layout);
71
-    } else {
72
-      const input = _.cloneDeep(options);
73
-      OptionsProcessor.processOptions(input);
74
-      promise = this.nativeCommandsSender.showOverlay(type, input);
75
-    }
76
-    return promise;
77
-  }
65
+  // showOverlay(type, options) {
66
+    // let promise;
67
+    // if (type === 'custom') {
68
+    //   const layout = this.layoutTreeParser.createDialogContainer({ name: options });
69
+    //   this.layoutTreeCrawler.crawl(layout);
70
+    //   promise = this.nativeCommandsSender.showOverlay(type, layout);
71
+    // } else {
72
+    //   const input = _.cloneDeep(options);
73
+    //   OptionsProcessor.processOptions(input);
74
+    //   promise = this.nativeCommandsSender.showOverlay(type, input);
75
+    // }
76
+    // return promise;
77
+  // }
78
 
78
 
79
   dismissOverlay() {
79
   dismissOverlay() {
80
     return this.nativeCommandsSender.dismissOverlay();
80
     return this.nativeCommandsSender.dismissOverlay();

+ 44
- 48
lib/src/commands/Commands.test.js View File

20
   describe('setRoot', () => {
20
   describe('setRoot', () => {
21
     it('sends setRoot to native after parsing into a correct layout tree', () => {
21
     it('sends setRoot to native after parsing into a correct layout tree', () => {
22
       uut.setRoot({
22
       uut.setRoot({
23
-        container: {
23
+        component: {
24
           name: 'com.example.MyScreen'
24
           name: 'com.example.MyScreen'
25
         }
25
         }
26
       });
26
       });
27
       expect(mockCommandsSender.setRoot).toHaveBeenCalledTimes(1);
27
       expect(mockCommandsSender.setRoot).toHaveBeenCalledTimes(1);
28
       expect(mockCommandsSender.setRoot).toHaveBeenCalledWith({
28
       expect(mockCommandsSender.setRoot).toHaveBeenCalledWith({
29
-        type: 'ContainerStack',
30
-        id: 'ContainerStack+UNIQUE_ID',
31
-        data: {},
32
-        children: [
33
-          {
34
-            type: 'Container',
35
-            id: 'Container+UNIQUE_ID',
36
-            children: [],
37
-            data: {
38
-              name: 'com.example.MyScreen',
39
-              navigationOptions: {}
40
-            }
41
-          }
42
-        ]
29
+        type: 'Component',
30
+        id: 'Component+UNIQUE_ID',
31
+        children: [],
32
+        data: {
33
+          name: 'com.example.MyScreen',
34
+          navigationOptions: {}
35
+        }
43
       });
36
       });
44
     });
37
     });
45
 
38
 
46
     it('deep clones input to avoid mutation errors', () => {
39
     it('deep clones input to avoid mutation errors', () => {
47
       const obj = {};
40
       const obj = {};
48
-      uut.setRoot({ container: { name: 'bla', inner: obj } });
49
-      expect(mockCommandsSender.setRoot.mock.calls[0][0].children[0].data.inner).not.toBe(obj);
41
+      uut.setRoot({ component: { name: 'bla', inner: obj } });
42
+      expect(mockCommandsSender.setRoot.mock.calls[0][0].data.inner).not.toBe(obj);
50
     });
43
     });
51
 
44
 
52
     it('passProps into containers', () => {
45
     it('passProps into containers', () => {
53
-      expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual({});
54
-      uut.setRoot(SimpleLayouts.singleScreenWithAditionalParams);
55
-      expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual(SimpleLayouts.passProps);
46
+      const passProps = {
47
+        fn: () => 'Hello'
48
+      };
49
+      expect(store.getPropsForContainerId('Component+UNIQUE_ID')).toEqual({});
50
+      uut.setRoot({ component: { name: 'asd', passProps } });
51
+      expect(store.getPropsForContainerId('Component+UNIQUE_ID')).toEqual(passProps);
52
+      expect(store.getPropsForContainerId('Component+UNIQUE_ID').fn()).toEqual('Hello');
56
     });
53
     });
57
 
54
 
58
     it('returns a promise with the resolved layout', async () => {
55
     it('returns a promise with the resolved layout', async () => {
59
       mockCommandsSender.setRoot.mockReturnValue(Promise.resolve('the resolved layout'));
56
       mockCommandsSender.setRoot.mockReturnValue(Promise.resolve('the resolved layout'));
60
-      const result = await uut.setRoot({ container: { name: 'com.example.MyScreen' } });
57
+      const result = await uut.setRoot({ component: { name: 'com.example.MyScreen' } });
61
       expect(result).toEqual('the resolved layout');
58
       expect(result).toEqual('the resolved layout');
62
     });
59
     });
63
   });
60
   });
87
   describe('showModal', () => {
84
   describe('showModal', () => {
88
     it('sends command to native after parsing into a correct layout tree', () => {
85
     it('sends command to native after parsing into a correct layout tree', () => {
89
       uut.showModal({
86
       uut.showModal({
90
-        container: {
87
+        component: {
91
           name: 'com.example.MyScreen'
88
           name: 'com.example.MyScreen'
92
         }
89
         }
93
       });
90
       });
94
       expect(mockCommandsSender.showModal).toHaveBeenCalledTimes(1);
91
       expect(mockCommandsSender.showModal).toHaveBeenCalledTimes(1);
95
       expect(mockCommandsSender.showModal).toHaveBeenCalledWith({
92
       expect(mockCommandsSender.showModal).toHaveBeenCalledWith({
96
-        type: 'ContainerStack',
97
-        id: 'ContainerStack+UNIQUE_ID',
98
-        data: {},
99
-        children: [{
100
-          type: 'Container',
101
-          id: 'Container+UNIQUE_ID',
102
-          data: {
103
-            name: 'com.example.MyScreen',
104
-            navigationOptions: {}
105
-          },
106
-          children: []
107
-        }]
93
+        type: 'Component',
94
+        id: 'Component+UNIQUE_ID',
95
+        data: {
96
+          name: 'com.example.MyScreen',
97
+          navigationOptions: {}
98
+        },
99
+        children: []
108
       });
100
       });
109
     });
101
     });
110
 
102
 
111
     it('deep clones input to avoid mutation errors', () => {
103
     it('deep clones input to avoid mutation errors', () => {
112
       const obj = {};
104
       const obj = {};
113
-      uut.showModal({ container: { name: 'name', inner: obj } });
105
+      uut.showModal({ component: { name: 'name', inner: obj } });
114
       expect(mockCommandsSender.showModal.mock.calls[0][0].data.inner).not.toBe(obj);
106
       expect(mockCommandsSender.showModal.mock.calls[0][0].data.inner).not.toBe(obj);
115
     });
107
     });
116
 
108
 
117
     it('passProps into containers', () => {
109
     it('passProps into containers', () => {
118
-      expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual({});
110
+      const passProps = {
111
+        fn: () => 'hello'
112
+      };
113
+      expect(store.getPropsForContainerId('Component+UNIQUE_ID')).toEqual({});
119
       uut.showModal({
114
       uut.showModal({
120
-        container: {
115
+        component: {
121
           name: 'com.example.MyScreen',
116
           name: 'com.example.MyScreen',
122
-          passProps: SimpleLayouts.passProps
117
+          passProps
123
         }
118
         }
124
       });
119
       });
125
-      expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual(SimpleLayouts.passProps);
120
+      expect(store.getPropsForContainerId('Component+UNIQUE_ID')).toEqual(passProps);
126
     });
121
     });
127
 
122
 
128
     it('returns a promise with the resolved layout', async () => {
123
     it('returns a promise with the resolved layout', async () => {
129
       mockCommandsSender.showModal.mockReturnValue(Promise.resolve('the resolved layout'));
124
       mockCommandsSender.showModal.mockReturnValue(Promise.resolve('the resolved layout'));
130
-      const result = await uut.showModal({ container: { name: 'com.example.MyScreen' } });
125
+      const result = await uut.showModal({ component: { name: 'com.example.MyScreen' } });
131
       expect(result).toEqual('the resolved layout');
126
       expect(result).toEqual('the resolved layout');
132
     });
127
     });
133
   });
128
   });
163
   describe('push', () => {
158
   describe('push', () => {
164
     it('deep clones input to avoid mutation errors', () => {
159
     it('deep clones input to avoid mutation errors', () => {
165
       const obj = {};
160
       const obj = {};
166
-      uut.push('theContainerId', { name: 'name', inner: { foo: obj } });
167
-      expect(mockCommandsSender.push.mock.calls[0][1].data.inner.foo).not.toBe(obj);
161
+      uut.push('theContainerId', { component: { name: 'name', passProps: { foo: obj } } });
162
+      expect(mockCommandsSender.push.mock.calls[0][1].data.passProps.foo).not.toBe(obj);
168
     });
163
     });
169
 
164
 
170
     it('resolves with the parsed layout', async () => {
165
     it('resolves with the parsed layout', async () => {
171
       mockCommandsSender.push.mockReturnValue(Promise.resolve('the resolved layout'));
166
       mockCommandsSender.push.mockReturnValue(Promise.resolve('the resolved layout'));
172
-      const result = await uut.push('theContainerId', { name: 'com.example.MyScreen' });
167
+      const result = await uut.push('theContainerId', { component: { name: 'com.example.MyScreen' } });
173
       expect(result).toEqual('the resolved layout');
168
       expect(result).toEqual('the resolved layout');
174
     });
169
     });
175
 
170
 
176
     it('parses into correct layout node and sends to native', () => {
171
     it('parses into correct layout node and sends to native', () => {
177
-      uut.push('theContainerId', { name: 'com.example.MyScreen' });
172
+      uut.push('theContainerId', { component: { name: 'com.example.MyScreen' } });
178
       expect(mockCommandsSender.push).toHaveBeenCalledTimes(1);
173
       expect(mockCommandsSender.push).toHaveBeenCalledTimes(1);
179
       expect(mockCommandsSender.push).toHaveBeenCalledWith('theContainerId', {
174
       expect(mockCommandsSender.push).toHaveBeenCalledWith('theContainerId', {
180
-        type: 'Container',
181
-        id: 'Container+UNIQUE_ID',
175
+        type: 'Component',
176
+        id: 'Component+UNIQUE_ID',
182
         data: {
177
         data: {
183
           name: 'com.example.MyScreen',
178
           name: 'com.example.MyScreen',
184
           navigationOptions: {}
179
           navigationOptions: {}
201
             { type: 'sharedElement', fromId: 'title2', toId: 'title1', startDelay: 0, springVelocity: 0.2, duration: 0.5 }
196
             { type: 'sharedElement', fromId: 'title2', toId: 'title1', startDelay: 0, springVelocity: 0.2, duration: 0.5 }
202
           ],
197
           ],
203
           duration: 0.8
198
           duration: 0.8
204
-        } };
199
+        }
200
+      };
205
       uut.pop('theContainerId', options);
201
       uut.pop('theContainerId', options);
206
       expect(mockCommandsSender.pop).toHaveBeenCalledTimes(1);
202
       expect(mockCommandsSender.pop).toHaveBeenCalledTimes(1);
207
       expect(mockCommandsSender.pop).toHaveBeenCalledWith('theContainerId', options);
203
       expect(mockCommandsSender.pop).toHaveBeenCalledWith('theContainerId', options);
242
     });
238
     });
243
   });
239
   });
244
 
240
 
245
-  describe('showOverlay', () => {
241
+  xdescribe('showOverlay', () => {
246
     it('deep clones input to avoid mutation errors', () => {
242
     it('deep clones input to avoid mutation errors', () => {
247
       const obj = { title: 'test' };
243
       const obj = { title: 'test' };
248
       uut.showOverlay('alert', obj);
244
       uut.showOverlay('alert', obj);

+ 1
- 1
lib/src/commands/LayoutTreeCrawler.js View File

14
     node.id = this.uniqueIdProvider.generate(node.type);
14
     node.id = this.uniqueIdProvider.generate(node.type);
15
     node.data = node.data || {};
15
     node.data = node.data || {};
16
     node.children = node.children || [];
16
     node.children = node.children || [];
17
-    if ([LayoutTypes.Container, LayoutTypes.TopTab].includes(node.type)) {
17
+    if (_.isEqual(node.type, LayoutTypes.Component)) {
18
       this._handleContainer(node);
18
       this._handleContainer(node);
19
     }
19
     }
20
     _.forEach(node.children, this.crawl);
20
     _.forEach(node.children, this.crawl);

+ 16
- 16
lib/src/commands/LayoutTreeCrawler.test.js View File

13
   });
13
   });
14
 
14
 
15
   it('crawls a layout tree and adds unique id to each node', () => {
15
   it('crawls a layout tree and adds unique id to each node', () => {
16
-    const node = { type: LayoutTypes.ContainerStack, children: [{ type: LayoutTypes.BottomTabs }] };
16
+    const node = { type: LayoutTypes.Stack, children: [{ type: LayoutTypes.BottomTabs }] };
17
     uut.crawl(node);
17
     uut.crawl(node);
18
-    expect(node.id).toEqual('ContainerStack+UNIQUE_ID');
18
+    expect(node.id).toEqual('Stack+UNIQUE_ID');
19
     expect(node.children[0].id).toEqual('BottomTabs+UNIQUE_ID');
19
     expect(node.children[0].id).toEqual('BottomTabs+UNIQUE_ID');
20
   });
20
   });
21
 
21
 
22
   it('crawls a layout tree and ensures data exists', () => {
22
   it('crawls a layout tree and ensures data exists', () => {
23
-    const node = { type: LayoutTypes.ContainerStack, children: [{ type: LayoutTypes.BottomTabs }] };
23
+    const node = { type: LayoutTypes.Stack, children: [{ type: LayoutTypes.BottomTabs }] };
24
     uut.crawl(node);
24
     uut.crawl(node);
25
     expect(node.data).toEqual({});
25
     expect(node.data).toEqual({});
26
     expect(node.children[0].data).toEqual({});
26
     expect(node.children[0].data).toEqual({});
27
   });
27
   });
28
 
28
 
29
   it('crawls a layout tree and ensures children exists', () => {
29
   it('crawls a layout tree and ensures children exists', () => {
30
-    const node = { type: LayoutTypes.ContainerStack, children: [{ type: LayoutTypes.BottomTabs }] };
30
+    const node = { type: LayoutTypes.Stack, children: [{ type: LayoutTypes.BottomTabs }] };
31
     uut.crawl(node);
31
     uut.crawl(node);
32
     expect(node.children[0].children).toEqual([]);
32
     expect(node.children[0].children).toEqual([]);
33
   });
33
   });
34
 
34
 
35
   it('crawls a layout tree and asserts known layout type', () => {
35
   it('crawls a layout tree and asserts known layout type', () => {
36
-    const node = { type: LayoutTypes.ContainerStack, children: [{ type: 'Bob' }] };
36
+    const node = { type: LayoutTypes.Stack, children: [{ type: 'Bob' }] };
37
     expect(() => uut.crawl(node)).toThrow(new Error('Unknown layout type Bob'));
37
     expect(() => uut.crawl(node)).toThrow(new Error('Unknown layout type Bob'));
38
   });
38
   });
39
 
39
 
40
-  it('saves passProps into store for Container nodes', () => {
40
+  it('saves passProps into store for Component nodes', () => {
41
     const node = {
41
     const node = {
42
       type: LayoutTypes.BottomTabs, children: [
42
       type: LayoutTypes.BottomTabs, children: [
43
-        { type: LayoutTypes.Container, data: { name: 'the name', passProps: { myProp: 123 } } }]
43
+        { type: LayoutTypes.Component, data: { name: 'the name', passProps: { myProp: 123 } } }]
44
     };
44
     };
45
-    expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual({});
45
+    expect(store.getPropsForContainerId('Component+UNIQUE_ID')).toEqual({});
46
     uut.crawl(node);
46
     uut.crawl(node);
47
-    expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual({ myProp: 123 });
47
+    expect(store.getPropsForContainerId('Component+UNIQUE_ID')).toEqual({ myProp: 123 });
48
   });
48
   });
49
 
49
 
50
   it('Containers: injects navigationOptions from original container class static property', () => {
50
   it('Containers: injects navigationOptions from original container class static property', () => {
55
       }
55
       }
56
     };
56
     };
57
 
57
 
58
-    const node = { type: LayoutTypes.Container, data: { name: 'theContainerName' } };
58
+    const node = { type: LayoutTypes.Component, data: { name: 'theContainerName' } };
59
     store.setOriginalContainerClassForName('theContainerName', MyContainer);
59
     store.setOriginalContainerClassForName('theContainerName', MyContainer);
60
     uut.crawl(node);
60
     uut.crawl(node);
61
     expect(node.data.navigationOptions).toEqual(theStyle);
61
     expect(node.data.navigationOptions).toEqual(theStyle);
83
       }
83
       }
84
     };
84
     };
85
 
85
 
86
-    const node = { type: LayoutTypes.Container, data: { name: 'theContainerName', navigationOptions: passedOptions } };
86
+    const node = { type: LayoutTypes.Component, data: { name: 'theContainerName', navigationOptions: passedOptions } };
87
     store.setOriginalContainerClassForName('theContainerName', MyContainer);
87
     store.setOriginalContainerClassForName('theContainerName', MyContainer);
88
 
88
 
89
     uut.crawl(node);
89
     uut.crawl(node);
98
     });
98
     });
99
   });
99
   });
100
 
100
 
101
-  it('Containers: deepClones navigationOptions', () => {
101
+  it('Component: deepClones navigationOptions', () => {
102
     const theStyle = {};
102
     const theStyle = {};
103
     const MyContainer = class {
103
     const MyContainer = class {
104
       static get navigationOptions() {
104
       static get navigationOptions() {
106
       }
106
       }
107
     };
107
     };
108
 
108
 
109
-    const node = { type: LayoutTypes.Container, data: { name: 'theContainerName' } };
109
+    const node = { type: LayoutTypes.Component, data: { name: 'theContainerName' } };
110
     store.setOriginalContainerClassForName('theContainerName', MyContainer);
110
     store.setOriginalContainerClassForName('theContainerName', MyContainer);
111
     uut.crawl(node);
111
     uut.crawl(node);
112
     expect(node.data.navigationOptions).not.toBe(theStyle);
112
     expect(node.data.navigationOptions).not.toBe(theStyle);
113
   });
113
   });
114
 
114
 
115
   it('Containers: must contain data name', () => {
115
   it('Containers: must contain data name', () => {
116
-    const node = { type: LayoutTypes.Container, data: {} };
116
+    const node = { type: LayoutTypes.Component, data: {} };
117
     expect(() => uut.crawl(node)).toThrow(new Error('Missing container data.name'));
117
     expect(() => uut.crawl(node)).toThrow(new Error('Missing container data.name'));
118
   });
118
   });
119
 
119
 
120
   it('Containers: navigationOptions default obj', () => {
120
   it('Containers: navigationOptions default obj', () => {
121
     const MyContainer = class { };
121
     const MyContainer = class { };
122
 
122
 
123
-    const node = { type: LayoutTypes.Container, data: { name: 'theContainerName' } };
123
+    const node = { type: LayoutTypes.Component, data: { name: 'theContainerName' } };
124
     store.setOriginalContainerClassForName('theContainerName', MyContainer);
124
     store.setOriginalContainerClassForName('theContainerName', MyContainer);
125
     uut.crawl(node);
125
     uut.crawl(node);
126
     expect(node.data.navigationOptions).toEqual({});
126
     expect(node.data.navigationOptions).toEqual({});
132
 
132
 
133
     beforeEach(() => {
133
     beforeEach(() => {
134
       navigationOptions = {};
134
       navigationOptions = {};
135
-      node = { type: LayoutTypes.Container, data: { name: 'theContainerName', navigationOptions } };
135
+      node = { type: LayoutTypes.Component, data: { name: 'theContainerName', navigationOptions } };
136
     });
136
     });
137
 
137
 
138
     it('processes colors into numeric AARRGGBB', () => {
138
     it('processes colors into numeric AARRGGBB', () => {