Files
react-native/packages/rn-tester/RNTesterUnitTests/RCTBundleURLProviderTests.m
Saad Najmi 046ae12a6d Move kRCTPlatformName to RCTConstants.h (#39141)
Summary:
This PR is another one made with the intent to reduce the number of diffs between React Native and React Native macOS.

In https://github.com/facebook/react-native/commit/f7219ec02d71d2f0f6c71af4d5c3d4850a898fd8#diff-d091f6636e07dc62dd7d892489355707c43923ac15056fd2eb59d9a297d576a6 , we introduced `kRCTPlatformName`, which had already been in React Native macOS for a while (since we ifdef to either "ios" or "macos" in a number of places). This change moves the string up to a common header (dropping the "k" prefix) so we can refactor other strings that currently have a hardcoded "platform=ios" inside them.

## Changelog:

[IOS] [CHANGED] - Add RCTPlatformName to RCTConstants.h

Pull Request resolved: https://github.com/facebook/react-native/pull/39141

Test Plan: CI should pass

Reviewed By: NickGerleman

Differential Revision: D48656197

Pulled By: lunaleaps

fbshipit-source-id: b9ff08e2591d7553a1a452795f36d4405ddaa5b1
2023-08-28 17:04:29 -07:00

113 lines
3.3 KiB
Objective-C

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <XCTest/XCTest.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTConstants.h>
#import <React/RCTUtils.h>
#import "OCMock/OCMock.h"
static NSString *const testFile = @"test.jsbundle";
static NSString *const mainBundle = @"main.jsbundle";
static NSURL *mainBundleURL(void)
{
return [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:mainBundle];
}
static NSURL *localhostBundleURL(void)
{
return [NSURL
URLWithString:
[NSString
stringWithFormat:
@"http://localhost:8081/%@.bundle?platform=%@&dev=true&lazy=true&minify=false&inlineSourceMap=false&modulesOnly=false&runModule=true&app=com.apple.dt.xctest.tool",
testFile,
RCTPlatformName]];
}
static NSURL *ipBundleURL(void)
{
return [NSURL
URLWithString:
[NSString
stringWithFormat:
@"http://192.168.1.1:8081/%@.bundle?platform=%@&dev=true&lazy=true&minify=false&inlineSourceMap=false&modulesOnly=false&runModule=true&app=com.apple.dt.xctest.tool",
testFile,
RCTPlatformName]];
}
@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];
if (!getenv("CI_USE_PACKAGER")) {
XCTAssertEqualObjects(URL, mainBundleURL());
} else {
XCTAssertEqualObjects(URL, localhostBundleURL());
}
}
- (void)testLocalhostURL
{
id classMock = OCMClassMock([RCTBundleURLProvider class]);
[[[classMock stub] andReturnValue:@YES] isPackagerRunning:[OCMArg any] scheme:[OCMArg any]];
RCTBundleURLProvider *settings = [RCTBundleURLProvider sharedSettings];
settings.jsLocation = @"localhost";
NSURL *URL = [settings jsBundleURLForBundleRoot:testFile];
XCTAssertEqualObjects(URL, localhostBundleURL());
}
- (void)testIPURL
{
id classMock = OCMClassMock([RCTBundleURLProvider class]);
[[[classMock stub] andReturnValue:@YES] isPackagerRunning:[OCMArg any] scheme:[OCMArg any]];
RCTBundleURLProvider *settings = [RCTBundleURLProvider sharedSettings];
settings.jsLocation = @"192.168.1.1";
NSURL *URL = [settings jsBundleURLForBundleRoot:testFile];
XCTAssertEqualObjects(URL, ipBundleURL());
}
@end