You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exampleTests.m 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import <UIKit/UIKit.h>
  10. #import <XCTest/XCTest.h>
  11. #import <React/RCTLog.h>
  12. #import <React/RCTRootView.h>
  13. #define TIMEOUT_SECONDS 600
  14. #define TEXT_TO_LOOK_FOR @"Welcome to React Native!"
  15. @interface exampleTests : XCTestCase
  16. @end
  17. @implementation exampleTests
  18. - (BOOL)findSubviewInView:(UIView *)view matching:(BOOL(^)(UIView *view))test
  19. {
  20. if (test(view)) {
  21. return YES;
  22. }
  23. for (UIView *subview in [view subviews]) {
  24. if ([self findSubviewInView:subview matching:test]) {
  25. return YES;
  26. }
  27. }
  28. return NO;
  29. }
  30. - (void)testRendersWelcomeScreen
  31. {
  32. UIViewController *vc = [[[RCTSharedApplication() delegate] window] rootViewController];
  33. NSDate *date = [NSDate dateWithTimeIntervalSinceNow:TIMEOUT_SECONDS];
  34. BOOL foundElement = NO;
  35. __block NSString *redboxError = nil;
  36. RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
  37. if (level >= RCTLogLevelError) {
  38. redboxError = message;
  39. }
  40. });
  41. while ([date timeIntervalSinceNow] > 0 && !foundElement && !redboxError) {
  42. [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  43. [[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  44. foundElement = [self findSubviewInView:vc.view matching:^BOOL(UIView *view) {
  45. if ([view.accessibilityLabel isEqualToString:TEXT_TO_LOOK_FOR]) {
  46. return YES;
  47. }
  48. return NO;
  49. }];
  50. }
  51. RCTSetLogFunction(RCTDefaultLogFunction);
  52. XCTAssertNil(redboxError, @"RedBox error: %@", redboxError);
  53. XCTAssertTrue(foundElement, @"Couldn't find element with text '%@' in %d seconds", TEXT_TO_LOOK_FOR, TIMEOUT_SECONDS);
  54. }
  55. @end