1// Copyright 2015-present 650 Industries. All rights reserved.
2
3#import "ExpoKit.h"
4#import "EXKernel.h"
5#import "EXKernelLinkingManager.h"
6#import "EXRootViewController.h"
7#import "EXHomeAppManager.h"
8#import "EXShellManager.h"
9#import "EXTest.h"
10
11#import <React/RCTAssert.h>
12#import <React/RCTUtils.h>
13
14#import <XCTest/XCTest.h>
15
16@interface ExponentIntegrationTests : XCTestCase
17
18@property (nonatomic, strong) EXRootViewController *rootViewController;
19@property (nonatomic, strong) NSString *testSuiteUrl;
20
21@end
22
23@implementation ExponentIntegrationTests
24
25- (void)setUp
26{
27  [super setUp];
28  [self _loadConfig];
29
30  _rootViewController = (EXRootViewController *)[ExpoKit sharedInstance].rootViewController;
31  // if test environment isn't configured for a shell app, override here
32  // since clearly we're running tests
33  if ([EXShellManager sharedInstance].testEnvironment == EXTestEnvironmentNone) {
34    [EXShellManager sharedInstance].testEnvironment = EXTestEnvironmentLocal;
35  }
36
37  // NOTE(2018-02-20): Without giving the kernel a second to run, it never opens test-suite. With a
38  // cursory pass through the code, I didn't see the correct event to wait for. Perhaps after we
39  // implement a pure-native kernel, we'll be able to remove this shoddy delay.
40  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
41    [[EXKernel sharedInstance].serviceRegistry.linkingManager openUrl:_testSuiteUrl isUniversalLink:NO];
42  });
43
44}
45
46- (void)testDoesTestSuiteAppPassAllJSTests
47{
48  XCTAssert((_testSuiteUrl), @"No url configured for JS test-suite. Make sure EXTestEnvironment.plist exists and contains a url to test-suite.");
49
50  XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription: @"Run all JS integration tests"];
51
52  __block NSDictionary *jsTestSuiteResult = nil;
53  id<NSObject> observer = [NSNotificationCenter.defaultCenter
54                           addObserverForName:EXTestSuiteCompletedNotification
55                           object:nil
56                           queue:NSOperationQueue.currentQueue
57                           usingBlock:^(NSNotification *notification) {
58                             jsTestSuiteResult = notification.userInfo;
59                             [expectation fulfill];
60                           }];
61
62  [self waitForExpectations:@[expectation] timeout:180];
63  [NSNotificationCenter.defaultCenter removeObserver:observer];
64
65  XCTAssert((jsTestSuiteResult), @"Test suite timed out");
66  XCTAssert(([jsTestSuiteResult[@"failed"] integerValue] == 0), @"Test suite failed: %@", jsTestSuiteResult);
67}
68
69#pragma mark - internal
70
71- (void)_loadConfig
72{
73  // This plist is generated with `powertools configure-ios-test-suite-url`
74  NSString *configPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"EXTestEnvironment" ofType:@"plist"];
75  NSDictionary *testConfig = (configPath) ? [NSDictionary dictionaryWithContentsOfFile:configPath] : [NSDictionary dictionary];
76  if (testConfig) {
77    _testSuiteUrl = testConfig[@"testSuiteUrl"];
78  }
79}
80
81@end
82