react-native/Libraries/RCTTest/RCTTestRunner.m

105 lines
3.8 KiB
Mathematica
Raw Normal View History

/**
* 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 "RCTTestRunner.h"
2015-03-24 17:20:13 +00:00
#import "FBSnapshotTestController.h"
#import "RCTRedBox.h"
#import "RCTRootView.h"
#import "RCTTestModule.h"
#import "RCTUtils.h"
#define TIMEOUT_SECONDS 240
@interface RCTBridge (RCTTestRunner)
@property (nonatomic, weak) RCTBridge *batchedBridge;
@end
@implementation RCTTestRunner
2015-03-24 17:20:13 +00:00
{
FBSnapshotTestController *_testController;
2015-03-24 17:20:13 +00:00
}
2015-03-24 17:20:13 +00:00
- (instancetype)initWithApp:(NSString *)app referenceDir:(NSString *)referenceDir
{
2015-03-24 17:20:13 +00:00
if ((self = [super init])) {
NSString *sanitizedAppName = [app stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
sanitizedAppName = [sanitizedAppName stringByReplacingOccurrencesOfString:@"\\" withString:@"-"];
_testController = [[FBSnapshotTestController alloc] initWithTestName:sanitizedAppName];
_testController.referenceImagesDirectory = referenceDir;
2015-04-11 22:08:00 +00:00
_scriptURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8081/%@.includeRequire.runModule.bundle?dev=true", app]];
}
return self;
}
2015-03-24 17:20:13 +00:00
- (void)setRecordMode:(BOOL)recordMode
{
_testController.recordMode = recordMode;
2015-03-24 17:20:13 +00:00
}
- (BOOL)recordMode
{
return _testController.recordMode;
}
2015-03-24 17:20:13 +00:00
- (void)runTest:(SEL)test module:(NSString *)moduleName
{
2015-03-24 17:20:13 +00:00
[self runTest:test module:moduleName initialProps:nil expectErrorBlock:nil];
}
- (void)runTest:(SEL)test module:(NSString *)moduleName
initialProps:(NSDictionary *)initialProps expectErrorRegex:(NSString *)errorRegex
2015-03-24 17:20:13 +00:00
{
[self runTest:test module:moduleName initialProps:initialProps expectErrorBlock:^BOOL(NSString *error){
2015-04-11 22:08:00 +00:00
return [error rangeOfString:errorRegex options:NSRegularExpressionSearch].location != NSNotFound;
}];
}
2015-03-24 17:20:13 +00:00
- (void)runTest:(SEL)test module:(NSString *)moduleName initialProps:(NSDictionary *)initialProps expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock
{
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:_scriptURL
moduleName:moduleName
launchOptions:nil];
rootView.initialProperties = initialProps;
rootView.frame = CGRectMake(0, 0, 320, 2000); // Constant size for testing on multiple devices
2015-03-26 01:59:42 +00:00
NSString *testModuleName = RCTBridgeModuleNameForClass([RCTTestModule class]);
RCTTestModule *testModule = rootView.bridge.batchedBridge.modules[testModuleName];
testModule.controller = _testController;
2015-03-24 17:20:13 +00:00
testModule.testSelector = test;
2015-04-02 14:33:21 +00:00
testModule.view = rootView;
UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController;
vc.view = [[UIView alloc] init];
2015-03-24 17:20:13 +00:00
[vc.view addSubview:rootView]; // Add as subview so it doesn't get resized
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:TIMEOUT_SECONDS];
NSString *error = [[RCTRedBox sharedInstance] currentErrorMessage];
while ([date timeIntervalSinceNow] > 0 && ![testModule isDone] && error == nil) {
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:date];
[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:date];
error = [[RCTRedBox sharedInstance] currentErrorMessage];
}
2015-03-24 17:20:13 +00:00
[rootView removeFromSuperview];
RCTAssert(vc.view.subviews.count == 0, @"There shouldn't be any other views: %@", vc.view);
vc.view = nil;
[[RCTRedBox sharedInstance] dismiss];
if (expectErrorBlock) {
2015-03-24 17:20:13 +00:00
RCTAssert(expectErrorBlock(error), @"Expected an error but nothing matched.");
} else if (error) {
RCTAssert(error == nil, @"RedBox error: %@", error);
} else {
RCTAssert([testModule isDone], @"Test didn't finish within %d seconds", TIMEOUT_SECONDS);
}
}
@end