Browse Source

parsing commands

Daniel Zlotin 8 years ago
parent
commit
eefbe6daa3

+ 2
- 3
src/Navigation.js View File

1
 import * as ContainerRegistry from './containers/ContainerRegistry';
1
 import * as ContainerRegistry from './containers/ContainerRegistry';
2
-import {NativeModules} from 'react-native';
3
-const {NativeNavigation} = NativeModules;
2
+import * as Commands from './commands/Commands';
4
 
3
 
5
 export function registerContainer(containerKey, getContainerFunc) {
4
 export function registerContainer(containerKey, getContainerFunc) {
6
   ContainerRegistry.registerContainer(containerKey, getContainerFunc);
5
   ContainerRegistry.registerContainer(containerKey, getContainerFunc);
7
 }
6
 }
8
 
7
 
9
 export function startApp(params) {
8
 export function startApp(params) {
10
-  NativeNavigation.startApp(params);
9
+  Commands.startApp(params);
11
 }
10
 }
12
 
11
 
13
 export function push(params) {
12
 export function push(params) {

+ 11
- 2
src/Navigation.test.js View File

2
 
2
 
3
 describe('Navigation', () => {
3
 describe('Navigation', () => {
4
   let Navigation;
4
   let Navigation;
5
-  let ContainerRegistry;
5
+  let ContainerRegistry, Commands;
6
 
6
 
7
   beforeEach(() => {
7
   beforeEach(() => {
8
     jest.mock('./containers/ContainerRegistry');
8
     jest.mock('./containers/ContainerRegistry');
9
+    jest.mock('./commands/Commands');
9
     ContainerRegistry = require('./containers/ContainerRegistry');
10
     ContainerRegistry = require('./containers/ContainerRegistry');
11
+    Commands = require('./commands/Commands');
10
     Navigation = require('./Navigation');
12
     Navigation = require('./Navigation');
11
   });
13
   });
12
 
14
 
28
     ], (f) => expect(f).toBeInstanceOf(Function));
30
     ], (f) => expect(f).toBeInstanceOf(Function));
29
   });
31
   });
30
 
32
 
31
-  it('delegates register container to container registry', () => {
33
+  it('registerContainer delegates to ContainerRegistry', () => {
32
     expect(ContainerRegistry.registerContainer).not.toHaveBeenCalled();
34
     expect(ContainerRegistry.registerContainer).not.toHaveBeenCalled();
33
     const fn = jest.fn();
35
     const fn = jest.fn();
34
     Navigation.registerContainer('key', fn);
36
     Navigation.registerContainer('key', fn);
35
     expect(ContainerRegistry.registerContainer).toHaveBeenCalledTimes(1);
37
     expect(ContainerRegistry.registerContainer).toHaveBeenCalledTimes(1);
36
     expect(ContainerRegistry.registerContainer).toHaveBeenCalledWith('key', fn);
38
     expect(ContainerRegistry.registerContainer).toHaveBeenCalledWith('key', fn);
37
   });
39
   });
40
+
41
+  it('startApp delegates to Commands', () => {
42
+    const params = {};
43
+    Navigation.startApp(params);
44
+    expect(Commands.startApp).toHaveBeenCalledTimes(1);
45
+    expect(Commands.startApp).toHaveBeenCalledWith(params);
46
+  });
38
 });
47
 });

+ 43
- 13
src/commands/Commands.js View File

1
 import _ from 'lodash';
1
 import _ from 'lodash';
2
+import {NativeModules} from 'react-native';
3
+const {NativeNavigation} = NativeModules;
4
+import {uniqueId} from '../providers/UniqueIdProvider';
2
 
5
 
3
 export function startApp(params) {
6
 export function startApp(params) {
4
-  validateParams(params);
7
+  NativeNavigation.startApp();
5
 }
8
 }
9
+//
10
+//function parseParams(params) {
11
+//  const msg = `invalid params passed to startApp: ${params}`;
12
+//  if (!params) {
13
+//    throw new Error(msg);
14
+//  }
15
+//
16
+//  if (params.tabs) {
17
+//    return parseTabs(params);
18
+//  }
19
+//
20
+//  if (params.container) {
21
+//    return parseContainer(params);
22
+//  }
23
+//
24
+//  throw new Error(msg);
25
+//}
26
+//
27
+//function parseTabs(params) {
28
+////&& params.tabs.length > 0 && _.every(params.tabs, (t) => t.containerKey)
29
+//}
30
+//
31
+//function parseContainer(params) {
32
+//
33
+//}
6
 
34
 
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.containerKey) {
13
-    return true;
14
-  }
15
-  if (params.tabs && params.tabs.length > 0 && _.every(params.tabs, (t) => t.containerKey)) {
16
-    return true;
17
-  }
18
-  throw new Error(msg);
35
+export function parse(params) {
36
+  return {
37
+    containerStack: {
38
+      id: uniqueId(`containerStack`),
39
+      stack: [
40
+        {
41
+          container: {
42
+            id: uniqueId(`container`),
43
+            key: params.container.key
44
+          }
45
+        }
46
+      ]
47
+    }
48
+  };
19
 }
