mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-17 12:19:12 +08:00
Added tests for synchronous methods in native modules on iOS
Reviewed By: javache Differential Revision: D4947631 fbshipit-source-id: d7e497c44602eb6e38896a00edb61639ab2b8cd4
This commit is contained in:
committed by
Facebook Github Bot
parent
db0c22192c
commit
971b083c6a
@@ -48,10 +48,22 @@ static RCTModuleMethod *buildDefaultMethodWithMethodSignature(NSString *methodSi
|
||||
moduleClass:[RCTModuleMethodTests class]];
|
||||
}
|
||||
|
||||
static RCTModuleMethod *buildSyncMethodWithMethodSignature(NSString *methodSignature) {
|
||||
return [[RCTModuleMethod alloc] initWithMethodSignature:methodSignature
|
||||
JSMethodName:nil
|
||||
isSync:YES
|
||||
moduleClass:[RCTModuleMethodTests class]];
|
||||
}
|
||||
|
||||
+ (NSString *)moduleName { return nil; }
|
||||
|
||||
- (void)doFoo { }
|
||||
|
||||
- (void)doFooWithBar:(__unused NSString *)bar { }
|
||||
|
||||
- (id)echoString:(NSString *)input { return input; }
|
||||
- (id)methodThatReturnsNil { return nil; }
|
||||
|
||||
- (void)testNonnull
|
||||
{
|
||||
NSString *methodSignature = @"doFooWithBar:(nonnull NSString *)bar";
|
||||
@@ -135,4 +147,71 @@ static RCTModuleMethod *buildDefaultMethodWithMethodSignature(NSString *methodSi
|
||||
}));
|
||||
}
|
||||
|
||||
- (void)testFunctionType
|
||||
{
|
||||
{
|
||||
NSString *methodSignature = @"doFoo";
|
||||
RCTModuleMethod *method = buildDefaultMethodWithMethodSignature(methodSignature);
|
||||
XCTAssertTrue(method.functionType == RCTFunctionTypeNormal);
|
||||
}
|
||||
|
||||
{
|
||||
NSString *methodSignature = @"openURL:(NSURL *)URL resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject";
|
||||
RCTModuleMethod *method = buildDefaultMethodWithMethodSignature(methodSignature);
|
||||
XCTAssertTrue(method.functionType == RCTFunctionTypePromise);
|
||||
}
|
||||
|
||||
{
|
||||
NSString *methodSignature = @"echoString:(NSString *)input";
|
||||
RCTModuleMethod *method = buildSyncMethodWithMethodSignature(methodSignature);
|
||||
XCTAssertTrue(method.functionType == RCTFunctionTypeSync);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)testReturnsValueForSyncFunction
|
||||
{
|
||||
{
|
||||
NSString *methodSignature = @"echoString:(NSString *)input";
|
||||
RCTModuleMethod *method = buildSyncMethodWithMethodSignature(methodSignature);
|
||||
id result = [method invokeWithBridge:nil module:self arguments:@[@"Test String Value"]];
|
||||
XCTAssertEqualObjects(result, @"Test String Value");
|
||||
}
|
||||
|
||||
{
|
||||
NSString *methodSignature = @"methodThatReturnsNil";
|
||||
RCTModuleMethod *method = buildSyncMethodWithMethodSignature(methodSignature);
|
||||
id result = [method invokeWithBridge:nil module:self arguments:@[]];
|
||||
XCTAssertNil(result);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)testReturnsNilForDefaultFunction
|
||||
{
|
||||
NSString *methodSignature = @"doFoo";
|
||||
RCTModuleMethod *method = buildDefaultMethodWithMethodSignature(methodSignature);
|
||||
id result = [method invokeWithBridge:nil module:self arguments:@[]];
|
||||
XCTAssertNil(result);
|
||||
}
|
||||
|
||||
- (void)testReturnTypeForSyncFunction
|
||||
{
|
||||
{
|
||||
NSString *methodSignature = @"methodThatReturnsNil";
|
||||
RCTModuleMethod *method = buildSyncMethodWithMethodSignature(methodSignature);
|
||||
XCTAssertFalse(RCTLogsError(^{
|
||||
// Invoke method to trigger parsing
|
||||
__unused SEL selector = method.selector;
|
||||
}), @"Unexpected error when parsing sync function with (id) return type");
|
||||
}
|
||||
|
||||
{
|
||||
NSString *methodSignature = @"doFoo";
|
||||
RCTModuleMethod *method = buildSyncMethodWithMethodSignature(methodSignature);
|
||||
XCTAssertTrue(RCTLogsError(^{
|
||||
// Invoke method to trigger parsing
|
||||
__unused SEL selector = method.selector;
|
||||
}), @"Failed to trigger an error when parsing sync function with non-(id) return type");
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user