Browse Source

preparing the store

Daniel Zlotin 7 years ago
parent
commit
794b294865

+ 1
- 1
playground/e2e/app.test.js View File

26
 
26
 
27
   xit('screen lifecycle', () => {
27
   xit('screen lifecycle', () => {
28
     elementByLabel('Switch to lifecycle screen').tap();
28
     elementByLabel('Switch to lifecycle screen').tap();
29
-    expect(elementByLabel('Appeared!')).toBeVisible();
29
+    expect(elementByLabel('onStart!')).toBeVisible();
30
   });
30
   });
31
 });
31
 });
32
 
32
 

+ 4
- 4
playground/src/containers/LifecycleScreen.js View File

9
     };
9
     };
10
   }
10
   }
11
 
11
 
12
-  onAppear() {
13
-    this.setState({ text: 'Appeared!' });
12
+  onStart() {
13
+    this.setState({ text: 'onStart!' });
14
   }
14
   }
15
 
15
 
16
-  onDisappear() {
17
-    this.setState({ text: 'Disappeared!' });
16
+  onStop() {
17
+    this.setState({ text: 'onStop!' });
18
   }
18
   }
19
 
19
 
20
   render() {
20
   render() {

+ 1
- 1
src/containers/ContainerRegistry.js View File

9
   registerContainer(containerName, getContainerFunc) {
9
   registerContainer(containerName, getContainerFunc) {
10
     const OriginalContainer = getContainerFunc();
10
     const OriginalContainer = getContainerFunc();
11
     const NavigationContainer = ContainerWrapper.wrap(containerName, OriginalContainer, this.store);
11
     const NavigationContainer = ContainerWrapper.wrap(containerName, OriginalContainer, this.store);
12
-    this.store.setContainerClass(containerName, NavigationContainer);
12
+    this.store.setContainerClassForName(containerName, NavigationContainer);
13
     AppRegistry.registerComponent(containerName, () => NavigationContainer);
13
     AppRegistry.registerComponent(containerName, () => NavigationContainer);
14
   }
14
   }
15
 }
15
 }

+ 12
- 1
src/containers/ContainerRegistry.test.js View File

7
 
7
 
