Browse Source

renamed to screens

Daniel Zlotin 7 years ago
parent
commit
f62a3aee9e

+ 3
- 3
src2/Navigation.js View File

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

+ 9
- 9
src2/Navigation.test.js View File

@@ -3,18 +3,18 @@ import _ from 'lodash';
3 3
 describe('Navigation', () => {
4 4
   let Navigation;
5 5
   let Commands;
6
-  let ContainerRegistry;
6
+  let ScreenRegistry;
7 7
 
8 8
   beforeEach(() => {
9
-    jest.mock('./containers/ContainerRegistry');
9
+    jest.mock('./screens/ScreenRegistry');
10 10
     Navigation = require('./Navigation');
11 11
     Commands = require('./commands/Commands');
12
-    ContainerRegistry = require('./containers/ContainerRegistry');
12
+    ScreenRegistry = require('./screens/ScreenRegistry');
13 13
   });
14 14
 
15 15
   it('exposes static commands', () => {
16 16
     _.forEach([
17
-      Navigation.registerContainer,
17
+      Navigation.registerScreen,
18 18
       Navigation.startApp,
19 19
       Navigation.push,
20 20
       Navigation.pop,
@@ -30,11 +30,11 @@ describe('Navigation', () => {
30 30
     ], (f) => expect(f).toBeInstanceOf(Function));
31 31
   });
32 32
 
33
-  it('delegates register container to container registry', () => {
34
-    expect(ContainerRegistry.registerContainer).not.toHaveBeenCalled();
33
+  it('delegates register screen to screen registry', () => {
34
+    expect(ScreenRegistry.registerScreen).not.toHaveBeenCalled();
35 35
     const fn = jest.fn();
36
-    Navigation.registerContainer('key', fn);
37
-    expect(ContainerRegistry.registerContainer).toHaveBeenCalledTimes(1);
38
-    expect(ContainerRegistry.registerContainer).toHaveBeenCalledWith('key', fn);
36
+    Navigation.registerScreen('key', fn);
37
+    expect(ScreenRegistry.registerScreen).toHaveBeenCalledTimes(1);
38
+    expect(ScreenRegistry.registerScreen).toHaveBeenCalledWith('key', fn);
39 39
   });
40 40
 });

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

@@ -1,38 +0,0 @@
1
-import React, {Component} from 'react';
2
-import {AppRegistry} from 'react-native';
3
-import * as PropsStore from './PropsStore';
4
-import * as ContainerStore from './ContainerStore';
5
-
6
-export function registerContainer(containerKey, getContainerFunc) {
7
-  const OriginalContainer = getContainerFunc();
8
-  const NavigationContainer = wrapContainer(containerKey, OriginalContainer);
9
-  ContainerStore.saveContainerClass(containerKey, NavigationContainer);
10
-  AppRegistry.registerComponent(containerKey, () => NavigationContainer);
11
-}
12
-
13
-function wrapContainer(containerKey, OriginalContainer) {
14
-  return class extends Component {
15
-    constructor(props) {
16
-      super(props);
17
-      if (!props.screenId) {
18
-        throw new Error(`Screen ${containerKey} does not have a screenId!`);
19
-      }
20
-      this.state = {
21
-        screenId: props.screenId,
22
-        allProps: {...props, ...PropsStore.getPropsForScreenId(props.screenId)}
23
-      };
24
-    }
25
-
26
-    componentWillReceiveProps(nextProps) {
27
-      this.setState({
28
-        allProps: {...nextProps, ...PropsStore.getPropsForScreenId(this.state.screenId)}
29
-      });
30
-    }
31
-
32
-    render() {
33
-      return (
34
-        <OriginalContainer {...this.state.allProps} screenId={this.state.screenId}/>
35
-      );
36
-    }
37
-  };
38
-}

+ 0
- 130
src2/containers/ContainerRegistry.test.js View File

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

+ 0
- 11
src2/containers/ContainerStore.js View File

@@ -1,11 +0,0 @@
1
-const state = {
2
-  containersByKey: {}
3
-};
4
-
5
-export function saveContainerClass(containerKey, containerClass) {
6
-  state.containersByKey[containerKey] = containerClass;
7
-}
8
-
9
-export function getContainerClass(containerKey) {
10
-  return state.containersByKey[containerKey];
11
-}

+ 0
- 19
src2/containers/ContainerStore.test.js View File

@@ -1,19 +0,0 @@
1
-describe('ContainerStore', () => {
2
-  let uut;
3
-
4
-  beforeEach(() => {
5
-    uut = require('./ContainerStore');
6
-  });
7
-
8
-  class Component {
9
-    //
10
-  }
11
-  class MyComponent extends Component {
12
-    //
13
-  }
14
-
15
-  it('holds containers classes by containerKey', () => {
16
-    uut.saveContainerClass('example.mykey', MyComponent);
17
-    expect(uut.getContainerClass('example.mykey')).toEqual(MyComponent);
18
-  });
19
-});

src2/containers/PropsStore.js → src2/screens/PropsStore.js View File


src2/containers/PropsStore.test.js → src2/screens/PropsStore.test.js View File


+ 44
- 0
src2/screens/ScreenRegistry.js View File

@@ -0,0 +1,44 @@
1
+import React, {Component} from 'react';
2
+import {AppRegistry} from 'react-native';
3
+import * as PropsStore from './PropsStore';
4
+import * as ScreenStore from './ScreenStore';
5
+
6
+export function registerScreen(screenKey, getScreenFunc) {
7
+  const OriginalScreen = getScreenFunc();
8
+  const NavigationScreen = wrapScreen(screenKey, OriginalScreen);
9
+  ScreenStore.saveScreenClass(screenKey, NavigationScreen);
10
+  AppRegistry.registerComponent(screenKey, () => NavigationScreen);
11
+}
12
+
13
+export const bla = {};
14
+
15
+function wrapScreen(screenKey, OriginalScreen) {
16
+  return class extends Component {
17
+    constructor(props) {
18
+      super(props);
19
+      if (!props.screenId) {
20
+        throw new Error(`Screen ${screenKey} does not have a screenId!`);
21
+      }
22
+      this.state = {
23
+        screenId: props.screenId,
24
+        allProps: {...props, ...PropsStore.getPropsForScreenId(props.screenId)}
25
+      };
26
+    }
27
+
28
+    componentWillReceiveProps(nextProps) {
29
+      this.setState({
30
+        allProps: {...nextProps, ...PropsStore.getPropsForScreenId(this.state.screenId)}
31
+      });
32
+    }
33
+
34
+    render() {
35
+      return (
36
+        <OriginalScreen
37
+          ref={(r) => bla.ref = r}
38
+          {...this.state.allProps}
39
+          screenId={this.state.screenId}
40
+        />
41
+      );
42
+    }
43
+  };
44
+}

+ 130
- 0
src2/screens/ScreenRegistry.test.js View File

@@ -0,0 +1,130 @@
1
+import {AppRegistry, Text} from 'react-native';
2
+import React, {Component} from 'react';
3
+import renderer from 'react-test-renderer';
4
+
5
+describe('ComponentRegistry', () => {
6
+  let uut;
7
+  let myScreenRef;
8
+  let testParentRef;
9
+  let screenStore;
10
+
11
+  class MyScreen extends Component {
12
+    constructor(props) {
13
+      super(props);
14
+      myScreenRef = this; //eslint-disable-line
15
+    }
16
+
17
+    render() {
18
+      return <Text>{'Hello, World!'}</Text>;
19
+    }
20
+  }
21
+
22
+  class TestParent extends Component { //eslint-disable-line
23
+    constructor(props) {
24
+      super(props);
25
+      testParentRef = this; //eslint-disable-line
26
+      this.ChildClass = props.ChildClass;
27
+      this.state = {propsFromState: {}};
28
+    }
29
+
30
+    render() {
31
+      const Child = this.ChildClass;
32
+      return (
33
+        <Child screenId="screen1" {...this.state.propsFromState}/>
34
+      );
35
+    }
36
+  }
37
+
38
+  beforeEach(() => {
39
+    uut = require('./ScreenRegistry');
40
+    screenStore = require('./ScreenStore');
41
+  });
42
+
43
+  afterEach(() => {
44
+    myScreenRef = null;
45
+    testParentRef = null;
46
+  });
47
+
48
+  describe('registerScreen', () => {
49
+    beforeEach(() => {
50
+      AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
51
+    });
52
+
53
+    it('registers screen component by screenKey into AppRegistry', () => {
54
+      expect(AppRegistry.registerComponent).not.toHaveBeenCalled();
55
+      uut.registerScreen('example.MyScreen.key', () => MyScreen);
56
+      expect(AppRegistry.registerComponent).toHaveBeenCalledTimes(1);
57
+      expect(AppRegistry.registerComponent.mock.calls[0][0]).toEqual('example.MyScreen.key');
58
+    });
59
+
60
+    it('resulting in a normal component', () => {
61
+      uut.registerScreen('example.MyScreen.key', () => MyScreen);
62
+      const Screen = AppRegistry.registerComponent.mock.calls[0][1]();
63
+      const tree = renderer.create(<Screen screenId="123"/>);
64
+      expect(tree.toJSON().children).toEqual(['Hello, World!']);
65
+    });
66
+  });
67
+
68
+  describe('NavigationScreen wrapping', () => {
69
+    const screenKey = 'example.MyScreen';
70
+
71
+    beforeEach(() => {
72
+      uut.registerScreen(screenKey, () => MyScreen);
73
+    });
74
+
75
+    it('must have screenId as prop', () => {
76
+      const NavigationScreen = screenStore.getScreenClass(screenKey);
77
+      expect(() => {
78
+        renderer.create(<NavigationScreen/>);
79
+      }).toThrow(new Error('Screen example.MyScreen does not have a screenId!'));
80
+    });
81
+
82
+    it('wraps the screen and saves to store', () => {
83
+      const NavigationScreen = screenStore.getScreenClass(screenKey);
84
+      expect(NavigationScreen).not.toBeInstanceOf(MyScreen);
85
+      const tree = renderer.create(<NavigationScreen screenId={'screen1'}/>);
86
+      expect(tree.toJSON().children).toEqual(['Hello, World!']);
87
+      expect(myScreenRef).toBeInstanceOf(MyScreen);
88
+    });
89
+
90
+    it('injects props from wrapper into original screen', () => {
91
+      const NavigationScreen = screenStore.getScreenClass(screenKey);
92
+      renderer.create(<NavigationScreen screenId={'screen1'} myProp={'yo'}/>);
93
+      expect(myScreenRef.props.myProp).toEqual('yo');
94
+    });
95
+
96
+    it('updates props from wrapper into original screen', () => {
97
+      const NavigationScreen = screenStore.getScreenClass(screenKey);
98
+      renderer.create(<TestParent ChildClass={NavigationScreen}/>);
99
+      expect(myScreenRef.props.foo).toEqual(undefined);
100
+      testParentRef.setState({propsFromState: {foo: 'yo'}});
101
+      expect(myScreenRef.props.foo).toEqual('yo');
102
+    });
103
+
104
+    it('pulls props from the PropsStore and injects them into the inner screen', () => {
105
+      require('./PropsStore').setPropsForScreenId('screen123', {numberProp: 1, stringProp: 'hello', objectProp: {a: 2}});
106
+      const NavigationScreen = screenStore.getScreenClass(screenKey);
107
+      renderer.create(<NavigationScreen screenId={'screen123'}/>);
108
+      expect(myScreenRef.props).toEqual({screenId: 'screen123', numberProp: 1, stringProp: 'hello', objectProp: {a: 2}});
109
+    });
110
+
111
+    it('updates props from PropsStore into inner screen', () => {
112
+      const NavigationScreen = screenStore.getScreenClass(screenKey);
113
+      renderer.create(<TestParent ChildClass={NavigationScreen}/>);
114
+      require('./PropsStore').setPropsForScreenId('screen1', {myProp: 'hello'});
115
+      expect(myScreenRef.props.foo).toEqual(undefined);
116
+      expect(myScreenRef.props.myProp).toEqual(undefined);
117
+      testParentRef.setState({propsFromState: {foo: 'yo'}});
118
+      expect(myScreenRef.props.foo).toEqual('yo');
119
+      expect(myScreenRef.props.myProp).toEqual('hello');
120
+    });
121
+
122
+    it('protects screenId from change', () => {
123
+      const NavigationScreen = screenStore.getScreenClass(screenKey);
124
+      renderer.create(<TestParent ChildClass={NavigationScreen}/>);
125
+      expect(myScreenRef.props.screenId).toEqual('screen1');
126
+      testParentRef.setState({propsFromState: {screenId: 'ERROR'}});
127
+      expect(myScreenRef.props.screenId).toEqual('screen1');
128
+    });
129
+  });
130
+});

+ 11
- 0
src2/screens/ScreenStore.js View File

@@ -0,0 +1,11 @@
1
+const state = {
2
+  screensByKey: {}
3
+};
4
+
5
+export function saveScreenClass(screenKey, ScreenClass) {
6
+  state.screensByKey[screenKey] = ScreenClass;
7
+}
8
+
9
+export function getScreenClass(screenKey) {
10
+  return state.screensByKey[screenKey];
11
+}

+ 19
- 0
src2/screens/ScreenStore.test.js View File

@@ -0,0 +1,19 @@
1
+describe('ScreenStore', () => {
2
+  let uut;
3
+
4
+  beforeEach(() => {
5
+    uut = require('./ScreenStore');
6
+  });
7
+
8
+  class Component {
9
+    //
10
+  }
11
+  class MyComponent extends Component {
12
+    //
13
+  }
14
+
15
+  it('holds screens classes by screenKey', () => {
16
+    uut.saveScreenClass('example.mykey', MyComponent);
17
+    expect(uut.getScreenClass('example.mykey')).toEqual(MyComponent);
18
+  });
19
+});