2016-05-31 11:56:49 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-05-31 11:56:49 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-05-31 11:56:49 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
2016-11-24 17:44:51 +00:00
|
|
|
#import <React/RCTBundleURLProvider.h>
|
|
|
|
#import <React/RCTUtils.h>
|
2016-05-31 11:56:49 +00:00
|
|
|
|
|
|
|
static NSString *const testFile = @"test.jsbundle";
|
|
|
|
static NSString *const mainBundle = @"main.jsbundle";
|
|
|
|
|
|
|
|
static NSURL *mainBundleURL()
|
|
|
|
{
|
|
|
|
return [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:mainBundle];
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSURL *localhostBundleURL()
|
|
|
|
{
|
|
|
|
return [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8081/%@.bundle?platform=ios&dev=true&minify=false", testFile]];
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSURL *ipBundleURL()
|
|
|
|
{
|
|
|
|
return [NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.1:8081/%@.bundle?platform=ios&dev=true&minify=false", testFile]];
|
|
|
|
}
|
|
|
|
|
|
|
|
@implementation NSBundle (RCTBundleURLProviderTests)
|
|
|
|
|
|
|
|
- (NSURL *)RCT_URLForResource:(NSString *)name withExtension:(NSString *)ext
|
|
|
|
{
|
|
|
|
// Ensure that test files is always reported as existing
|
|
|
|
if ([[name stringByAppendingFormat:@".%@", ext] isEqualToString:mainBundle]) {
|
|
|
|
return [[self bundleURL] URLByAppendingPathComponent:mainBundle];
|
|
|
|
}
|
|
|
|
return [self RCT_URLForResource:name withExtension:ext];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface RCTBundleURLProviderTests : XCTestCase
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTBundleURLProviderTests
|
|
|
|
|
|
|
|
- (void)setUp
|
|
|
|
{
|
|
|
|
[super setUp];
|
|
|
|
|
|
|
|
RCTSwapInstanceMethods([NSBundle class],
|
|
|
|
@selector(URLForResource:withExtension:),
|
|
|
|
@selector(RCT_URLForResource:withExtension:));
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tearDown
|
|
|
|
{
|
|
|
|
RCTSwapInstanceMethods([NSBundle class],
|
|
|
|
@selector(URLForResource:withExtension:),
|
|
|
|
@selector(RCT_URLForResource:withExtension:));
|
|
|
|
|
|
|
|
[super tearDown];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testBundleURL
|
|
|
|
{
|
|
|
|
RCTBundleURLProvider *settings = [RCTBundleURLProvider sharedSettings];
|
|
|
|
settings.jsLocation = nil;
|
|
|
|
NSURL *URL = [settings jsBundleURLForBundleRoot:testFile fallbackResource:nil];
|
|
|
|
if (!getenv("CI_USE_PACKAGER")) {
|
|
|
|
XCTAssertEqualObjects(URL, mainBundleURL());
|
|
|
|
} else {
|
|
|
|
XCTAssertEqualObjects(URL, localhostBundleURL());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testLocalhostURL
|
|
|
|
{
|
|
|
|
RCTBundleURLProvider *settings = [RCTBundleURLProvider sharedSettings];
|
|
|
|
settings.jsLocation = @"localhost";
|
|
|
|
NSURL *URL = [settings jsBundleURLForBundleRoot:testFile fallbackResource:nil];
|
|
|
|
XCTAssertEqualObjects(URL, localhostBundleURL());
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testIPURL
|
|
|
|
{
|
|
|
|
RCTBundleURLProvider *settings = [RCTBundleURLProvider sharedSettings];
|
|
|
|
settings.jsLocation = @"192.168.1.1";
|
|
|
|
NSURL *URL = [settings jsBundleURLForBundleRoot:testFile fallbackResource:nil];
|
|
|
|
XCTAssertEqualObjects(URL, ipBundleURL());
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|