Skip to content

Commit 3a42661

Browse files
Pawel Sienkowskifacebook-github-bot-3
authored andcommitted
Custom logic blocks for RCTTestRunner's runTest: method
Reviewed By: majak Differential Revision: D2626497 fb-gh-sync-id: c78e3b47be4ee192e899594bd05b13521af7172e
1 parent 760a2fc commit 3a42661

4 files changed

Lines changed: 41 additions & 14 deletions

File tree

Examples/UIExplorer/UIExplorerIntegrationTests/RCTUIManagerScenarioTests.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1313
*/
1414

15-
1615
#import <UIKit/UIKit.h>
1716
#import <XCTest/XCTest.h>
1817
#import "RCTSparseArray.h"

Examples/UIExplorer/UIExplorerIntegrationTests/UIExplorerIntegrationTests.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ - (void)testTheTester_waitOneFrame
4747
[_runner runTest:_cmd
4848
module:@"IntegrationTestHarnessTest"
4949
initialProps:@{@"waitOneFrame": @YES}
50-
expectErrorBlock:nil];
50+
configurationBlock:nil];
5151
}
5252

5353
- (void)testTheTester_ExpectError
5454
{
5555
[_runner runTest:_cmd
5656
module:@"IntegrationTestHarnessTest"
5757
initialProps:@{@"shouldThrow": @YES}
58+
configurationBlock:nil
5859
expectErrorRegex:@"because shouldThrow"];
5960
}
6061

Libraries/RCTTest/RCTTestRunner.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
@protocol RCTBridgeModule;
2626

27+
@class RCTRootView;
28+
2729
@interface RCTTestRunner : NSObject
2830

2931
@property (nonatomic, assign) BOOL recordMode;
@@ -59,26 +61,42 @@
5961

6062
/**
6163
* Same as runTest:, but allows for passing initialProps for providing mock data
62-
* or requesting different behaviors, and expectErrorRegex verifies that the
63-
* error you expected was thrown.
64+
* or requesting different behaviors, configurationBlock provides arbitrary logic for the hosting
65+
* root view manipulation.
66+
*
67+
* @param test Selector of the test, usually just `_cmd`.
68+
* @param moduleName Name of the JS component as registered by `AppRegistry.registerComponent` in JS.
69+
* @param initialProps props that are passed into the component when rendered.
70+
* @param configurationBlock A block that takes the hosting root view and performs arbitrary manipulation after its creation.
71+
*/
72+
73+
- (void)runTest:(SEL)test module:(NSString *)moduleName initialProps:(NSDictionary *)initialProps configurationBlock:(void(^)(RCTRootView *rootView))configurationBlock;
74+
75+
/**
76+
* Same as runTest:, but allows for passing initialProps for providing mock data
77+
* or requesting different behaviors, configurationBlock provides arbitrary logic for the hosting
78+
* root view manipulation, and expectErrorRegex verifies that the error you expected was thrown.
6479
*
6580
* @param test Selector of the test, usually just `_cmd`.
6681
* @param moduleName Name of the JS component as registered by `AppRegistry.registerComponent` in JS.
6782
* @param initialProps props that are passed into the component when rendered.
83+
* @param configurationBlock A block that takes the hosting root view and performs arbitrary manipulation after its creation.
6884
* @param expectErrorRegex A regex that must match the error thrown. If no error is thrown, the test fails.
6985
*/
70-
- (void)runTest:(SEL)test module:(NSString *)moduleName initialProps:(NSDictionary *)initialProps expectErrorRegex:(NSString *)expectErrorRegex;
86+
- (void)runTest:(SEL)test module:(NSString *)moduleName initialProps:(NSDictionary *)initialProps configurationBlock:(void(^)(RCTRootView *rootView))configurationBlock expectErrorRegex:(NSString *)expectErrorRegex;
7187

7288
/**
7389
* Same as runTest:, but allows for passing initialProps for providing mock data
74-
* or requesting different behaviors, and expectErrorBlock provides arbitrary
90+
* or requesting different behaviors, configurationBlock provides arbitrary logic for the hosting
91+
* root view manipulation, and expectErrorBlock provides arbitrary
7592
* logic for processing errors (nil will cause any error to fail the test).
7693
*
7794
* @param test Selector of the test, usually just `_cmd`.
7895
* @param moduleName Name of the JS component as registered by `AppRegistry.registerComponent` in JS.
7996
* @param initialProps props that are passed into the component when rendered.
97+
* @param configurationBlock A block that takes the hosting root view and performs arbitrary manipulation after its creation.
8098
* @param expectErrorBlock A block that takes the error message and returns NO to fail the test.
8199
*/
82-
- (void)runTest:(SEL)test module:(NSString *)moduleName initialProps:(NSDictionary *)initialProps expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock;
100+
- (void)runTest:(SEL)test module:(NSString *)moduleName initialProps:(NSDictionary *)initialProps configurationBlock:(void(^)(RCTRootView *rootView))configurationBlock expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock;
83101

84102
@end

Libraries/RCTTest/RCTTestRunner.m

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,24 @@ - (BOOL)recordMode
6868

6969
- (void)runTest:(SEL)test module:(NSString *)moduleName
7070
{
71-
[self runTest:test module:moduleName initialProps:nil expectErrorBlock:nil];
71+
[self runTest:test module:moduleName initialProps:nil configurationBlock:nil expectErrorBlock:nil];
7272
}
7373

74-
- (void)runTest:(SEL)test module:(NSString *)moduleName
75-
initialProps:(NSDictionary *)initialProps expectErrorRegex:(NSString *)errorRegex
74+
- (void)runTest:(SEL)test module:(NSString *)moduleName initialProps:(NSDictionary *)initialProps configurationBlock:(void(^)(RCTRootView *rootView))configurationBlock
75+
{
76+
[self runTest:test module:moduleName initialProps:initialProps configurationBlock:configurationBlock expectErrorBlock:nil];
77+
}
78+
79+
- (void)runTest:(SEL)test module:(NSString *)moduleName initialProps:(NSDictionary *)initialProps configurationBlock:(void(^)(RCTRootView *rootView))configurationBlock expectErrorRegex:(NSString *)errorRegex
7680
{
77-
[self runTest:test module:moduleName initialProps:initialProps expectErrorBlock:^BOOL(NSString *error){
81+
BOOL(^expectErrorBlock)(NSString *error) = ^BOOL(NSString *error){
7882
return [error rangeOfString:errorRegex options:NSRegularExpressionSearch].location != NSNotFound;
79-
}];
83+
};
84+
85+
[self runTest:test module:moduleName initialProps:initialProps configurationBlock:configurationBlock expectErrorBlock:expectErrorBlock];
8086
}
8187

82-
- (void)runTest:(SEL)test module:(NSString *)moduleName
83-
initialProps:(NSDictionary *)initialProps expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock
88+
- (void)runTest:(SEL)test module:(NSString *)moduleName initialProps:(NSDictionary *)initialProps configurationBlock:(void(^)(RCTRootView *rootView))configurationBlock expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock
8489
{
8590
__weak id weakJSContext;
8691

@@ -110,6 +115,10 @@ - (void)runTest:(SEL)test module:(NSString *)moduleName
110115
vc.view = [UIView new];
111116
[vc.view addSubview:rootView]; // Add as subview so it doesn't get resized
112117

118+
if (configurationBlock) {
119+
configurationBlock(rootView);
120+
}
121+
113122
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:kTestTimeoutSeconds];
114123
while (date.timeIntervalSinceNow > 0 && testModule.status == RCTTestStatusPending && error == nil) {
115124
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];

0 commit comments

Comments
 (0)