ReactMapDemoTests.m 1.8 KB

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