Permalink
Browse files

Added tests for synchronous methods in native modules on iOS

Reviewed By: javache

Differential Revision: D4947631

fbshipit-source-id: d7e497c44602eb6e38896a00edb61639ab2b8cd4
  • Loading branch information...
fromcelticpark authored and facebook-github-bot committed Apr 27, 2017
1 parent db0c221 commit 971b083c6abe2158c6015cf658b8090b8f29d4f1
@@ -71,6 +71,7 @@ - (void)testTheTester_ExpectError
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)
@@ -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
@@ -48,10 +48,22 @@ @implementation RCTModuleMethodTests
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 @@ - (void)testWhitespaceTolerance
}));
}
- (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
@@ -33,6 +33,7 @@ var TESTS = [
require('./ImageCachePolicyTest'),
require('./ImageSnapshotTest'),
require('./PromiseTest'),
require('./SyncMethodTest'),
require('./WebSocketTest'),
];
@@ -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;

0 comments on commit 971b083

Please sign in to comment.