Browse Source

starting work on commands

Daniel Zlotin 8 years ago
parent
commit
0f55163293

+ 4
- 2
src2/Navigation.js View File

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
 export function startApp(params) {
7
 export function startApp(params) {

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

2
 
2
 
3
 describe('Navigation', () => {
3
 describe('Navigation', () => {
4
   let Navigation;
4
   let Navigation;
5
+  let Commands;
6
+  let ContainerRegistry;
5
 
7
 
6
   beforeEach(() => {
8
   beforeEach(() => {
9
+    jest.mock('./containers/ContainerRegistry');
7
     Navigation = require('./Navigation');
10
     Navigation = require('./Navigation');
11
+    Commands = require('./commands/Commands');
12
+    ContainerRegistry = require('./containers/ContainerRegistry');
8
   });
13
   });
9
 
14
 
10
   it('exposes static commands', () => {
15
   it('exposes static commands', () => {
24
       Navigation.dismissInAppNotification
29
       Navigation.dismissInAppNotification
25
     ], (f) => expect(f).toBeInstanceOf(Function));
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

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

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
   AppRegistry.registerComponent(containerKey, () => NavigationContainer);
10
   AppRegistry.registerComponent(containerKey, () => NavigationContainer);
11
 }
11
 }
12
 
12
 
13
-export function getRegisteredContainer(containerKey) {
14
-  return ContainerStore.getContainerClass(containerKey);
15
-}
16
-
17
 function wrapContainer(containerKey, OriginalContainer) {
13
 function wrapContainer(containerKey, OriginalContainer) {
18
   return class extends Component {
14
   return class extends Component {
19
     constructor(props) {
15
     constructor(props) {

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

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