1#import <UIKit/UIKit.h> 2#import <XCTest/XCTest.h> 3 4#import <React/RCTLog.h> 5#import <React/RCTRootView.h> 6 7#define TIMEOUT_SECONDS 600 8#define TEXT_TO_LOOK_FOR @"Welcome to React" 9 10@interface MusicFreeTests : XCTestCase 11 12@end 13 14@implementation MusicFreeTests 15 16- (BOOL)findSubviewInView:(UIView *)view matching:(BOOL (^)(UIView *view))test 17{ 18 if (test(view)) { 19 return YES; 20 } 21 for (UIView *subview in [view subviews]) { 22 if ([self findSubviewInView:subview matching:test]) { 23 return YES; 24 } 25 } 26 return NO; 27} 28 29- (void)testRendersWelcomeScreen 30{ 31 UIViewController *vc = [[[RCTSharedApplication() delegate] window] rootViewController]; 32 NSDate *date = [NSDate dateWithTimeIntervalSinceNow:TIMEOUT_SECONDS]; 33 BOOL foundElement = NO; 34 35 __block NSString *redboxError = nil; 36#ifdef DEBUG 37 RCTSetLogFunction( 38 ^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) { 39 if (level >= RCTLogLevelError) { 40 redboxError = message; 41 } 42 }); 43#endif 44 45 while ([date timeIntervalSinceNow] > 0 && !foundElement && !redboxError) { 46 [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; 47 [[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; 48 49 foundElement = [self findSubviewInView:vc.view 50 matching:^BOOL(UIView *view) { 51 if ([view.accessibilityLabel isEqualToString:TEXT_TO_LOOK_FOR]) { 52 return YES; 53 } 54 return NO; 55 }]; 56 } 57 58#ifdef DEBUG 59 RCTSetLogFunction(RCTDefaultLogFunction); 60#endif 61 62 XCTAssertNil(redboxError, @"RedBox error: %@", redboxError); 63 XCTAssertTrue(foundElement, @"Couldn't find element with text '%@' in %d seconds", TEXT_TO_LOOK_FOR, TIMEOUT_SECONDS); 64} 65 66@end 67