Browse Source

Reject pop command when viewController not found in the hierarchy (#6048)

Until now the promise was rejected only on Android.
Yogev Ben David 4 years ago
parent
commit
4413aa4a76
No account linked to committer's email address

+ 11
- 7
lib/ios/RNNCommandsHandler.m View File

202
 	RNNAssertMainQueue();
202
 	RNNAssertMainQueue();
203
     
203
     
204
 	RNNComponentViewController *vc = (RNNComponentViewController*)[RNNLayoutManager findComponentForId:componentId];
204
 	RNNComponentViewController *vc = (RNNComponentViewController*)[RNNLayoutManager findComponentForId:componentId];
205
-	RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
206
-	[vc overrideOptions:options];
207
-	
208
-	[vc.stack pop:vc animated:[vc.resolveOptionsWithDefault.animations.pop.enable getWithDefaultValue:YES] completion:^{
209
-		[_eventEmitter sendOnNavigationCommandCompletion:pop commandId:commandId params:@{@"componentId": componentId}];
210
-		completion();
211
-	} rejection:rejection];
205
+    if (vc) {
206
+        RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
207
+        [vc overrideOptions:options];
208
+        
209
+        [vc.stack pop:vc animated:[vc.resolveOptionsWithDefault.animations.pop.enable getWithDefaultValue:YES] completion:^{
210
+            [self->_eventEmitter sendOnNavigationCommandCompletion:pop commandId:commandId params:@{@"componentId": componentId}];
211
+            completion();
212
+        } rejection:rejection];
213
+    } else {
214
+        [RNNErrorHandler reject:rejection withErrorCode:1012 errorDescription:[NSString stringWithFormat:@"Popping component failed - componentId '%@' not found", componentId]];
215
+    }
212
 }
216
 }
213
 
217
 
214
 - (void)popTo:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
218
 - (void)popTo:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {

+ 15
- 0
playground/ios/NavigationTests/RNNCommandsHandlerTest.m View File

454
 	[self.modalManager verify];
454
 	[self.modalManager verify];
455
 }
455
 }
456
 
456
 
457
+- (void)testPop_shouldRejectPromiseForInvalidComponentId {
458
+	[self.uut setReadyToReceiveCommands:true];
459
+	
460
+    XCTestExpectation *expectation = [self expectationWithDescription:@"Should invoke reject block"];
461
+
462
+	[self.uut pop:@"invalidComponentId" commandId:@"pop" mergeOptions:nil completion:^{
463
+		[expectation fulfill];
464
+	} rejection:^(NSString *code, NSString *message, NSError *error) {
465
+		XCTAssert([code isEqualToString:@"1012"]);
466
+		XCTAssert([message isEqualToString:@"Popping component failed - componentId 'invalidComponentId' not found"]);
467
+		[expectation fulfill];
468
+	}];
469
+	
470
+	[self waitForExpectationsWithTimeout:5 handler:nil];
471
+}
457
 
472
 
458
 @end
473
 @end