Ingen beskrivning

ViewShotExampleTests.m 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <UIKit/UIKit.h>
  8. #import <XCTest/XCTest.h>
  9. #import <React/RCTLog.h>
  10. #import <React/RCTRootView.h>
  11. #define TIMEOUT_SECONDS 600
  12. #define TEXT_TO_LOOK_FOR @"Welcome to React Native!"
  13. @interface ViewShotExampleTests : XCTestCase
  14. @end
  15. @implementation ViewShotExampleTests
  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. - (void)testRendersWelcomeScreen
  29. {
  30. UIViewController *vc = [[[RCTSharedApplication() delegate] window] rootViewController];
  31. NSDate *date = [NSDate dateWithTimeIntervalSinceNow:TIMEOUT_SECONDS];
  32. BOOL foundElement = NO;
  33. __block NSString *redboxError = nil;
  34. RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
  35. if (level >= RCTLogLevelError) {
  36. redboxError = message;
  37. }
  38. });
  39. while ([date timeIntervalSinceNow] > 0 && !foundElement && !redboxError) {
  40. [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  41. [[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  42. foundElement = [self findSubviewInView:vc.view matching:^BOOL(UIView *view) {
  43. if ([view.accessibilityLabel isEqualToString:TEXT_TO_LOOK_FOR]) {
  44. return YES;
  45. }
  46. return NO;
  47. }];
  48. }
  49. RCTSetLogFunction(RCTDefaultLogFunction);
  50. XCTAssertNil(redboxError, @"RedBox error: %@", redboxError);
  51. XCTAssertTrue(foundElement, @"Couldn't find element with text '%@' in %d seconds", TEXT_TO_LOOK_FOR, TIMEOUT_SECONDS);
  52. }
  53. @end