Files
react-native/packages/rn-tester/RNTesterIntegrationTests/RCTLoggingTests.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

145 lines
5.6 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/RCTAssert.h>
#import <React/RCTBridge.h>
#import <React/RCTConstants.h>
#import <React/RCTLog.h>
// Time to wait for an expected log statement to show before failing the test
const int64_t LOGGER_TIMEOUT = 10 * NSEC_PER_SEC;
@interface RCTLoggingTests : XCTestCase
@end
@implementation RCTLoggingTests {
RCTBridge *_bridge;
dispatch_semaphore_t _logSem;
RCTLogLevel _lastLogLevel;
RCTLogSource _lastLogSource;
NSString *_lastLogMessage;
}
- (void)setUp
{
NSURL *scriptURL;
if (getenv("CI_USE_PACKAGER")) {
NSString *app = @"IntegrationTests/IntegrationTestsApp";
scriptURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8081/%@.bundle?platform=%@&dev=true",
app,
RCTPlatformName]];
} else {
scriptURL = [[NSBundle bundleForClass:[RCTBridge class]] URLForResource:@"main" withExtension:@"jsbundle"];
}
RCTAssert(scriptURL != nil, @"No scriptURL set");
_bridge = [[RCTBridge alloc] initWithBundleURL:scriptURL moduleProvider:NULL launchOptions:nil];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60];
while (date.timeIntervalSinceNow > 0 && _bridge.loading) {
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
XCTAssertFalse(_bridge.loading);
_logSem = dispatch_semaphore_create(0);
// Set the log function to signal the semaphore
RCTSetLogFunction(
^(RCTLogLevel level,
RCTLogSource source,
__unused NSString *fileName,
__unused NSNumber *lineNumber,
NSString *message) {
if (source == RCTLogSourceJavaScript) {
self->_lastLogLevel = level;
self->_lastLogSource = source;
self->_lastLogMessage = message;
dispatch_semaphore_signal(self->_logSem);
}
});
}
- (void)tearDown
{
[_bridge invalidate];
_bridge = nil;
RCTSetLogFunction(RCTDefaultLogFunction);
}
- (void)testLogging
{
intptr_t waitRet = 0;
// First queue the marker and spin until it happens to be logged.
// This is to ensure we skip all of the other messages, that were logged earlier.
NSString *const LogMarker = @"===LOG_MARKER===";
[_bridge enqueueJSCall:@"LoggingTestModule.logToConsole" args:@[ LogMarker ]];
do {
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for log marker");
} while (waitRet == 0 && ![_lastLogMessage isEqualToString:LogMarker]);
[_bridge enqueueJSCall:@"LoggingTestModule.logToConsole" args:@[ @"Invoking console.log" ]];
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for logToConsole");
XCTAssertEqual(_lastLogLevel, RCTLogLevelInfo);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertEqualObjects(_lastLogMessage, @"Invoking console.log");
[_bridge enqueueJSCall:@"LoggingTestModule.warning" args:@[ @"Generating warning" ]];
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for warning");
XCTAssertEqual(_lastLogLevel, RCTLogLevelWarning);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertEqualObjects(_lastLogMessage, @"Generating warning");
[_bridge enqueueJSCall:@"LoggingTestModule.invariant" args:@[ @"Invariant failed" ]];
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for invariant");
XCTAssertEqual(_lastLogLevel, RCTLogLevelError);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertTrue([_lastLogMessage containsString:@"Invariant Violation: Invariant failed"]);
[_bridge enqueueJSCall:@"LoggingTestModule.logErrorToConsole" args:@[ @"Invoking console.error" ]];
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for logErrorToConsole");
// For local bundles, we may first get a warning about symbolication
if (![_lastLogMessage isEqualToString:@"Invoking console.error"]) {
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for logErrorToConsole #2");
}
XCTAssertEqual(_lastLogLevel, RCTLogLevelError);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertEqualObjects(_lastLogMessage, @"Invoking console.error");
[_bridge enqueueJSCall:@"LoggingTestModule.throwError" args:@[ @"Throwing an error" ]];
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for throwError");
// For local bundles, we may first get a warning about symbolication
if (![_lastLogMessage containsString:@"Error: Throwing an error"]) {
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for throwError #2");
}
XCTAssertEqual(_lastLogLevel, RCTLogLevelError);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertTrue([_lastLogMessage containsString:@"Error: Throwing an error"]);
}
@end