8
 describe('ContainerRegistry', () => {
8
 describe('ContainerRegistry', () => {
9
   let uut;
9
   let uut;
10
+  let store;
10
 
11
 
11
   class MyContainer extends Component {
12
   class MyContainer extends Component {
12
     render() {
13
     render() {
15
   }
16
   }
16
 
17
 
17
   beforeEach(() => {
18
   beforeEach(() => {
19
+    store = new Store();
18
     AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
20
     AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
19
-    uut = new ContainerRegistry(new Store());
21
+    uut = new ContainerRegistry(store);
20
   });
22
   });
21
 
23
 
22
   it('registers container component by containerName into AppRegistry', () => {
24
   it('registers container component by containerName into AppRegistry', () => {
26
     expect(AppRegistry.registerComponent.mock.calls[0][0]).toEqual('example.MyContainer.name');
28
     expect(AppRegistry.registerComponent.mock.calls[0][0]).toEqual('example.MyContainer.name');
27
   });
29
   });
28
 
30
 
31
+  it('saves the container wrapped into the store', () => {
32
+    expect(store.getContainerClassForName('example.MyContainer.name')).toBeUndefined();
33
+    uut.registerContainer('example.MyContainer.name', () => MyContainer);
34
+    const Class = store.getContainerClassForName('example.MyContainer.name');
35
+    expect(Class).not.toBeUndefined();
36
+    expect(Class).not.toEqual(MyContainer);
37
+    expect(Object.getPrototypeOf(Class)).toEqual(Component);
38
+  });
39
+
29
   it('resulting in a normal component', () => {
40
   it('resulting in a normal component', () => {
30
     uut.registerContainer('example.MyContainer.name', () => MyContainer);
41
     uut.registerContainer('example.MyContainer.name', () => MyContainer);
31
     const Container = AppRegistry.registerComponent.mock.calls[0][1]();
42
     const Container = AppRegistry.registerComponent.mock.calls[0][1]();

+ 10
- 6
src/containers/ContainerWrapper.js View File

1
 import React, { Component } from 'react';
1
 import React, { Component } from 'react';
2
 
2
 
3
 export default class ContainerWrapper {
3
 export default class ContainerWrapper {
4
-  static wrap(containerName, OriginalContainer, propStore) {
4
+  static wrap(containerName, OriginalContainer, store) {
5
     return class extends Component {
5
     return class extends Component {
6
       constructor(props) {
6
       constructor(props) {
7
         super(props);
7
         super(props);
8
-        if (!props.id) {
9
-          throw new Error(`Container ${containerName} does not have an id!`);
10
-        }
8
+        this._assertId(props);
11
         this.state = {
9
         this.state = {
12
           id: props.id,
10
           id: props.id,
13
-          allProps: { ...props, ...propStore.getPropsForContainerId(props.id) }
11
+          allProps: { ...props, ...store.getPropsForContainerId(props.id) }
14
         };
12
         };
15
       }
13
       }
16
 
14
 
15
+      _assertId(props) {
16
+        if (!props.id) {
17
+          throw new Error(`Container ${containerName} does not have an id!`);
18
+        }
19
+      }
20
+
17
       componentWillReceiveProps(nextProps) {
21
       componentWillReceiveProps(nextProps) {
18
         this.setState({
22
         this.setState({
19
-          allProps: { ...nextProps, ...propStore.getPropsForContainerId(this.state.id) }
23
+          allProps: { ...nextProps, ...store.getPropsForContainerId(this.state.id) }
20
         });
24
         });
21
       }
25
       }
22
 
26
 

+ 1
- 1
src/containers/ContainerWrapper.test.js View File

50
     }).toThrow(new Error('Container example.MyContainer does not have an id!'));
50
     }).toThrow(new Error('Container example.MyContainer does not have an id!'));
51
   });
51
   });
52
 
52
 
53
-  it('wraps the container and saves to store', () => {
53
+  it('wraps the container', () => {
54
     const NavigationContainer = ContainerWrapper.wrap(containerName, MyContainer, store);
54
     const NavigationContainer = ContainerWrapper.wrap(containerName, MyContainer, store);
55
     expect(NavigationContainer).not.toBeInstanceOf(MyContainer);
55
     expect(NavigationContainer).not.toBeInstanceOf(MyContainer);
56
     const tree = renderer.create(<NavigationContainer id={'container1'} />);
56
     const tree = renderer.create(<NavigationContainer id={'container1'} />);

+ 16
- 2
src/containers/Store.js View File

4
   constructor() {
4
   constructor() {
5
     this.propsByContainerId = {};
5
     this.propsByContainerId = {};
6
     this.containersByName = {};
6
     this.containersByName = {};
7
+    this.refsById = {};
7
   }
8
   }
8
 
9
 
9
   setPropsForContainerId(containerId, props) {
10
   setPropsForContainerId(containerId, props) {
14
     return _.get(this.propsByContainerId, containerId, {});
15
     return _.get(this.propsByContainerId, containerId, {});
15
   }
16
   }
16
 
17
 
17
-  setContainerClass(containerName, ContainerClass) {
18
+  setContainerClassForName(containerName, ContainerClass) {
18
     this.containersByName[containerName] = ContainerClass;
19
     this.containersByName[containerName] = ContainerClass;
19
   }
20
   }
20
 
21
 
21
-  getContainerClass(containerName) {
22
+  getContainerClassForName(containerName) {
22
     return this.containersByName[containerName];
23
     return this.containersByName[containerName];
23
   }
24
   }
25
+
26
+  setRefForId(id, ref) {
27
+    this.refsById[id] = ref;
28
+  }
29
+
30
+  getRefForId(id) {
31
+    return this.refsById[id];
32
+  }
33
+
34
+  cleanId(id) {
35
+    this.refsById[id] = undefined;
36
+    this.propsByContainerId[id] = undefined;
37
+  }
24
 }
38
 }

+ 19
- 2
src/containers/Store.test.js View File

26
     const MyComponent = class {
26
     const MyComponent = class {
27
       //
27
       //
28
     };
28
     };
29
-    uut.setContainerClass('example.mycontainer', MyComponent);
30
-    expect(uut.getContainerClass('example.mycontainer')).toEqual(MyComponent);
29
+    uut.setContainerClassForName('example.mycontainer', MyComponent);
30
+    expect(uut.getContainerClassForName('example.mycontainer')).toEqual(MyComponent);
31
+  });
32
+
33
+  it('holds container refs by id', () => {
34
+    const ref = {};
35
+    uut.setRefForId('refUniqueId', ref);
36
+    expect(uut.getRefForId('other')).toBeUndefined();
37
+    expect(uut.getRefForId('refUniqueId')).toBe(ref);
38
+  });
39
+
40
+  it('clean by id', () => {
41
+    uut.setRefForId('refUniqueId', {});
42
+    uut.setPropsForContainerId('refUniqueId', { foo: 'bar' });
43
+
44
+    uut.cleanId('refUniqueId');
45
+
46
+    expect(uut.getRefForId('refUniqueId')).toBeUndefined();
47
+    expect(uut.getPropsForContainerId('refUniqueId')).toEqual({});
31
   });
48
   });
32
 });
49
 });
33
 
50