Browse Source

constants initialized lazily, js testing support

Daniel Zlotin 6 years ago
parent
commit
0a87f8bb05

+ 13
- 5
integration/redux/Redux.test.js View File

2
 require('react-native');
2
 require('react-native');
3
 const renderer = require('react-test-renderer');
3
 const renderer = require('react-test-renderer');
4
 const { Provider } = require('react-redux');
4
 const { Provider } = require('react-redux');
5
+const { Navigation } = require('../../lib/dist/index');
5
 
6
 
6
 describe('redux support', () => {
7
 describe('redux support', () => {
7
   let MyConnectedComponent;
8
   let MyConnectedComponent;
13
   });
14
   });
14
 
15
 
15
   it('renders normally', () => {
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
     expect(tree.toJSON().children).toEqual(['no name']);
29
     expect(tree.toJSON().children).toEqual(['no name']);
22
   });
30
   });
23
 
31
 

+ 21
- 20
lib/src/Navigation.ts View File

11
 import { Element } from './adapters/Element';
11
 import { Element } from './adapters/Element';
12
 import { ComponentEventsObserver } from './events/ComponentEventsObserver';
12
 import { ComponentEventsObserver } from './events/ComponentEventsObserver';
13
 import { CommandsObserver } from './events/CommandsObserver';
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
 export class Navigation {
16
 export class Navigation {
20
   public readonly Element: React.ComponentType<{ elementId: any; resizeMode: any; }>;
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
   constructor() {
30
   constructor() {
36
     this.Element = Element;
31
     this.Element = Element;
37
-
38
     this.store = new Store();
32
     this.store = new Store();
39
     this.nativeEventsReceiver = new NativeEventsReceiver();
33
     this.nativeEventsReceiver = new NativeEventsReceiver();
40
     this.uniqueIdProvider = new UniqueIdProvider();
34
     this.uniqueIdProvider = new UniqueIdProvider();
54
    * Every navigation component in your app must be registered with a unique name.
48
    * Every navigation component in your app must be registered with a unique name.
55
    * The component itself is a traditional React component extending React.Component.
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
   public events(): EventsRegistry {
149
   public events(): EventsRegistry {
156
     return this.eventsRegistry;
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 View File

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 View File

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

+ 14
- 2
lib/src/adapters/NativeEventsReceiver.ts View File

1
+// tslint:disable:no-console
1
 import { NativeModules, NativeEventEmitter } from 'react-native';
2
 import { NativeModules, NativeEventEmitter } from 'react-native';
2
 import { EventSubscription } from '../interfaces/EventSubscription';
3
 import { EventSubscription } from '../interfaces/EventSubscription';
3
 
4
 
4
 export class NativeEventsReceiver {
5
 export class NativeEventsReceiver {
5
-  private emitter: NativeEventEmitter;
6
+  private emitter;
6
   constructor() {
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
   public registerAppLaunchedListener(callback: () => void): EventSubscription {
22
   public registerAppLaunchedListener(callback: () => void): EventSubscription {

+ 0
- 22
lib/src/constants/Constants.test.ts View File

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 View File

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
-}