Selaa lähdekoodia

constants initialized lazily, js testing support

Daniel Zlotin 6 vuotta sitten
vanhempi
commit
0a87f8bb05

+ 13
- 5
integration/redux/Redux.test.js Näytä tiedosto

@@ -2,6 +2,7 @@ const React = require('react');
2 2
 require('react-native');
3 3
 const renderer = require('react-test-renderer');
4 4
 const { Provider } = require('react-redux');
5
+const { Navigation } = require('../../lib/dist/index');
5 6
 
6 7
 describe('redux support', () => {
7 8
   let MyConnectedComponent;
@@ -13,11 +14,18 @@ describe('redux support', () => {
13 14
   });
14 15
 
15 16
   it('renders normally', () => {
16
-    const tree = renderer.create(
17
-      <Provider store={store.reduxStore}>
18
-        <MyConnectedComponent />
19
-      </Provider>
20
-    );
17
+    const HOC = class extends React.Component {
18
+      render() {
19
+        return (
20
+          <Provider store={store.reduxStore}>
21
+            <MyConnectedComponent />
22
+          </Provider>
23
+        );
24
+      }
25
+    };
26
+    Navigation.registerComponent('ComponentName', () => HOC);
27
+
28
+    const tree = renderer.create(<HOC />);
21 29
     expect(tree.toJSON().children).toEqual(['no name']);
22 30
   });
23 31
 

+ 21
- 20
lib/src/Navigation.ts Näytä tiedosto

@@ -11,30 +11,24 @@ import { ComponentProvider } from 'react-native';
11 11
 import { Element } from './adapters/Element';
12 12
 import { ComponentEventsObserver } from './events/ComponentEventsObserver';
13 13
 import { CommandsObserver } from './events/CommandsObserver';
14
-import { Constants } from './constants/Constants';
15
-import { NativeModules } from 'react-native';
16
-
17
-const NavigationModule = NativeModules.RNNBridgeModule;
14
+import { Constants } from './adapters/Constants';
18 15
 
19 16
 export class Navigation {
20 17
   public readonly Element: React.ComponentType<{ elementId: any; resizeMode: any; }>;
21
-  public readonly constants = new Constants(NavigationModule);
22
-
23
-  private readonly store;
24
-  private readonly nativeEventsReceiver;
25
-  private readonly uniqueIdProvider;
26
-  private readonly componentRegistry;
27
-  private readonly layoutTreeParser;
28
-  private readonly layoutTreeCrawler;
29
-  private readonly nativeCommandsSender;
30
-  private readonly commands;
31
-  private readonly eventsRegistry;
32
-  private readonly commandsObserver;
33
-  private readonly componentEventsObserver;
18
+  public readonly store: Store;
19
+  private readonly nativeEventsReceiver: NativeEventsReceiver;
20
+  private readonly uniqueIdProvider: UniqueIdProvider;
21
+  private readonly componentRegistry: ComponentRegistry;
22
+  private readonly layoutTreeParser: LayoutTreeParser;
23
+  private readonly layoutTreeCrawler: LayoutTreeCrawler;
24
+  private readonly nativeCommandsSender: NativeCommandsSender;
25
+  private readonly commands: Commands;
26
+  private readonly eventsRegistry: EventsRegistry;
27
+  private readonly commandsObserver: CommandsObserver;
28
+  private readonly componentEventsObserver: ComponentEventsObserver;
34 29
 
35 30
   constructor() {
36 31
     this.Element = Element;
37
-
38 32
     this.store = new Store();
39 33
     this.nativeEventsReceiver = new NativeEventsReceiver();
40 34
     this.uniqueIdProvider = new UniqueIdProvider();
@@ -54,8 +48,8 @@ export class Navigation {
54 48
    * Every navigation component in your app must be registered with a unique name.
55 49
    * The component itself is a traditional React component extending React.Component.
56 50
    */
57
-  public registerComponent(componentName: string, getComponentClassFunc: ComponentProvider): React.ComponentType<any> {
58
-    return this.componentRegistry.registerComponent(componentName, getComponentClassFunc);
51
+  public registerComponent(componentName: string, getComponentClassFunc: ComponentProvider): void {
52
+    this.componentRegistry.registerComponent(componentName, getComponentClassFunc);
59 53
   }
60 54
 
61 55
   /**
@@ -155,4 +149,11 @@ export class Navigation {
155 149
   public events(): EventsRegistry {
156 150
     return this.eventsRegistry;
157 151
   }
152
+
153
+  /**
154
+   * Constants coming from native
155
+   */
156
+  public constants(): Constants {
157
+    return Constants.get();
158
+  }
158 159
 }

+ 21
- 0
lib/src/adapters/Constants.ts Näytä tiedosto

@@ -0,0 +1,21 @@
1
+import { NativeModules } from 'react-native';
2
+
3
+export class Constants {
4
+  static get(): Constants {
5
+    if (!this.instance) {
6
+      this.instance = new Constants();
7
+    }
8
+    return this.instance;
9
+  }
10
+
11
+  private static instance: Constants;
12
+
13
+  public readonly statusBarHeight: number;
14
+  public readonly backButtonId: string;
15
+
16
+  private constructor() {
17
+    const m = NativeModules.RNNBridgeModule;
18
+    this.statusBarHeight = m.statusBarHeight;
19
+    this.backButtonId = m.backButtonId;
20
+  }
21
+}

+ 1
- 1
lib/src/adapters/NativeCommandsSender.ts Näytä tiedosto

@@ -1,7 +1,7 @@
1 1
 import { NativeModules } from 'react-native';
2 2
 
3 3
 export class NativeCommandsSender {
4
-  private nativeCommandsModule;
4
+  private readonly nativeCommandsModule;
5 5
   constructor() {
6 6
     this.nativeCommandsModule = NativeModules.RNNBridgeModule;
7 7
   }

+ 14
- 2
lib/src/adapters/NativeEventsReceiver.ts Näytä tiedosto

@@ -1,10 +1,22 @@
1
+// tslint:disable:no-console
1 2
 import { NativeModules, NativeEventEmitter } from 'react-native';
2 3
 import { EventSubscription } from '../interfaces/EventSubscription';
3 4
 
4 5
 export class NativeEventsReceiver {
5
-  private emitter: NativeEventEmitter;
6
+  private emitter;
6 7
   constructor() {
7
-    this.emitter = new NativeEventEmitter(NativeModules.RNNEventEmitter);
8
+    if (NativeModules.RNNEventEmitter) {
9
+      this.emitter = new NativeEventEmitter(NativeModules.RNNEventEmitter);
10
+    } else {
11
+      console.log('Using mock NativeEventEmitter module');
12
+      this.emitter = {
13
+        addListener: () => {
14
+          return {
15
+            remove: () => undefined
16
+          };
17
+        }
18
+      };
19
+    }
8 20
   }
9 21
 
10 22
   public registerAppLaunchedListener(callback: () => void): EventSubscription {

+ 0
- 22
lib/src/constants/Constants.test.ts Näytä tiedosto

@@ -1,22 +0,0 @@
1
-import { Constants } from './Constants';
2
-
3
-const NavigationModule = {
4
-  backButton: 'RNN.back',
5
-  statusBarHeight: 63
6
-};
7
-
8
-describe('Constants', () => {
9
-  let uut: Constants;
10
-
11
-  beforeEach(() => {
12
-    uut = new Constants(NavigationModule);
13
-  });
14
-
15
-  it('backButton', () => {
16
-    expect(uut.backButton).toEqual(NavigationModule.backButton);
17
-  });
18
-
19
-  it('statusBarHeight', () => {
20
-    expect(uut.statusBarHeight).toEqual(NavigationModule.statusBarHeight);
21
-  });
22
-});

+ 0
- 9
lib/src/constants/Constants.ts Näytä tiedosto

@@ -1,9 +0,0 @@
1
-export class Constants {
2
-  public readonly backButton;
3
-  public readonly statusBarHeight;
4
-
5
-  constructor(navigationModule) {
6
-    this.backButton = navigationModule.backButton;
7
-    this.statusBarHeight = navigationModule.statusBarHeight;
8
-  }
9
-}