Parcourir la source

simplified commands api

Daniel Zlotin il y a 7 ans
Parent
révision
65b655a554

+ 4
- 4
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java Voir le fichier

@@ -65,21 +65,21 @@ public class NavigationModule extends ReactContextBaseJavaModule {
65 65
 	}
66 66
 
67 67
 	@ReactMethod
68
-	public void popTo(final String onContainerId, final String toContainerId) {
68
+	public void popTo(final String containerId) {
69 69
 		handle(new Runnable() {
70 70
 			@Override
71 71
 			public void run() {
72
-				navigator().popTo(onContainerId, toContainerId);
72
+				navigator().popTo(containerId);
73 73
 			}
74 74
 		});
75 75
 	}
76 76
 
77 77
 	@ReactMethod
78
-	public void popToRoot(final String onContainerId) {
78
+	public void popToRoot(final String containerId) {
79 79
 		handle(new Runnable() {
80 80
 			@Override
81 81
 			public void run() {
82
-				navigator().popToRoot(onContainerId);
82
+				navigator().popToRoot(containerId);
83 83
 			}
84 84
 		});
85 85
 	}

+ 5
- 6
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java Voir le fichier

@@ -87,13 +87,12 @@ public class Navigator extends ParentController {
87 87
 		}
88 88
 	}
89 89
 
90
-	public void popTo(final String fromId, final String toId) {
91
-		ViewController from = findControllerById(fromId);
92
-		ViewController to = findControllerById(toId);
93
-		if (from != null && to != null) {
94
-			StackController parentStackController = from.getParentStackController();
90
+	public void popTo(final String containerId) {
91
+		ViewController target = findControllerById(containerId);
92
+		if (target != null) {
93
+			StackController parentStackController = target.getParentStackController();
95 94
 			if (parentStackController != null) {
96
-				parentStackController.popTo(to);
95
+				parentStackController.popTo(target);
97 96
 			}
98 97
 		}
99 98
 	}

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java Voir le fichier

@@ -154,7 +154,7 @@ public class NavigatorTest extends BaseTest {
154 154
 		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
155 155
 		uut.setRoot(bottomTabsController);
156 156
 
157
-		uut.popTo(child2.getId(), child2.getId());
157
+		uut.popTo(child2.getId());
158 158
 
159 159
 		assertThat(stack2.getChildControllers()).containsOnly(child2);
160 160
 	}

+ 2
- 2
lib/ios/RNNBridgeModule.m Voir le fichier

@@ -30,8 +30,8 @@ RCT_EXPORT_METHOD(pop:(NSString*)containerId) {
30 30
 	[_commandsHandler pop:containerId];
31 31
 }
32 32
 
33
-RCT_EXPORT_METHOD(popTo:(NSString*)fromContainerId toContainerId:(NSString*)toContainerId) {
34
-	[_commandsHandler popTo:toContainerId fromContainerId:fromContainerId];
33
+RCT_EXPORT_METHOD(popTo:(NSString*)containerId) {
34
+	[_commandsHandler popTo:containerId];
35 35
 }
36 36
 
37 37
 RCT_EXPORT_METHOD(popToRoot:(NSString*)containerId) {

+ 1
- 1
lib/ios/RNNCommandsHandler.h Voir le fichier

@@ -14,7 +14,7 @@
14 14
 
15 15
 -(void) pop:(NSString*)containerId;
16 16
 
17
--(void) popTo:(NSString*)containerId fromContainerId:(NSString*)fromContainerId;
17
+-(void) popTo:(NSString*)containerId;
18 18
 
19 19
 -(void) popToRoot:(NSString*)containerId;
20 20
 

+ 2
- 2
lib/ios/RNNCommandsHandler.m Voir le fichier

@@ -44,9 +44,9 @@
44 44
 	[_navigationStackManager pop:containerId];
45 45
 }
46 46
 
47
--(void) popTo:(NSString*)containerId fromContainerId:(NSString*)fromContainerId; {
47
+-(void) popTo:(NSString*)containerId {
48 48
 	[self assertReady];
49
-	[_navigationStackManager popTo:containerId fromContainerId:fromContainerId];
49
+	[_navigationStackManager popTo:containerId];
50 50
 }
51 51
 
52 52
 -(void) popToRoot:(NSString*)containerId {

+ 1
- 1
lib/ios/RNNNavigationStackManager.h Voir le fichier

@@ -8,7 +8,7 @@
8 8
 
9 9
 -(void)push:(UIViewController*)newTop onTop:(NSString*)containerId;
10 10
 -(void)pop:(NSString*)containerId;
11
--(void)popTo:(NSString*)toContainerId fromContainerId:(NSString*)fromContainerId;
11
+-(void)popTo:(NSString*)containerId;
12 12
 -(void)popToRoot:(NSString*)containerId;
13 13
 
14 14
 @end

+ 8
- 8
lib/ios/RNNNavigationStackManager.m Voir le fichier

@@ -28,15 +28,15 @@
28 28
 	[_store removeContainer:containerId];
29 29
 }
30 30
 
31
--(void)popTo:(NSString*)toContainerId fromContainerId:(NSString*)fromContainerId {
32
-	UIViewController *vc = [_store findContainerForId:fromContainerId];
33
-	UINavigationController *nvc = [vc navigationController];
34
-	
35
-	UIViewController *toVC = [_store findContainerForId:toContainerId];
31
+-(void)popTo:(NSString*)containerId {
32
+	UIViewController *vc = [_store findContainerForId:containerId];
36 33
 	
37
-	if (vc && toVC) {
38
-		NSArray *poppedVCs = [nvc popToViewController:toVC animated:YES];
39
-		[self removePopedViewControllers:poppedVCs];
34
+	if (vc) {
35
+		UINavigationController *nvc = [vc navigationController];
36
+		if(nvc) {
37
+			NSArray *poppedVCs = [nvc popToViewController:vc animated:YES];
38
+			[self removePopedViewControllers:poppedVCs];
39
+		}
40 40
 	}
41 41
 }
42 42
 

+ 1
- 1
lib/ios/ReactNativeNavigationTests/RNNNavigationStackManagerTest.m Voir le fichier

@@ -62,7 +62,7 @@
62 62
 
63 63
 - (void)testPopToSpecificVC_removeAllPopedVCFromStore {
64 64
 	self.nvc.willReturnVCs = @[self.vc2, self.vc3];
65
-	[self.uut popTo:@"vc1" fromContainerId:@"vc3"];
65
+	[self.uut popTo:@"vc1"];
66 66
 	
67 67
 	XCTAssertNil([self.store findContainerForId:@"vc2"]);
68 68
 	XCTAssertNil([self.store findContainerForId:@"vc3"]);

+ 23
- 12
lib/src/Navigation.js Voir le fichier

@@ -4,8 +4,7 @@ import UniqueIdProvider from './adapters/UniqueIdProvider';
4 4
 
5 5
 import Store from './containers/Store';
6 6
 import ContainerRegistry from './containers/ContainerRegistry';
7
-import AppCommands from './commands/AppCommands';
8
-import ContainerCommands from './commands/ContainerCommands';
7
+import Commands from './commands/Commands';
9 8
 import LayoutTreeParser from './commands/LayoutTreeParser';
10 9
 import LayoutTreeCrawler from './commands/LayoutTreeCrawler';
11 10
 import PrivateEventsListener from './events/PrivateEventsListener';
@@ -20,7 +19,7 @@ class Navigation {
20 19
     this.layoutTreeParser = new LayoutTreeParser();
21 20
     this.layoutTreeCrawler = new LayoutTreeCrawler(this.uniqueIdProvider, this.store);
22 21
     this.nativeCommandsSender = new NativeCommandsSender();
23
-    this.appCommands = new AppCommands(this.nativeCommandsSender, this.layoutTreeParser, this.layoutTreeCrawler);
22
+    this.commands = new Commands(this.nativeCommandsSender, this.layoutTreeParser, this.layoutTreeCrawler);
24 23
     this.publicEventsRegistry = new PublicEventsRegistry(this.nativeEventsReceiver);
25 24
     this.privateEventsListener = new PrivateEventsListener(this.nativeEventsReceiver, this.store);
26 25
     this.privateEventsListener.listenAndHandlePrivateEvents();
@@ -31,27 +30,39 @@ class Navigation {
31 30
   }
32 31
 
33 32
   setRoot(params) {
34
-    return this.appCommands.setRoot(params);
33
+    return this.commands.setRoot(params);
35 34
   }
36 35
 
37 36
   showModal(params) {
38
-    return this.appCommands.showModal(params);
37
+    return this.commands.showModal(params);
39 38
   }
40 39
 
41
-  dismissModal(id) {
42
-    return this.appCommands.dismissModal(id);
40
+  dismissModal(containerId) {
41
+    return this.commands.dismissModal(containerId);
43 42
   }
44 43
 
45 44
   dismissAllModals() {
46
-    return this.appCommands.dismissAllModals();
45
+    return this.commands.dismissAllModals();
47 46
   }
48 47
 
49
-  events() {
50
-    return this.publicEventsRegistry;
48
+  push(onContainerId, params) {
49
+    return this.commands.push(onContainerId, params);
50
+  }
51
+
52
+  pop(containerId) {
53
+    return this.commands.pop(containerId);
54
+  }
55
+
56
+  popTo(containerId) {
57
+    return this.commands.popTo(containerId);
58
+  }
59
+
60
+  popToRoot(containerId) {
61
+    return this.commands.popToRoot(containerId);
51 62
   }
52 63
 
53
-  on(containerId) {
54
-    return new ContainerCommands(containerId, this.nativeCommandsSender, this.layoutTreeParser, this.layoutTreeCrawler);
64
+  events() {
65
+    return this.publicEventsRegistry;
55 66
   }
56 67
 }
57 68
 

+ 51
- 20
lib/src/Navigation.test.js Voir le fichier

@@ -8,7 +8,7 @@ describe('Navigation', () => {
8 8
     jest.mock('./adapters/NativeEventsReceiver');
9 9
 
10 10
     jest.mock('./containers/ContainerRegistry');
11
-    jest.mock('./commands/AppCommands');
11
+    jest.mock('./commands/Commands');
12 12
 
13 13
     Navigation = require('./Navigation').default;
14 14
   });
@@ -22,30 +22,37 @@ describe('Navigation', () => {
22 22
   });
23 23
 
24 24
   it('setRoot delegates to Commands', async () => {
25
-    Navigation.appCommands.setRoot.mockReturnValue(Promise.resolve('result'));
25
+    Navigation.commands.setRoot.mockReturnValue(Promise.resolve('result'));
26 26
     const params = {};
27 27
     const result = await Navigation.setRoot(params);
28 28
     expect(result).toEqual('result');
29
-    expect(Navigation.appCommands.setRoot).toHaveBeenCalledTimes(1);
30
-    expect(Navigation.appCommands.setRoot).toHaveBeenCalledWith(params);
29
+    expect(Navigation.commands.setRoot).toHaveBeenCalledTimes(1);
30
+    expect(Navigation.commands.setRoot).toHaveBeenCalledWith(params);
31 31
   });
32 32
 
33
-  it('showModal delegates to AppCommands', async () => {
34
-    Navigation.appCommands.showModal.mockReturnValue(Promise.resolve('result'));
33
+  it('showModal delegates to Commands', async () => {
34
+    Navigation.commands.showModal.mockReturnValue(Promise.resolve('result'));
35 35
     const params = {};
36 36
     const result = await Navigation.showModal(params);
37 37
     expect(result).toEqual('result');
38
-    expect(Navigation.appCommands.showModal).toHaveBeenCalledTimes(1);
39
-    expect(Navigation.appCommands.showModal).toHaveBeenCalledWith(params);
38
+    expect(Navigation.commands.showModal).toHaveBeenCalledWith(params);
39
+    expect(Navigation.commands.showModal).toHaveBeenCalledTimes(1);
40 40
   });
41 41
 
42
-  it('dismissModal delegates to AppCommands', async () => {
43
-    Navigation.appCommands.dismissModal.mockReturnValue(Promise.resolve('result'));
42
+  it('dismissModal delegates to Commands', async () => {
43
+    Navigation.commands.dismissModal.mockReturnValue(Promise.resolve('result'));
44 44
     const params = {};
45 45
     const result = await Navigation.dismissModal(params);
46 46
     expect(result).toEqual('result');
47
-    expect(Navigation.appCommands.dismissModal).toHaveBeenCalledTimes(1);
48
-    expect(Navigation.appCommands.dismissModal).toHaveBeenCalledWith(params);
47
+    expect(Navigation.commands.dismissModal).toHaveBeenCalledTimes(1);
48
+    expect(Navigation.commands.dismissModal).toHaveBeenCalledWith(params);
49
+  });
50
+
51
+  it('dismissAllModals', async () => {
52
+    Navigation.commands.dismissAllModals.mockReturnValue(Promise.resolve('result'));
53
+    const result = await Navigation.dismissAllModals();
54
+    expect(Navigation.commands.dismissAllModals).toHaveBeenCalledTimes(1);
55
+    expect(result).toEqual('result');
49 56
   });
50 57
 
51 58
   it('events return public events', () => {
@@ -55,18 +62,42 @@ describe('Navigation', () => {
55 62
     expect(Navigation.nativeEventsReceiver.appLaunched).toHaveBeenCalledWith(cb);
56 63
   });
57 64
 
58
-  it('on containerId returns an object that performs commands on a container', () => {
59
-    expect(Navigation.on('myUniqueId').push).toBeInstanceOf(Function);
60
-  });
61
-
62 65
   it('starts listening and handles internal events', () => {
63 66
     expect(Navigation.nativeEventsReceiver.containerStart).toHaveBeenCalledTimes(1);
64 67
   });
65 68
 
66
-  it('dismissAllModals', async () => {
67
-    Navigation.appCommands.dismissAllModals.mockReturnValue(Promise.resolve('result'));
68
-    const result = await Navigation.dismissAllModals();
69
-    expect(Navigation.appCommands.dismissAllModals).toHaveBeenCalledTimes(1);
69
+  it('push delegates to commands', async () => {
70
+    Navigation.commands.push.mockReturnValue(Promise.resolve('result'));
71
+    const params = {};
72
+    const result = await Navigation.push('theContainerId', params);
73
+    expect(result).toEqual('result');
74
+    expect(Navigation.commands.push).toHaveBeenCalledWith('theContainerId', params);
75
+    expect(Navigation.commands.push).toHaveBeenCalledTimes(1);
76
+  });
77
+
78
+  it('pop delegates to commands', async () => {
79
+    Navigation.commands.pop.mockReturnValue(Promise.resolve('result'));
80
+    const result = await Navigation.pop('theContainerId');
81
+    expect(result).toEqual('result');
82
+    expect(Navigation.commands.pop).toHaveBeenCalledWith('theContainerId');
83
+    expect(Navigation.commands.pop).toHaveBeenCalledTimes(1);
84
+  });
85
+
86
+  it('popTo delegates to commands', async () => {
87
+    Navigation.commands.popTo.mockReturnValue(Promise.resolve('result'));
88
+    const params = {};
89
+    const result = await Navigation.popTo('theContainerId');
90
+    expect(result).toEqual('result');
91
+    expect(Navigation.commands.popTo).toHaveBeenCalledWith('theContainerId');
92
+    expect(Navigation.commands.popTo).toHaveBeenCalledTimes(1);
93
+  });
94
+
95
+  it('popToRoot delegates to commands', async () => {
96
+    Navigation.commands.popToRoot.mockReturnValue(Promise.resolve('result'));
97
+    const params = {};
98
+    const result = await Navigation.popToRoot('theContainerId');
70 99
     expect(result).toEqual('result');
100
+    expect(Navigation.commands.popToRoot).toHaveBeenCalledWith('theContainerId');
101
+    expect(Navigation.commands.popToRoot).toHaveBeenCalledTimes(1);
71 102
   });
72 103
 });

+ 6
- 6
lib/src/adapters/NativeCommandsSender.js Voir le fichier

@@ -20,9 +20,9 @@ export default class NativeCommandsSender {
20 20
     return Promise.resolve(containerId);
21 21
   }
22 22
 
23
-  popTo(containerId, targetContainerId) {
24
-    this.nativeCommandsModule.popTo(containerId, targetContainerId);
25
-    return Promise.resolve(targetContainerId);
23
+  popTo(containerId) {
24
+    this.nativeCommandsModule.popTo(containerId);
25
+    return Promise.resolve(containerId);
26 26
   }
27 27
 
28 28
   popToRoot(containerId) {
@@ -35,9 +35,9 @@ export default class NativeCommandsSender {
35 35
     return Promise.resolve(layout);
36 36
   }
37 37
 
38
-  dismissModal(id) {
39
-    this.nativeCommandsModule.dismissModal(id);
40
-    return Promise.resolve(id);
38
+  dismissModal(containerId) {
39
+    this.nativeCommandsModule.dismissModal(containerId);
40
+    return Promise.resolve(containerId);
41 41
   }
42 42
 
43 43
   dismissAllModals() {

lib/src/commands/AppCommands.js → lib/src/commands/Commands.js Voir le fichier

@@ -1,6 +1,6 @@
1 1
 import _ from 'lodash';
2 2
 
3
-export default class AppCommands {
3
+export default class Commands {
4 4
   constructor(nativeCommandsSender, layoutTreeParser, layoutTreeCrawler) {
5 5
     this.nativeCommandsSender = nativeCommandsSender;
6 6
     this.layoutTreeParser = layoutTreeParser;
@@ -28,5 +28,24 @@ export default class AppCommands {
28 28
   dismissAllModals() {
29 29
     return this.nativeCommandsSender.dismissAllModals();
30 30
   }
31
+
32
+  push(onContainerId, containerData) {
33
+    const input = _.cloneDeep(containerData);
34
+    const layout = this.layoutTreeParser.createContainer(input);
35
+    this.layoutTreeCrawler.crawl(layout);
36
+    return this.nativeCommandsSender.push(onContainerId, layout);
37
+  }
38
+
39
+  pop(containerId) {
40
+    return this.nativeCommandsSender.pop(containerId);
41
+  }
42
+
43
+  popTo(containerId) {
44
+    return this.nativeCommandsSender.popTo(containerId);
45
+  }
46
+
47
+  popToRoot(containerId) {
48
+    return this.nativeCommandsSender.popToRoot(containerId);
49
+  }
31 50
 }
32 51
 

lib/src/commands/AppCommands.test.js → lib/src/commands/Commands.test.js Voir le fichier

@@ -5,9 +5,9 @@ import Store from '../containers/Store';
5 5
 import UniqueIdProvider from '../adapters/UniqueIdProvider.mock';
6 6
 import NativeCommandsSender from '../adapters/NativeCommandsSender.mock';
7 7
 
8
-import AppCommands from './AppCommands';
8
+import Commands from './Commands';
9 9
 
10
-describe('AppCommands', () => {
10
+describe('Commands', () => {
11 11
   let uut;
12 12
   let mockCommandsSender;
13 13
   let store;
@@ -16,7 +16,7 @@ describe('AppCommands', () => {
16 16
     mockCommandsSender = new NativeCommandsSender();
17 17
     store = new Store();
18 18
 
19
-    uut = new AppCommands(mockCommandsSender, new LayoutTreeParser(), new LayoutTreeCrawler(new UniqueIdProvider(), store));
19
+    uut = new Commands(mockCommandsSender, new LayoutTreeParser(), new LayoutTreeCrawler(new UniqueIdProvider(), store));
20 20
   });
21 21
 
22 22
   describe('setRoot', () => {
@@ -139,4 +139,74 @@ describe('AppCommands', () => {
139 139
       expect(result).toEqual('the id');
140 140
     });
141 141
   });
142
+
143
+  describe('push', () => {
144
+    it('deep clones input to avoid mutation errors', () => {
145
+      const obj = {};
146
+      uut.push('theContainerId', { name: 'name', inner: { foo: obj } });
147
+      expect(mockCommandsSender.push.mock.calls[0][1].data.inner.foo).not.toBe(obj);
148
+    });
149
+
150
+    it('resolves with the parsed layout', async () => {
151
+      mockCommandsSender.push.mockReturnValue(Promise.resolve('the resolved layout'));
152
+      const result = await uut.push('theContainerId', { name: 'com.example.MyScreen' });
153
+      expect(result).toEqual('the resolved layout');
154
+    });
155
+
156
+    it('parses into correct layout node and sends to native', () => {
157
+      uut.push('theContainerId', { name: 'com.example.MyScreen' });
158
+      expect(mockCommandsSender.push).toHaveBeenCalledTimes(1);
159
+      expect(mockCommandsSender.push).toHaveBeenCalledWith('theContainerId', {
160
+        type: 'Container',
161
+        id: 'Container+UNIQUE_ID',
162
+        data: {
163
+          name: 'com.example.MyScreen',
164
+          navigationStyle: {}
165
+        },
166
+        children: []
167
+      });
168
+    });
169
+  });
170
+
171
+  describe('pop', () => {
172
+    it('pops a container, passing containerId', () => {
173
+      uut.pop('theContainerId');
174
+      expect(mockCommandsSender.pop).toHaveBeenCalledTimes(1);
175
+      expect(mockCommandsSender.pop).toHaveBeenCalledWith('theContainerId');
176
+    });
177
+
178
+    it('pop returns a promise that resolves to containerId', async () => {
179
+      mockCommandsSender.pop.mockReturnValue(Promise.resolve('theContainerId'));
180
+      const result = await uut.pop('theContainerId');
181
+      expect(result).toEqual('theContainerId');
182
+    });
183
+  });
184
+
185
+  describe('popTo', () => {
186
+    it('pops all containers until the passed Id is top', () => {
187
+      uut.popTo('theContainerId');
188
+      expect(mockCommandsSender.popTo).toHaveBeenCalledTimes(1);
189
+      expect(mockCommandsSender.popTo).toHaveBeenCalledWith('theContainerId');
190
+    });
191
+
192
+    it('returns a promise that resolves to targetId', async () => {
193
+      mockCommandsSender.popTo.mockReturnValue(Promise.resolve('theContainerId'));
194
+      const result = await uut.popTo('theContainerId');
195
+      expect(result).toEqual('theContainerId');
196
+    });
197
+  });
198
+
199
+  describe('popToRoot', () => {
200
+    it('pops all containers to root', () => {
201
+      uut.popToRoot('theContainerId');
202
+      expect(mockCommandsSender.popToRoot).toHaveBeenCalledTimes(1);
203
+      expect(mockCommandsSender.popToRoot).toHaveBeenCalledWith('theContainerId');
204
+    });
205
+
206
+    it('returns a promise that resolves to targetId', async () => {
207
+      mockCommandsSender.popToRoot.mockReturnValue(Promise.resolve('theContainerId'));
208
+      const result = await uut.popToRoot('theContainerId');
209
+      expect(result).toEqual('theContainerId');
210
+    });
211
+  });
142 212
 });

+ 0
- 29
lib/src/commands/ContainerCommands.js Voir le fichier

@@ -1,29 +0,0 @@
1
-import _ from 'lodash';
2
-
3
-export default class ContainerCommands {
4
-  constructor(containerId, nativeCommandsSender, layoutTreeParser, layoutTreeCrawler) {
5
-    this.containerId = containerId;
6
-    this.nativeCommandsSender = nativeCommandsSender;
7
-    this.layoutTreeParser = layoutTreeParser;
8
-    this.layoutTreeCrawler = layoutTreeCrawler;
9
-  }
10
-
11
-  push(containerData) {
12
-    const input = _.cloneDeep(containerData);
13
-    const layout = this.layoutTreeParser.createContainer(input);
14
-    this.layoutTreeCrawler.crawl(layout);
15
-    return this.nativeCommandsSender.push(this.containerId, layout);
16
-  }
17
-
18
-  pop() {
19
-    return this.nativeCommandsSender.pop(this.containerId);
20
-  }
21
-
22
-  popTo(toContainerId) {
23
-    return this.nativeCommandsSender.popTo(this.containerId, toContainerId);
24
-  }
25
-
26
-  popToRoot() {
27
-    return this.nativeCommandsSender.popToRoot(this.containerId);
28
-  }
29
-}

+ 0
- 92
lib/src/commands/ContainerCommands.test.js Voir le fichier

@@ -1,92 +0,0 @@
1
-import NativeCommandsSender from '../adapters/NativeCommandsSender.mock';
2
-import UniqueIdProvider from '../adapters/UniqueIdProvider.mock';
3
-import Store from '../containers/Store';
4
-import LayoutTreeParser from './LayoutTreeParser';
5
-import LayoutTreeCrawler from './LayoutTreeCrawler';
6
-
7
-import ContainerCommands from './ContainerCommands';
8
-
9
-describe('ContainerCommands', () => {
10
-  let uut;
11
-  let mockCommandsSender;
12
-  const containerId = 'myUniqueId';
13
-
14
-  beforeEach(() => {
15
-    mockCommandsSender = new NativeCommandsSender();
16
-    uut = new ContainerCommands(containerId, mockCommandsSender, new LayoutTreeParser(), new LayoutTreeCrawler(new UniqueIdProvider(), new Store()));
17
-  });
18
-
19
-  it('holds containerId', () => {
20
-    expect(uut.containerId).toEqual('myUniqueId');
21
-  });
22
-
23
-  describe('push', () => {
24
-    it('deep clones input to avoid mutation errors', () => {
25
-      const obj = {};
26
-      uut.push({ name: 'name', inner: { foo: obj } });
27
-      expect(mockCommandsSender.push.mock.calls[0][1].data.inner.foo).not.toBe(obj);
28
-    });
29
-
30
-    it('resolves with the parsed layout', async () => {
31
-      mockCommandsSender.push.mockReturnValue(Promise.resolve('the resolved layout'));
32
-      const result = await uut.push({ name: 'com.example.MyScreen' });
33
-      expect(result).toEqual('the resolved layout');
34
-    });
35
-
36
-    it('parses into correct layout node and sends to native', () => {
37
-      uut.push({ name: 'com.example.MyScreen' });
38
-      expect(mockCommandsSender.push).toHaveBeenCalledTimes(1);
39
-      expect(mockCommandsSender.push).toHaveBeenCalledWith('myUniqueId', {
40
-        type: 'Container',
41
-        id: 'Container+UNIQUE_ID',
42
-        data: {
43
-          name: 'com.example.MyScreen',
44
-          navigationStyle: {}
45
-        },
46
-        children: []
47
-      });
48
-    });
49
-  });
50
-
51
-  describe('pop', () => {
52
-    it('pops a container, passing containerId', () => {
53
-      uut.pop();
54
-      expect(mockCommandsSender.pop).toHaveBeenCalledTimes(1);
55
-      expect(mockCommandsSender.pop).toHaveBeenCalledWith(containerId);
56
-    });
57
-
58
-    it('pop returns a promise that resolves to containerId', async () => {
59
-      mockCommandsSender.pop.mockReturnValue(Promise.resolve(containerId));
60
-      const result = await uut.pop();
61
-      expect(result).toEqual(containerId);
62
-    });
63
-  });
64
-
65
-  describe('popTo', () => {
66
-    it('pops all containers until the passed Id is top', () => {
67
-      uut.popTo('someOtherContainerId');
68
-      expect(mockCommandsSender.popTo).toHaveBeenCalledTimes(1);
69
-      expect(mockCommandsSender.popTo).toHaveBeenCalledWith(containerId, 'someOtherContainerId');
70
-    });
71
-
72
-    it('returns a promise that resolves to targetId', async () => {
73
-      mockCommandsSender.popTo.mockReturnValue(Promise.resolve(containerId));
74
-      const result = await uut.popTo('someOtherContainerId');
75
-      expect(result).toEqual(containerId);
76
-    });
77
-  });
78
-
79
-  describe('popToRoot', () => {
80
-    it('pops all containers to root', () => {
81
-      uut.popToRoot();
82
-      expect(mockCommandsSender.popToRoot).toHaveBeenCalledTimes(1);
83
-      expect(mockCommandsSender.popToRoot).toHaveBeenCalledWith(containerId);
84
-    });
85
-
86
-    it('returns a promise that resolves to targetId', async () => {
87
-      mockCommandsSender.popToRoot.mockReturnValue(Promise.resolve(containerId));
88
-      const result = await uut.popToRoot();
89
-      expect(result).toEqual(containerId);
90
-    });
91
-  });
92
-});

+ 1
- 1
playground/src/containers/LifecycleScreen.js Voir le fichier

@@ -36,7 +36,7 @@ class LifecycleScreen extends Component {
36 36
   }
37 37
 
38 38
   onClickPush() {
39
-    Navigation.on(this.props.id).push({
39
+    Navigation.push(this.props.id, {
40 40
       name: 'navigation.playground.TextScreen'
41 41
     });
42 42
   }

+ 5
- 5
playground/src/containers/PushedScreen.js Voir le fichier

@@ -39,7 +39,7 @@ class PushedScreen extends Component {
39 39
   }
40 40
 
41 41
   onClickPush() {
42
-    Navigation.on(this.props.id).push({
42
+    Navigation.push(this.props.id, {
43 43
       name: 'navigation.playground.PushedScreen',
44 44
       passProps: {
45 45
         stackPosition: this.getStackPosition() + 1,
@@ -49,19 +49,19 @@ class PushedScreen extends Component {
49 49
   }
50 50
 
51 51
   onClickPop() {
52
-    Navigation.on(this.props.id).pop();
52
+    Navigation.pop(this.props.id);
53 53
   }
54 54
 
55 55
   onClickPopPrevious() {
56
-    Navigation.on(_.last(this.props.previousScreenIds)).pop();
56
+    Navigation.pop(_.last(this.props.previousScreenIds));
57 57
   }
58 58
 
59 59
   onClickPopToFirstPosition() {
60
-    Navigation.on(this.props.id).popTo(this.props.previousScreenIds[0]);
60
+    Navigation.popTo(this.props.previousScreenIds[0]);
61 61
   }
62 62
 
63 63
   onClickPopToRoot() {
64
-    Navigation.on(this.props.id).popToRoot();
64
+    Navigation.popToRoot(this.props.id);
65 65
   }
66 66
 
67 67
   getStackPosition() {

+ 2
- 2
playground/src/containers/WelcomeScreen.js Voir le fichier

@@ -100,13 +100,13 @@ class WelcomeScreen extends Component {
100 100
   }
101 101
 
102 102
   onClickPush() {
103
-    Navigation.on(this.props.id).push({
103
+    Navigation.push(this.props.id, {
104 104
       name: 'navigation.playground.PushedScreen'
105 105
     });
106 106
   }
107 107
 
108 108
   onClickLifecycleScreen() {
109
-    Navigation.on(this.props.id).push({
109
+    Navigation.push(this.props.id, {
110 110
       name: 'navigation.playground.LifecycleScreen'
111 111
     });
112 112
   }