Browse Source

starting work on commands

Daniel Zlotin 7 years ago
parent
commit
0f55163293

+ 4
- 2
src2/Navigation.js View File

@@ -1,5 +1,7 @@
1
-export function registerContainer(params) {
2
-  //
1
+import * as ContainerRegistry from './containers/ContainerRegistry';
2
+
3
+export function registerContainer(containerKey, getContainerFunc) {
4
+  ContainerRegistry.registerContainer(containerKey, getContainerFunc);
3 5
 }
4 6
 
5 7
 export function startApp(params) {

+ 13
- 0
src2/Navigation.test.js View File

@@ -2,9 +2,14 @@ import _ from 'lodash';
2 2
 
3 3
 describe('Navigation', () => {
4 4
   let Navigation;
5
+  let Commands;
6
+  let ContainerRegistry;
5 7
 
6 8
   beforeEach(() => {
9
+    jest.mock('./containers/ContainerRegistry');
7 10
     Navigation = require('./Navigation');
11
+    Commands = require('./commands/Commands');
12
+    ContainerRegistry = require('./containers/ContainerRegistry');
8 13
   });
9 14
 
10 15
   it('exposes static commands', () => {
@@ -24,4 +29,12 @@ describe('Navigation', () => {
24 29
       Navigation.dismissInAppNotification
25 30
     ], (f) => expect(f).toBeInstanceOf(Function));
26 31
   });
32
+
33
+  it('delegates register container to container registry', () => {
34
+    expect(ContainerRegistry.registerContainer).not.toHaveBeenCalled();
35
+    const fn = jest.fn();
36
+    Navigation.registerContainer('key', fn);
37
+    expect(ContainerRegistry.registerContainer).toHaveBeenCalledTimes(1);
38
+    expect(ContainerRegistry.registerContainer).toHaveBeenCalledWith('key', fn);
39
+  });
27 40
 });

+ 19
- 0
src2/commands/Commands.js View File

@@ -0,0 +1,19 @@
1
+import _ from 'lodash';
2
+
3
+export function startApp(params) {
4
+  validateParams(params);
5
+}
6
+
7
+function validateParams(params) {
8
+  const msg = `invalid params passed to startApp: ${params}`;
9
+  if (!params) {
10
+    throw new Error(msg);
11
+  }
12
+  if (params.screenKey) {
13
+    return true;
14
+  }
15
+  if (params.tabs && params.tabs.length > 0 && _.every(params.tabs, (t) => t.screenKey)) {
16
+    return true;
17
+  }
18
+  throw new Error(msg);
19
+}

+ 32
- 0
src2/commands/Commands.test.js View File

@@ -0,0 +1,32 @@
1
+describe('Commands', () => {
2
+  let uut;
3
+
4
+  beforeEach(() => {
5
+    uut = require('./Commands');
6
+  });
7
+
8
+  describe('startApp', () => {
9
+    it('receives params object', () => {
10
+      uut.startApp({
11
+        screenKey: {
12
+          screen: 'example.MyScreen'
13
+        },
14
+        drawer: {
15
+          left: {
16
+            screen: 'example.SideMenu'
17
+          }
18
+        }
19
+      });
20
+    });
21
+
22
+    it('expects to get screenKey, or tabs with screenKeys', () => {
23
+      expect(() => uut.startApp({screenKey: 'example.MyScreen'})).not.toThrow();
24
+      expect(() => uut.startApp({tabs: [{screenKey: 'example.Tab1'}]})).not.toThrow();
25
+      expect(() => uut.startApp()).toThrow();
26
+      expect(() => uut.startApp({})).toThrow();
27
+      expect(() => uut.startApp({tabs: []})).toThrow();
28
+      expect(() => uut.startApp({tabs: [{}]})).toThrow();
29
+      expect(() => uut.startApp({tabs: [{screenKey: 'example.Tab1'}, {}]})).toThrow();
30
+    });
31
+  });
32
+});

+ 0
- 4
src2/containers/ContainerRegistry.js View File

@@ -10,10 +10,6 @@ export function registerContainer(containerKey, getContainerFunc) {
10 10
   AppRegistry.registerComponent(containerKey, () => NavigationContainer);
11 11
 }
12 12
 
13
-export function getRegisteredContainer(containerKey) {
14
-  return ContainerStore.getContainerClass(containerKey);
15
-}
16
-
17 13
 function wrapContainer(containerKey, OriginalContainer) {
18 14
   return class extends Component {
19 15
     constructor(props) {

+ 16
- 11
src2/containers/ContainerRegistry.test.js View File

@@ -6,6 +6,7 @@ describe('ComponentRegistry', () => {
6 6
   let uut;
7 7
   let myContainerRef;
8 8
   let testParentRef;
9
+  let containerStore;
9 10
 
10 11
   class MyContainer extends Component {
11 12
     constructor(props) {
@@ -36,6 +37,7 @@ describe('ComponentRegistry', () => {
36 37
 
37 38
   beforeEach(() => {
38 39
     uut = require('./ContainerRegistry');
40
+    containerStore = require('./ContainerStore');
39 41
   });
40 42
 
41 43
   afterEach(() => {
@@ -44,8 +46,11 @@ describe('ComponentRegistry', () => {
44 46
   });
45 47
 
46 48
   describe('registerContainer', () => {
47
-    it('registers container component by containerKey into AppRegistry', () => {
49
+    beforeEach(() => {
48 50
       AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
51
+    });
52
+
53
+    it('registers container component by containerKey into AppRegistry', () => {
49 54
       expect(AppRegistry.registerComponent).not.toHaveBeenCalled();
50 55
       uut.registerContainer('example.MyContainer.key', () => MyContainer);
51 56
       expect(AppRegistry.registerComponent).toHaveBeenCalledTimes(1);
@@ -53,7 +58,6 @@ describe('ComponentRegistry', () => {
53 58
     });
54 59
 
55 60
     it('resulting in a normal component', () => {
56
-      AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
57 61
       uut.registerContainer('example.MyContainer.key', () => MyContainer);
58 62
       const Container = AppRegistry.registerComponent.mock.calls[0][1]();
59 63
       const tree = renderer.create(<Container screenId="123"/>);
@@ -61,35 +65,36 @@ describe('ComponentRegistry', () => {
61 65
     });
62 66
   });
63 67
 
64
-  describe('wrapping NavigationContainer', () => {
68
+  describe('NavigationContainer wrapping', () => {
65 69
     const containerKey = 'example.MyContainer';
66 70
 
67 71
     beforeEach(() => {
68 72
       uut.registerContainer(containerKey, () => MyContainer);
69 73
     });
70 74
 
71
-    it('asserts has screenId as prop', () => {
72
-      const NavigationContainer = uut.getRegisteredContainer(containerKey);
75
+    it('must have screenId as prop', () => {
76
+      const NavigationContainer = containerStore.getContainerClass(containerKey);
73 77
       expect(() => {
74 78
         renderer.create(<NavigationContainer/>);
75 79
       }).toThrow(new Error('Screen example.MyContainer does not have a screenId!'));
76 80
     });
77 81
 
78 82
     it('wraps the container and saves to store', () => {
79
-      const NavigationContainer = uut.getRegisteredContainer(containerKey);
83
+      const NavigationContainer = containerStore.getContainerClass(containerKey);
84
+      expect(NavigationContainer).not.toBeInstanceOf(MyContainer);
80 85
       const tree = renderer.create(<NavigationContainer screenId={'screen1'}/>);
81 86
       expect(tree.toJSON().children).toEqual(['Hello, World!']);
82 87
       expect(myContainerRef).toBeInstanceOf(MyContainer);
83 88
     });
84 89
 
85 90
     it('injects props from wrapper into original container', () => {
86
-      const NavigationContainer = uut.getRegisteredContainer(containerKey);
91
+      const NavigationContainer = containerStore.getContainerClass(containerKey);
87 92
       renderer.create(<NavigationContainer screenId={'screen1'} myProp={'yo'}/>);
88 93
       expect(myContainerRef.props.myProp).toEqual('yo');
89 94
     });
90 95
 
91 96
     it('updates props from wrapper into original container', () => {
92
-      const NavigationContainer = uut.getRegisteredContainer(containerKey);
97
+      const NavigationContainer = containerStore.getContainerClass(containerKey);
93 98
       renderer.create(<TestParent ChildClass={NavigationContainer}/>);
94 99
       expect(myContainerRef.props.foo).toEqual(undefined);
95 100
       testParentRef.setState({propsFromState: {foo: 'yo'}});
@@ -98,13 +103,13 @@ describe('ComponentRegistry', () => {
98 103
 
99 104
     it('pulls props from the PropsStore and injects them into the inner container', () => {
100 105
       require('./PropsStore').setPropsForScreenId('screen123', {numberProp: 1, stringProp: 'hello', objectProp: {a: 2}});
101
-      const NavigationContainer = uut.getRegisteredContainer(containerKey);
106
+      const NavigationContainer = containerStore.getContainerClass(containerKey);
102 107
       renderer.create(<NavigationContainer screenId={'screen123'}/>);
103 108
       expect(myContainerRef.props).toEqual({screenId: 'screen123', numberProp: 1, stringProp: 'hello', objectProp: {a: 2}});
104 109
     });
105 110
 
106 111
     it('updates props from PropsStore into inner container', () => {
107
-      const NavigationContainer = uut.getRegisteredContainer(containerKey);
112
+      const NavigationContainer = containerStore.getContainerClass(containerKey);
108 113
       renderer.create(<TestParent ChildClass={NavigationContainer}/>);
109 114
       require('./PropsStore').setPropsForScreenId('screen1', {myProp: 'hello'});
110 115
       expect(myContainerRef.props.foo).toEqual(undefined);
@@ -115,7 +120,7 @@ describe('ComponentRegistry', () => {
115 120
     });
116 121
 
117 122
     it('protects screenId from change', () => {
118
-      const NavigationContainer = uut.getRegisteredContainer(containerKey);
123
+      const NavigationContainer = containerStore.getContainerClass(containerKey);
119 124
       renderer.create(<TestParent ChildClass={NavigationContainer}/>);
120 125
       expect(myContainerRef.props.screenId).toEqual('screen1');
121 126
       testParentRef.setState({propsFromState: {screenId: 'ERROR'}});