Daniel Zlotin 7 лет назад
Родитель
Сommit
794b294865

+ 1
- 1
playground/e2e/app.test.js Просмотреть файл

@@ -26,7 +26,7 @@ describe('app', () => {
26 26
 
27 27
   xit('screen lifecycle', () => {
28 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 Просмотреть файл

@@ -9,12 +9,12 @@ class LifecycleScreen extends Component {
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 20
   render() {

+ 1
- 1
src/containers/ContainerRegistry.js Просмотреть файл

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

+ 12
- 1
src/containers/ContainerRegistry.test.js Просмотреть файл

@@ -7,6 +7,7 @@ import Store from './Store';
7 7
 
8 8
 describe('ContainerRegistry', () => {
9 9
   let uut;
10
+  let store;
10 11
 
11 12
   class MyContainer extends Component {
12 13
     render() {
@@ -15,8 +16,9 @@ describe('ContainerRegistry', () => {
15 16
   }
16 17
 
17 18
   beforeEach(() => {
19
+    store = new Store();
18 20
     AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
19
-    uut = new ContainerRegistry(new Store());
21
+    uut = new ContainerRegistry(store);
20 22
   });
21 23
 
22 24
   it('registers container component by containerName into AppRegistry', () => {
@@ -26,6 +28,15 @@ describe('ContainerRegistry', () => {
26 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 40
   it('resulting in a normal component', () => {
30 41
     uut.registerContainer('example.MyContainer.name', () => MyContainer);
31 42
     const Container = AppRegistry.registerComponent.mock.calls[0][1]();

+ 10
- 6
src/containers/ContainerWrapper.js Просмотреть файл

@@ -1,22 +1,26 @@
1 1
 import React, { Component } from 'react';
2 2
 
3 3
 export default class ContainerWrapper {
4
-  static wrap(containerName, OriginalContainer, propStore) {
4
+  static wrap(containerName, OriginalContainer, store) {
5 5
     return class extends Component {
6 6
       constructor(props) {
7 7
         super(props);
8
-        if (!props.id) {
9
-          throw new Error(`Container ${containerName} does not have an id!`);
10
-        }
8
+        this._assertId(props);
11 9
         this.state = {
12 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 21
       componentWillReceiveProps(nextProps) {
18 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 Просмотреть файл

@@ -50,7 +50,7 @@ describe('ContainerWrapper', () => {
50 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 54
     const NavigationContainer = ContainerWrapper.wrap(containerName, MyContainer, store);
55 55
     expect(NavigationContainer).not.toBeInstanceOf(MyContainer);
56 56
     const tree = renderer.create(<NavigationContainer id={'container1'} />);

+ 16
- 2
src/containers/Store.js Просмотреть файл

@@ -4,6 +4,7 @@ export default class Store {
4 4
   constructor() {
5 5
     this.propsByContainerId = {};
6 6
     this.containersByName = {};
7
+    this.refsById = {};
7 8
   }
8 9
 
9 10
   setPropsForContainerId(containerId, props) {
@@ -14,11 +15,24 @@ export default class Store {
14 15
     return _.get(this.propsByContainerId, containerId, {});
15 16
   }
16 17
 
17
-  setContainerClass(containerName, ContainerClass) {
18
+  setContainerClassForName(containerName, ContainerClass) {
18 19
     this.containersByName[containerName] = ContainerClass;
19 20
   }
20 21
 
21
-  getContainerClass(containerName) {
22
+  getContainerClassForName(containerName) {
22 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 Просмотреть файл

@@ -26,8 +26,25 @@ describe('Store', () => {
26 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