Daniel Zlotin пре 6 година
родитељ
комит
9eecaae742

+ 3
- 3
lib/src/adapters/NativeEventsReceiver.ts Прегледај датотеку

@@ -14,15 +14,15 @@ export class NativeEventsReceiver {
14 14
     return this.emitter.addListener('RNN.onAppLaunched', callback);
15 15
   }
16 16
 
17
-  registerComponentDidAppear(callback: (componendId: string, componentName: string) => void): EventSubscription {
17
+  registerComponentDidAppear(callback: (componentId: string, componentName: string) => void): EventSubscription {
18 18
     return this.emitter.addListener('RNN.componentDidAppear', callback);
19 19
   }
20 20
 
21
-  registerComponentDidDisappear(callback: (componendId: string, componentName: string) => void): EventSubscription {
21
+  registerComponentDidDisappear(callback: (componentId: string, componentName: string) => void): EventSubscription {
22 22
     return this.emitter.addListener('RNN.componentDidDisappear', callback);
23 23
   }
24 24
 
25
-  registerOnNavigationInteraction(callback: (name: string) => void): EventSubscription {
25
+  registerOnNavigationInteraction(callback: (componentId: string, params) => void): EventSubscription {
26 26
     return this.emitter.addListener('RNN.onNavigationInteraction', callback);
27 27
   }
28 28
 }

+ 13
- 17
lib/src/components/ComponentWrapper.tsx Прегледај датотеку

@@ -1,38 +1,24 @@
1 1
 import * as React from 'react';
2 2
 import * as  _ from 'lodash';
3 3
 
4
-interface State {
5
-  componentId: string;
6
-  allProps: {};
7
-}
8
-
9 4
 export class ComponentWrapper {
5
+
10 6
   static wrap(componentName: string, OriginalComponentClass: React.ComponentType<any>, store): React.ComponentType<any> {
11 7
 
12
-    class WrappedComponent extends React.Component<any, State> {
8
+    class WrappedComponent extends React.Component<any, { componentId: string; allProps: {}; }> {
13 9
 
14 10
       private originalComponentRef;
15 11
 
16 12
       constructor(props) {
17 13
         super(props);
14
+        this._assertComponentId();
18 15
         this._saveComponentRef = this._saveComponentRef.bind(this);
19
-        this._assertComponentId(props);
20 16
         this.state = {
21 17
           componentId: props.componentId,
22 18
           allProps: _.merge({}, props, store.getPropsForId(props.componentId))
23 19
         };
24 20
       }
25 21
 
26
-      _assertComponentId(props) {
27
-        if (!props.componentId) {
28
-          throw new Error(`Component ${componentName} does not have a componentId!`);
29
-        }
30
-      }
31
-
32
-      _saveComponentRef(r) {
33
-        this.originalComponentRef = r;
34
-      }
35
-
36 22
       componentWillMount() {
37 23
         store.setRefForId(this.state.componentId, this);
38 24
       }
@@ -75,6 +61,16 @@ export class ComponentWrapper {
75 61
           />
76 62
         );
77 63
       }
64
+
65
+      private _assertComponentId() {
66
+        if (!this.props.componentId) {
67
+          throw new Error(`Component ${componentName} does not have a componentId!`);
68
+        }
69
+      }
70
+
71
+      private _saveComponentRef(r) {
72
+        this.originalComponentRef = r;
73
+      }
78 74
     }
79 75
 
80 76
     return WrappedComponent;

+ 73
- 0
lib/src/events/ComponentEventsRegistry.test.ts Прегледај датотеку

@@ -0,0 +1,73 @@
1
+import { ComponentEventsRegistry } from './ComponentEventsRegistry';
2
+import { Store } from '../components/Store';
3
+
4
+describe(`ComponentEventRegistry`, () => {
5
+  let uut: ComponentEventsRegistry;
6
+  let eventRegistry;
7
+  let store: Store;
8
+  let mockComponentRef;
9
+  const refId = 'myUniqueId';
10
+
11
+  beforeEach(() => {
12
+    eventRegistry = {
13
+      componentDidAppear: jest.fn(),
14
+      componentDidDisappear: jest.fn(),
15
+      onNavigationInteraction: jest.fn()
16
+    };
17
+
18
+    mockComponentRef = {
19
+      componentDidAppear: jest.fn(),
20
+      componentDidDisappear: jest.fn(),
21
+      onNavigationInteraction: jest.fn()
22
+    };
23
+
24
+    store = new Store();
25
+    store.setRefForId(refId, mockComponentRef);
26
+
27
+    uut = new ComponentEventsRegistry(eventRegistry, store);
28
+  });
29
+
30
+  it('register for lifecycle events on eventRegistry', () => {
31
+    expect(eventRegistry.componentDidAppear).toHaveBeenCalledTimes(0);
32
+    expect(eventRegistry.componentDidDisappear).toHaveBeenCalledTimes(0);
33
+    expect(eventRegistry.onNavigationInteraction).toHaveBeenCalledTimes(0);
34
+    uut.registerForAllComponents();
35
+    expect(eventRegistry.componentDidAppear).toHaveBeenCalledTimes(1);
36
+    expect(eventRegistry.componentDidDisappear).toHaveBeenCalledTimes(1);
37
+    expect(eventRegistry.onNavigationInteraction).toHaveBeenCalledTimes(1);
38
+  });
39
+
40
+  it('bubbles lifecycle to component from store', () => {
41
+    const params = {};
42
+    expect(mockComponentRef.componentDidAppear).toHaveBeenCalledTimes(0);
43
+    expect(mockComponentRef.componentDidDisappear).toHaveBeenCalledTimes(0);
44
+    expect(mockComponentRef.onNavigationInteraction).toHaveBeenCalledTimes(0);
45
+    uut.registerForAllComponents();
46
+    eventRegistry.componentDidAppear.mock.calls[0][0](refId);
47
+    eventRegistry.componentDidDisappear.mock.calls[0][0](refId);
48
+    eventRegistry.onNavigationInteraction.mock.calls[0][0](refId, params);
49
+    expect(mockComponentRef.componentDidAppear).toHaveBeenCalledTimes(1);
50
+    expect(mockComponentRef.componentDidDisappear).toHaveBeenCalledTimes(1);
51
+    expect(mockComponentRef.onNavigationInteraction).toHaveBeenCalledTimes(1);
52
+    expect(mockComponentRef.onNavigationInteraction).toHaveBeenCalledWith(params);
53
+  });
54
+
55
+  it('defensive unknown id', () => {
56
+    uut.registerForAllComponents();
57
+    expect(() => {
58
+      eventRegistry.componentDidAppear.mock.calls[0][0]('bad id');
59
+      eventRegistry.componentDidDisappear.mock.calls[0][0]('bad id');
60
+      eventRegistry.onNavigationInteraction.mock.calls[0][0]('bad id', {});
61
+    }).not.toThrow();
62
+  });
63
+
64
+  it('defensive method impl', () => {
65
+    store.setRefForId('myId', {});
66
+    uut.registerForAllComponents();
67
+    expect(() => {
68
+      eventRegistry.componentDidAppear.mock.calls[0][0]('myId');
69
+      eventRegistry.componentDidDisappear.mock.calls[0][0]('myId');
70
+      eventRegistry.onNavigationInteraction.mock.calls[0][0]('myId', {});
71
+    }).not.toThrow();
72
+  });
73
+});

+ 37
- 0
lib/src/events/ComponentEventsRegistry.ts Прегледај датотеку

@@ -0,0 +1,37 @@
1
+import { EventsRegistry } from './EventsRegistry';
2
+import { Store } from '../components/Store';
3
+
4
+export class ComponentEventsRegistry {
5
+  constructor(private eventsRegistry: EventsRegistry, private store: Store) {
6
+    this.componentDidAppear = this.componentDidAppear.bind(this);
7
+    this.componentDidDisappear = this.componentDidDisappear.bind(this);
8
+    this.onNavigationInteraction = this.onNavigationInteraction.bind(this);
9
+  }
10
+
11
+  public registerForAllComponents(): void {
12
+    this.eventsRegistry.componentDidAppear(this.componentDidAppear);
13
+    this.eventsRegistry.componentDidDisappear(this.componentDidDisappear);
14
+    this.eventsRegistry.onNavigationInteraction(this.onNavigationInteraction);
15
+  }
16
+
17
+  private componentDidAppear(componentId: string) {
18
+    const componentRef = this.store.getRefForId(componentId);
19
+    if (componentRef && componentRef.componentDidAppear) {
20
+      componentRef.componentDidAppear();
21
+    }
22
+  }
23
+
24
+  private componentDidDisappear(componentId: string) {
25
+    const componentRef = this.store.getRefForId(componentId);
26
+    if (componentRef && componentRef.componentDidDisappear) {
27
+      componentRef.componentDidDisappear();
28
+    }
29
+  }
30
+
31
+  private onNavigationInteraction(componentId: string, params) {
32
+    const componentRef = this.store.getRefForId(componentId);
33
+    if (componentRef && componentRef.onNavigationInteraction) {
34
+      componentRef.onNavigationInteraction(params);
35
+    }
36
+  }
37
+}

+ 2
- 2
lib/src/events/EventsRegistry.test.ts Прегледај датотеку

@@ -10,7 +10,7 @@ describe('EventsRegistry', () => {
10 10
     uut = new EventsRegistry(mockNativeEventsReceiver);
11 11
   });
12 12
 
13
-  it('exposes appLaunch event', () => {
13
+  it('exposes onAppLaunched event', () => {
14 14
     const subscription = {};
15 15
     const cb = jest.fn();
16 16
     mockNativeEventsReceiver.registerOnAppLaunched.mockReturnValueOnce(subscription);
@@ -46,7 +46,7 @@ describe('EventsRegistry', () => {
46 46
     expect(mockNativeEventsReceiver.registerComponentDidDisappear).toHaveBeenCalledWith(cb);
47 47
   });
48 48
 
49
-  it('exposes interaction event', () => {
49
+  it('exposes onNavigationInteraction event', () => {
50 50
     const subscription = {};
51 51
     const cb = jest.fn();
52 52
     mockNativeEventsReceiver.registerOnNavigationInteraction.mockReturnValueOnce(subscription);

+ 3
- 3
lib/src/events/EventsRegistry.ts Прегледај датотеку

@@ -11,15 +11,15 @@ export class EventsRegistry {
11 11
     return this.nativeEventsReceiver.registerOnAppLaunched(callback);
12 12
   }
13 13
 
14
-  public componentDidAppear(callback: (componendId: string, componentName: string) => void): EventSubscription {
14
+  public componentDidAppear(callback: (componentId: string, componentName: string) => void): EventSubscription {
15 15
     return this.nativeEventsReceiver.registerComponentDidAppear(callback);
16 16
   }
17 17
 
18
-  public componentDidDisappear(callback: (componendId: string, componentName: string) => void): EventSubscription {
18
+  public componentDidDisappear(callback: (componentId: string, componentName: string) => void): EventSubscription {
19 19
     return this.nativeEventsReceiver.registerComponentDidDisappear(callback);
20 20
   }
21 21
 
22
-  public onNavigationInteraction(callback: (name: string) => void): EventSubscription {
22
+  public onNavigationInteraction(callback: (componentId: string, params) => void): EventSubscription {
23 23
     return this.nativeEventsReceiver.registerOnNavigationInteraction(callback);
24 24
   }
25 25
 }