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:
Alex Dvornikov 2017-04-27 11:49:50 -07:00 committed by Facebook Github Bot
parent db0c22192c
commit 971b083c6a
5 changed files with 155 additions and 0 deletions

View File

@ -71,6 +71,7 @@ RCT_TEST(ImageCachePolicyTest)
RCT_TEST(ImageSnapshotTest)
//RCT_TEST(LayoutEventsTest) // Disabled due to flakiness: #8686784
RCT_TEST(SimpleSnapshotTest)
RCT_TEST(SyncMethodTest)
RCT_TEST(PromiseTest)
RCT_TEST_ONLY_WITH_PACKAGER(WebSocketTest)

View File

@ -0,0 +1,32 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
@interface UIExplorerTestModule : NSObject <RCTBridgeModule>
@end
@implementation UIExplorerTestModule
RCT_EXPORT_MODULE(UIExplorerTestModule)
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(echoString:(NSString *)input)
{
return input;
}
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(methodThatReturnsNil)
{
return nil;
}
@end

View File

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

View File

@ -33,6 +33,7 @@ var TESTS = [
require('./ImageCachePolicyTest'),
require('./ImageSnapshotTest'),
require('./PromiseTest'),
require('./SyncMethodTest'),
require('./WebSocketTest'),
];

View File

@ -0,0 +1,42 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
* @providesModule SyncMethodTest
*/
'use strict';
var React = require('react');
var ReactNative = require('react-native');
var { View } = ReactNative;
const {
TestModule,
UIExplorerTestModule,
} = ReactNative.NativeModules;
class SyncMethodTest extends React.Component {
componentDidMount() {
if (UIExplorerTestModule.echoString('test string value') !== 'test string value') {
throw new Error('Something wrong with sync method export');
}
if (UIExplorerTestModule.methodThatReturnsNil() != null) {
throw new Error('Something wrong with sync method export');
}
TestModule.markTestCompleted();
}
render(): React.Element<any> {
return <View />;
}
}
SyncMethodTest.displayName = 'SyncMethodTest';
module.exports = SyncMethodTest;