49
 }

+ 55
- 16
src/commands/Commands.test.js View File

1
 describe('Commands', () => {
1
 describe('Commands', () => {
2
   let uut;
2
   let uut;
3
+  let mockNativeNavigation;
4
+  let mockUniqueIdProvider;
3
 
5
 
4
   beforeEach(() => {
6
   beforeEach(() => {
7
+    mockNativeNavigation = {
8
+      startApp: jest.fn()
9
+    };
10
+    require('react-native').NativeModules.NativeNavigation = mockNativeNavigation;
11
+    jest.mock('../providers/UniqueIdProvider');
12
+    mockUniqueIdProvider = require('../providers/UniqueIdProvider');
5
     uut = require('./Commands');
13
     uut = require('./Commands');
6
   });
14
   });
7
 
15
 
8
   describe('startApp', () => {
16
   describe('startApp', () => {
9
-    it('receives params object', () => {
17
+    it('sends startApp to native', () => {
10
       uut.startApp({
18
       uut.startApp({
11
-        containerKey: {
12
-          root: 'example.MyContainer'
13
-        },
14
-        drawer: {
15
-          left: {
16
-            root: 'example.SideMenu'
17
-          }
19
+        container: {
20
+          key: 'example.MyContainer'
18
         }
21
         }
19
       });
22
       });
23
+      expect(mockNativeNavigation.startApp).toHaveBeenCalledTimes(1);
20
     });
24
     });
21
 
25
 
22
-    it('expects to get containerKey, or tabs with containerKeys', () => {
23
-      expect(() => uut.startApp({containerKey: 'example.MyContainer'})).not.toThrow();
24
-      expect(() => uut.startApp({tabs: [{containerKey: '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: [{containerKey: 'example.Tab1'}, {}]})).toThrow();
26
+    it('parses the params and construct single screen hirarchy', () => {
27
+      mockUniqueIdProvider.uniqueId = jest.fn((prefix) => `${prefix}123`);
28
+
29
+      expect(uut.parse(
30
+        {
31
+          container: {
32
+            key: 'com.example.MyScreen'
33
+          }
34
+        })).toEqual(
35
+        {
36
+          containerStack: {
37
+            id: 'containerStack123',
38
+            stack: [
39
+              {
40
+                container: {
41
+                  key: 'com.example.MyScreen',
42
+                  id: 'container123'
43
+                }
44
+              }
45
+            ]
46
+          }
47
+        });
30
     });
48
     });
49
+    //it('receives params object', () => {
50
+    //  uut.startApp({
51
+    //    container: {
52
+    //      key: 'example.MyContainer'
53
+    //    },
54
+    //    drawer: {
55
+    //      left: {
56
+    //        key: 'example.SideMenu'
57
+    //      }
58
+    //    }
59
+    //  });
60
+    //});
61
+    //it('expects to get containerKey, or tabs with containerKeys', () => {
62
+    //  expect(() => uut.startApp({containerKey: 'example.MyContainer'})).not.toThrow();
63
+    //  expect(() => uut.startApp({tabs: [{containerKey: 'example.Tab1'}]})).not.toThrow();
64
+    //  expect(() => uut.startApp()).toThrow();
65
+    //  expect(() => uut.startApp({})).toThrow();
66
+    //  expect(() => uut.startApp({tabs: []})).toThrow();
67
+    //  expect(() => uut.startApp({tabs: [{}]})).toThrow();
68
+    //  expect(() => uut.startApp({tabs: [{containerKey: 'example.Tab1'}, {}]})).toThrow();
69
+    //});
31
   });
70
   });
32
 });
71
 });

+ 5
- 0
src/providers/UniqueIdProvider.js View File

1
+import _ from 'lodash';
2
+
3
+export function uniqueId(prefix) {
4
+  return _.uniqueId(prefix);
5
+}

+ 18
- 0
src/providers/UniqueIdProvider.test.js View File

1
+describe('UniqueIdProvider', () => {
2
+  let uut;
3
+
4
+  beforeEach(() => {
5
+    uut = require('./UniqueIdProvider');
6
+  });
7
+
8
+  it('provides uniqueId', () => {
9
+    expect(uut.uniqueId()).toEqual('1');
10
+    expect(uut.uniqueId()).toEqual('2');
11
+  });
12
+
13
+  it('provides with prefix', () => {
14
+    expect(uut.uniqueId('prefix')).toEqual('prefix1');
15
+    expect(uut.uniqueId('prefix')).toEqual('prefix2');
16
+    expect(uut.uniqueId('other')).toEqual('other3');
17
+  });
18
+});