1
2#import <XCTest/XCTest.h>
3#import "EXAppLoader+Tests.h"
4#import "EXAppLoaderRequestExpectation.h"
5
6@interface EXAppLoaderRequestTests : XCTestCase
7
8@end
9
10@implementation EXAppLoaderRequestTests
11
12#pragma mark - requests that should succeed
13
14- (void)testDoesNCLLoad
15{
16  [self _testDoesUrlLoadSuccessfully:[NSURL URLWithString:@"exp://exp.host/@community/native-component-list"]
17                         description:@"AppLoader should load something for Native Component List"];
18}
19
20#pragma mark - requests that should fail
21
22- (void)testDoesUnsupportedSdkFailToLoad
23{
24  [self _testDoesUrlFailToLoad:[NSURL URLWithString:@"exp://exp.host/@ben/test-sdk20"]
25                   description:@"AppLoader should not load a valid app running an unsupported SDK version"];
26}
27
28- (void)testDoesUnsupportedReleaseChannelFailToLoad
29{
30  [self _testDoesUrlFailToLoad:[NSURL URLWithString:@"exp://exp.host/@ben/test-sdk28?release-channel=fake"]
31                   description:@"AppLoader should not load anything from a valid app with a nonexistent release channel"];
32}
33
34- (void)testDoesInvalidUrlFailToLoad
35{
36  [self _testDoesUrlFailToLoad:[NSURL URLWithString:@"exp://abcdef"]
37                   description:@"AppLoader should not load anything from a nonexistent app url"];
38}
39
40#pragma mark - internal
41
42- (void)_testDoesUrlFailToLoad:(NSURL *)url description:(NSString *)description
43{
44  XCTestExpectation *expectToSucceed = [[XCTestExpectation alloc] initWithDescription:description];
45  [expectToSucceed setInverted:YES]; // this test should fail if the request succeeds in loading an app.
46  XCTestExpectation *expectToFail = [[XCTestExpectation alloc] initWithDescription:description];
47  EXAppLoaderRequestExpectation *test = [[EXAppLoaderRequestExpectation alloc] initWithUrl:url expectToSucceed:expectToSucceed expectToFail:expectToFail];
48  [test request];
49
50  // wait for request to fail
51  // this will take the full 10 seconds because it also enforces that `expectToSucceed` times out
52  [self waitForExpectations:@[ expectToSucceed, expectToFail ] timeout:10.0f];
53
54  // perform additional validation on AppLoader
55  XCTAssert(test.appLoader.appFetcher != nil, @"App loader should preserve app fetcher state after failure");
56}
57
58- (void)_testDoesUrlLoadSuccessfully:(NSURL *)url description:(NSString *)description
59{
60  XCTestExpectation *expectToSucceed = [[XCTestExpectation alloc] initWithDescription:description];
61  XCTestExpectation *expectToFail = [[XCTestExpectation alloc] initWithDescription:description];
62  [expectToFail setInverted:YES]; // this test should fail if the request calls its failure handler.
63  EXAppLoaderRequestExpectation *test = [[EXAppLoaderRequestExpectation alloc] initWithUrl:url expectToSucceed:expectToSucceed expectToFail:expectToFail];
64  [test request];
65
66  // wait for request to load
67  [self waitForExpectations:@[ expectToSucceed ] timeout:30.0f];
68}
69
70@end
71