Browse Source

containers

Daniel Zlotin 8 years ago
parent
commit
2f0aa1e8f4

+ 5
- 2
src2/containers/ContainerRegistry.js View File

@@ -3,14 +3,17 @@ import {AppRegistry} from 'react-native';
3 3
 
4 4
 export function registerContainer(containerKey, getContainerFunc) {
5 5
   const OriginalContainer = getContainerFunc();
6
-  const NavigationContainer = wrapContainer(OriginalContainer);
6
+  const NavigationContainer = wrapContainer(containerKey, OriginalContainer);
7 7
   AppRegistry.registerComponent(containerKey, () => NavigationContainer);
8 8
 }
9 9
 
10
-function wrapContainer(OriginalContainer) {
10
+function wrapContainer(containerKey, OriginalContainer) {
11 11
   return class extends Component {
12 12
     constructor(props) {
13 13
       super(props);
14
+      if (!props.screenId) {
15
+        throw new Error(`Screen ${containerKey} does not have a screenId!`);
16
+      }
14 17
       this.state = {
15 18
         allProps: {...props}
16 19
       };

+ 63
- 54
src2/containers/ContainerRegistry.test.js View File

@@ -20,63 +20,72 @@ describe('ComponentRegistry', () => {
20 20
     uut = require('./ContainerRegistry');
21 21
   });
22 22
 
23
-  function getRegisteredComponentClassFromAppRegistry() {
24
-    return AppRegistry.registerComponent.mock.calls[0][1]();
25
-  }
26
-
27
-  it('registers container component by containerKey into AppRegistry', () => {
28
-    expect(AppRegistry.registerComponent).not.toHaveBeenCalled();
29
-
30
-    uut.registerContainer('example.MyContainer.key', () => MyContainer);
31
-
32
-    expect(AppRegistry.registerComponent).toHaveBeenCalledTimes(1);
33
-    expect(AppRegistry.registerComponent.mock.calls[0][0]).toEqual('example.MyContainer.key');
34
-  });
35
-
36
-  it('wraps the container', () => {
37
-    uut.registerContainer('example.MyContainer', () => MyContainer);
38
-
39
-    const NavigationContainer = getRegisteredComponentClassFromAppRegistry();
40
-    const tree = renderer.create(
41
-      <NavigationContainer/>
42
-    );
43
-    expect(tree.toJSON().children).toEqual(['Hello, World!']);
44
-  });
45
-
46
-  it('passes props from wrapper into original container', () => {
47
-    uut.registerContainer('example.MyContainer', () => MyContainer);
48
-
49
-    const NavigationContainer = getRegisteredComponentClassFromAppRegistry();
50
-    const tree = renderer.create(
51
-      <NavigationContainer name="Daniel"/>
52
-    );
53
-    expect(tree.toJSON().children).toEqual(['Hello, Daniel!']);
54
-  });
55
-
56
-  it('updates props into original container', () => {
57
-    uut.registerContainer('example.MyContainer', () => MyContainer);
58
-
59
-    const NavigationContainer = getRegisteredComponentClassFromAppRegistry();
60
-    let testParentRef = null;
61
-    class TestParent extends Component { //eslint-disable-line
62
-      constructor(props) {
63
-        super(props);
64
-        this.state = {};
65
-      }
23
+  describe('registerContainer', () => {
24
+    function getRegisteredComponentClassFromAppRegistry() {
25
+      return AppRegistry.registerComponent.mock.calls[0][1]();
26
+    }
66 27
 
67
-      render() {
68
-        return (
69
-          <NavigationContainer name={this.state.name}/>
70
-        );
71
-      }
28
+    function renderRegisteredContainer(props) {
29
+      const Container = getRegisteredComponentClassFromAppRegistry();
30
+      return renderer.create(
31
+        <Container screenId="screen1" {...props}/>
32
+      );
72 33
     }
73 34
 
74
-    const tree = renderer.create(
75
-      <TestParent ref={(r) => testParentRef = r}/>
76
-    );
35
+    it('registers container component by containerKey into AppRegistry', () => {
36
+      expect(AppRegistry.registerComponent).not.toHaveBeenCalled();
37
+
38
+      uut.registerContainer('example.MyContainer.key', () => MyContainer);
39
+
40
+      expect(AppRegistry.registerComponent).toHaveBeenCalledTimes(1);
41
+      expect(AppRegistry.registerComponent.mock.calls[0][0]).toEqual('example.MyContainer.key');
42
+    });
43
+
44
+    it('wraps the container', () => {
45
+      uut.registerContainer('example.MyContainer', () => MyContainer);
46
+      const tree = renderRegisteredContainer();
47
+      expect(tree.toJSON().children).toEqual(['Hello, World!']);
48
+    });
49
+
50
+    it('passes props from wrapper into original container', () => {
51
+      uut.registerContainer('example.MyContainer', () => MyContainer);
52
+      const tree = renderRegisteredContainer({name: 'Daniel'});
53
+      expect(tree.toJSON().children).toEqual(['Hello, Daniel!']);
54
+    });
55
+
56
+    it('injects and updates props into original container', () => {
57
+      uut.registerContainer('example.MyContainer', () => MyContainer);
58
+
59
+      const NavigationContainer = getRegisteredComponentClassFromAppRegistry();
60
+      class TestParent extends Component { //eslint-disable-line
61
+        constructor(props) {
62
+          super(props);
63
+          this.state = {};
64
+        }
65
+
66
+        render() {
67
+          return (
68
+            <NavigationContainer screenId="screen1" name={this.state.name}/>
69
+          );
70
+        }
71
+      }
77 72
 
78
-    expect(tree.toJSON().children).toEqual(['Hello, World!']);
79
-    testParentRef.setState({name: 'Gandalf'});
80
-    expect(tree.toJSON().children).toEqual(['Hello, Gandalf!']);
73
+      let testParentRef = null;
74
+      const tree = renderer.create(
75
+        <TestParent ref={(r) => testParentRef = r}/>
76
+      );
77
+
78
+      expect(tree.toJSON().children).toEqual(['Hello, World!']);
79
+      testParentRef.setState({name: 'Gandalf'});
80
+      expect(tree.toJSON().children).toEqual(['Hello, Gandalf!']);
81
+    });
82
+
83
+    it('asserts has screenId as prop', () => {
84
+      uut.registerContainer('example.MyContainer.key', () => MyContainer);
85
+      const NavigationContainer = getRegisteredComponentClassFromAppRegistry();
86
+      expect(() => {
87
+        renderer.create(<NavigationContainer/>);
88
+      }).toThrow(new Error('Screen example.MyContainer.key does not have a screenId!'));
89
+    });
81 90
   });
82 91
 });

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

@@ -0,0 +1 @@
1
+

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

@@ -0,0 +1,5 @@
1
+describe('ContainerStore', () => {
2
+  it('holds containers', () => {
3
+    //
4
+  });
5
+});

+ 0
- 2
wallaby.js View File

@@ -12,8 +12,6 @@ module.exports = function(wallaby) {
12 12
 
13 13
     files: [
14 14
       'package.json',
15
-      'node_modules/react/**/*.js',
16
-      'node_modules/*jest*/**/*.js',
17 15
       'src2/**/*.js',
18 16
       '!src2/**/*.test.js'
19 17
     ],