Daniel Zlotin пре 8 година
родитељ
комит
4a60af6a9d

+ 1
- 4
playground/src/app.js Прегледај датотеку

@@ -27,11 +27,8 @@ class WelcomeScreen extends Component {
27 27
   }
28 28
 }
29 29
 
30
-import {NativeModules, NativeEventEmitter} from 'react-native';
31
-
32 30
 export function start() {
33
-  const emitter = new NativeEventEmitter(NativeModules.RNNEventEmitter);
34
-  emitter.addListener('onAppLaunched', () => {
31
+  Navigation.onAppLaunched('onAppLaunched', () => {
35 32
     Navigation.registerContainer(`com.example.WelcomeScreen`, () => WelcomeScreen);
36 33
     Navigation.startApp({
37 34
       container: {

+ 16
- 179
src/Navigation.js Прегледај датотеку

@@ -1,191 +1,28 @@
1 1
 import * as ContainerRegistry from './containers/ContainerRegistry';
2 2
 import * as Commands from './commands/Commands';
3 3
 
4
-export function registerContainer(containerName, getContainerFunc) {
5
-  ContainerRegistry.registerContainer(containerName, getContainerFunc);
6
-}
7
-
8
-export function startApp(params) {
9
-  Commands.startApp(params);
10
-}
11
-
12
-export function push(params) {
13
-  //
14
-}
15
-
16
-export function pop(params) {
17
-  //
18
-}
19
-
20
-export function showModal(params) {
21
-  //
22
-}
23
-
24
-export function dismissModal(params) {
25
-  //
26
-}
27
-
28
-export function dismissAllModals(params) {
29
-  //
30
-}
31
-
32
-export function showLightbox(params) {
33
-  //
34
-}
4
+import NativeEventsReceiver from './adapters/NativeEventsReceiver';
35 5
 
36
-export function dismissLightbox(params) {
37
-  //
38
-}
6
+class Navigation {
7
+  constructor() {
8
+    this.nativeEventsReceiver = new NativeEventsReceiver();
9
+  }
39 10
 
40
-export function showInAppNotification(params) {
41
-  //
42
-}
11
+  registerContainer(containerName, getContainerFunc) {
12
+    ContainerRegistry.registerContainer(containerName, getContainerFunc);
13
+  }
43 14
 
44
-export function dismissInAppNotification(params) {
45
-  //
46
-}
15
+  startApp(params) {
16
+    Commands.startApp(params);
17
+  }
47 18
 
48
-export function popToRoot(params) {
49
-  //
50
-}
19
+  onAppLaunched(fn) {
20
+    this.nativeEventsReceiver.onAppLaunched(fn);
21
+  }
51 22
 
52
-export function newStack(params) {
53
-  //
54 23
 }
55 24
 
56
-// new work on src/Navigation:
57
-//+function startApp(layout) {
58
-//  +    return platformSpecific.startApp(layout);
59
-//  +}
60
-//+
61
-//+  startSingleScreenApp: startSingleScreenApp,
62
-//  +  startApp: startApp
63
-
64
-// new work on src/newParams.json:
65
-//
66
-//{
67
-//  "screen": {
68
-//  "screen": "example.FirstTabScreen",
69
-//    "title": "Navigation",
70
-//    "navigatorStyle": {
71
-//    "navBarBackgroundColor": "#4dbce9",
72
-//      "navBarTextColor": "#ffff00",
73
-//      "navBarSubtitleTextColor": "#ff0000",
74
-//      "navBarButtonColor": "#ffffff",
75
-//      "statusBarTextColorScheme": "light",
76
-//      "tabBarBackgroundColor": "#4dbce9",
77
-//      "tabBarButtonColor": "#ffffff",
78
-//      "tabBarSelectedButtonColor": "#ffff00"
79
-//  },
80
-//  "navigatorID": "navigatorID1_nav",
81
-//    "screenInstanceID": "screenInstanceID2",
82
-//    "navigatorEventID": "screenInstanceID2_events",
83
-//    "navigatorButtons": {
84
-//    "leftButtons": [
85
-//      {
86
-//        "icon": "http://localhost:8081/assets/img/navicon_menu@2x.png?platform=ios&hash=940519e495eac2a6236034f2bf88ce90",
87
-//        "id": "menu"
88
-//      }
89
-//    ],
90
-//      "rightButtons": [
91
-//      {
92
-//        "title": "Edit",
93
-//        "id": "edit",
94
-//        "enabled": true
95
-//      },
96
-//      {
97
-//        "icon": "http://localhost:8081/assets/img/navicon_add@2x.png?platform=ios&hash=667fefa47fb5daebbc3943669dc6eb26",
98
-//        "id": "add",
99
-//        "enabled": true
100
-//      }
101
-//    ]
102
-//  },
103
-//  "screenId": "example.FirstTabScreen",
104
-//    "styleParams": {
105
-//    "topBarColor": 4283284713,
106
-//      "titleBarTitleColor": 4294967040,
107
-//      "titleBarButtonColor": 4294967295,
108
-//      "drawBelowTopBar": true,
109
-//      "drawScreenAboveBottomTabs": true,
110
-//      "bottomTabsColor": 4283284713,
111
-//      "bottomTabsButtonColor": 4294967295,
112
-//      "bottomTabsSelectedButtonColor": 4294967040
113
-//  },
114
-//  "navigationParams": {
115
-//    "screenInstanceID": "screenInstanceID2",
116
-//      "navigatorID": "navigatorID1_nav",
117
-//      "navigatorEventID": "screenInstanceID2_events"
118
-//  }
119
-//},
120
-//  "drawer": {
121
-//  "left": {
122
-//    "screen": "example.SideMenu"
123
-//  }
124
-//},
125
-//  "appStyle": null,
126
-//  "sideMenu": {
127
-//  "left": {
128
-//    "screenId": "example.SideMenu",
129
-//      "navigatorID": "navigatorID3_nav",
130
-//      "screenInstanceID": "screenInstanceID4",
131
-//      "navigatorEventID": "screenInstanceID4_events",
132
-//      "navigationParams": {
133
-//      "screenInstanceID": "screenInstanceID4",
134
-//        "navigatorID": "navigatorID3_nav",
135
-//        "navigatorEventID": "screenInstanceID4_events"
136
-//    }
137
-//  },
138
-//  "right": null
139
-//},
140
-//  "animateShow": true
141
-//}
25
+const singleton = new Navigation();
142 26
 
143
-// new work on platformSpecificDeprecated.ios.js:
144
-//import Navigation from './../Navigation';
145
-//import Controllers, {Modal, Notification} from './controllers';
146
-//+import _ from 'lodash';
147
-//+import PropRegistry from '../PropRegistry';
148
-//+import * as newPlatformSpecific from './../platformSpecific';
149
-//+
150
-//const React = Controllers.hijackReact();
151
-//const {
152
-//  ControllerRegistry,
153
-//  TabBarControllerIOS,
154
-//  NavigationControllerIOS,
155
-//  DrawerControllerIOS
156
-//} = React;
157
-//-import _ from 'lodash';
158
-//
159
-//-import PropRegistry from '../PropRegistry';
160
-//+function startApp(layout) {
161
-//  +	newPlatformSpecific.startApp(layout);
162
-//  +}
163
-//
164
-//function startTabBasedApp(params) {
165
-//  if (!params.tabs) {
166
-//  @@ -566,6 +571,7 @@ function dismissContextualMenu() {
167
-//    }
168
-//
169
-//    export default {
170
-//    +  startApp,
171
-//    startTabBasedApp,
172
-//      startSingleScreenApp,
173
-//      navigatorPush,
27
+module.exports = singleton;
174 28
 
175
-//new work on platformSpecific.ios.js:
176
-//-module.exports = {};
177
-//+import React, {Component} from 'react';
178
-//+import {AppRegistry, NativeModules} from 'react-native';
179
-//+import _ from 'lodash';
180
-//+import PropRegistry from './PropRegistry';
181
-//+
182
-//  +const NativeAppMAnager = NativeModules.RCCManager;
183
-//+
184
-//  +function startApp(layout) {
185
-//    +
186
-//      +	NativeAppMAnager.startApp(layout);
187
-//    +}
188
-//+
189
-//+module.exports = {
190
-//                  +	startApp
191
-//                  +};

+ 13
- 20
src/Navigation.test.js Прегледај датотеку

@@ -1,33 +1,21 @@
1
-import _ from 'lodash';
2
-
3 1
 describe('Navigation', () => {
4 2
   let Navigation;
5 3
   let ContainerRegistry, Commands;
4
+  let mockNativeEventsReceiver;
6 5
 
7 6
   beforeEach(() => {
8 7
     jest.mock('./containers/ContainerRegistry');
9 8
     jest.mock('./commands/Commands');
10 9
     ContainerRegistry = require('./containers/ContainerRegistry');
11 10
     Commands = require('./commands/Commands');
12
-    Navigation = require('./Navigation');
13
-  });
14 11
 
15
-  it('exposes static commands', () => {
16
-    _.forEach([
17
-      Navigation.registerContainer,
18
-      Navigation.startApp,
19
-      Navigation.push,
20
-      Navigation.pop,
21
-      Navigation.popToRoot,
22
-      Navigation.newStack,
23
-      Navigation.showModal,
24
-      Navigation.dismissModal,
25
-      Navigation.dismissAllModals,
26
-      Navigation.showLightbox,
27
-      Navigation.dismissLightbox,
28
-      Navigation.showInAppNotification,
29
-      Navigation.dismissInAppNotification
30
-    ], (f) => expect(f).toBeInstanceOf(Function));
12
+    mockNativeEventsReceiver = {
13
+      onAppLaunched: jest.fn()
14
+    };
15
+
16
+    jest.mock('./adapters/NativeEventsReceiver', () => () => mockNativeEventsReceiver);
17
+
18
+    Navigation = require('./Navigation');
31 19
   });
32 20
 
33 21
   it('registerContainer delegates to ContainerRegistry', () => {
@@ -44,4 +32,9 @@ describe('Navigation', () => {
44 32
     expect(Commands.startApp).toHaveBeenCalledTimes(1);
45 33
     expect(Commands.startApp).toHaveBeenCalledWith(params);
46 34
   });
35
+
36
+  it('onAppLaunched delegates to NativeEventsReceiver', () => {
37
+    Navigation.onAppLaunched(jest.fn());
38
+    expect(mockNativeEventsReceiver.onAppLaunched).toHaveBeenCalledTimes(1);
39
+  });
47 40
 });

+ 11
- 0
src/adapters/NativeEventsReceiver.js Прегледај датотеку

@@ -0,0 +1,11 @@
1
+import {NativeModules, NativeEventEmitter} from 'react-native';
2
+
3
+export default class NativeEventsReceiver {
4
+  constructor() {
5
+    this.emitter = new NativeEventEmitter(NativeModules.RNNEventEmitter);
6
+  }
7
+
8
+  onAppLaunched(callback) {
9
+    this.emitter.addListener('onAppLaunched', callback);
10
+  }
11
+}

+ 19
- 0
src/adapters/NativeEventsReceiver.test.js Прегледај датотеку

@@ -0,0 +1,19 @@
1
+describe('NativeEventsReceiver', () => {
2
+  let uut;
3
+  const eventEmitterMock = {addListener: jest.fn()};
4
+
5
+  beforeEach(() => {
6
+    require('react-native').NativeModules.RNNEventEmitter = {};
7
+    const NativeEventsReceiver = require('./NativeEventsReceiver').default;
8
+    uut = new NativeEventsReceiver();
9
+    uut.emitter = eventEmitterMock;
10
+  });
11
+
12
+  it('register for onAppLaunched', () => {
13
+    const callback = jest.fn();
14
+    uut.onAppLaunched(callback);
15
+    expect(callback).not.toHaveBeenCalled();
16
+    expect(eventEmitterMock.addListener).toHaveBeenCalledTimes(1);
17
+    expect(eventEmitterMock.addListener).toHaveBeenCalledWith('onAppLaunched', callback);
18
+  });
19
+});

src/providers/UniqueIdProvider.js → src/adapters/UniqueIdProvider.js Прегледај датотеку


src/providers/UniqueIdProvider.test.js → src/adapters/UniqueIdProvider.test.js Прегледај датотеку


src/providers/__mocks__/UniqueIdProvider.js → src/adapters/__mocks__/UniqueIdProvider.js Прегледај датотеку


+ 1
- 1
src/commands/LayoutBuilder.js Прегледај датотеку

@@ -1,5 +1,5 @@
1 1
 import _ from 'lodash';
2
-import {uniqueId} from '../providers/UniqueIdProvider';
2
+import {uniqueId} from '../adapters/UniqueIdProvider';
3 3
 
4 4
 export function parse(params) {
5 5
   const layout = _.merge({}, params);

+ 1
- 1
src/commands/LayoutBuilder.test.js Прегледај датотеку

@@ -2,7 +2,7 @@ describe('LayoutBuilder', () => {
2 2
   let LayoutBuilder;
3 3
 
4 4
   beforeEach(() => {
5
-    jest.mock('../providers/UniqueIdProvider');
5
+    jest.mock('../adapters/UniqueIdProvider');
6 6
     LayoutBuilder = require('./LayoutBuilder');
7 7
   });
8 8