mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
test RCTInstance invalidation on RCTHost restart (#37899)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/37899 Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D46547012 fbshipit-source-id: 9b157678c474120aeca3896942e368678cb5b8eb
This commit is contained in:
committed by
Facebook GitHub Bot
parent
1807ba6f31
commit
75d34407c1
@@ -8,6 +8,8 @@
|
||||
#import <XCTest/XCTest.h>
|
||||
|
||||
#import <RCTTestUtils/ShimRCTInstance.h>
|
||||
#import <React/RCTLog.h>
|
||||
#import <React/RCTMockDef.h>
|
||||
#import <ReactCommon/RCTHermesInstance.h>
|
||||
#import <ReactCommon/RCTHost.h>
|
||||
#import <ReactCommon/RCTInstance.h>
|
||||
@@ -15,6 +17,22 @@
|
||||
|
||||
#import <OCMock/OCMock.h>
|
||||
|
||||
RCT_MOCK_REF(RCTHost, _RCTLogNativeInternal);
|
||||
|
||||
RCTLogLevel gLogLevel;
|
||||
int gLogCalledTimes = 0;
|
||||
NSString *gLogMessage = nil;
|
||||
static void RCTLogNativeInternalMock(RCTLogLevel level, const char *fileName, int lineNumber, NSString *format, ...)
|
||||
{
|
||||
gLogLevel = level;
|
||||
gLogCalledTimes++;
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
gLogMessage = [[NSString alloc] initWithFormat:format arguments:args];
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
@interface RCTHostTests : XCTestCase
|
||||
@end
|
||||
|
||||
@@ -43,15 +61,34 @@ static ShimRCTInstance *shimmedRCTInstance;
|
||||
- (void)tearDown
|
||||
{
|
||||
[shimmedRCTInstance reset];
|
||||
gLogCalledTimes = 0;
|
||||
gLogMessage = nil;
|
||||
|
||||
[super tearDown];
|
||||
}
|
||||
|
||||
- (void)testStart
|
||||
{
|
||||
RCT_MOCK_SET(RCTHost, _RCTLogNativeInternal, RCTLogNativeInternalMock);
|
||||
|
||||
XCTAssertEqual(shimmedRCTInstance.initCount, 0);
|
||||
[_subject start];
|
||||
OCMVerify(OCMTimes(1), [_mockHostDelegate hostDidStart:_subject]);
|
||||
XCTAssertEqual(shimmedRCTInstance.initCount, 1);
|
||||
XCTAssertEqual(gLogCalledTimes, 0);
|
||||
|
||||
XCTAssertEqual(shimmedRCTInstance.invalidateCount, 0);
|
||||
[_subject start];
|
||||
XCTAssertEqual(shimmedRCTInstance.initCount, 2);
|
||||
XCTAssertEqual(shimmedRCTInstance.invalidateCount, 1);
|
||||
OCMVerify(OCMTimes(2), [_mockHostDelegate hostDidStart:_subject]);
|
||||
XCTAssertEqual(gLogLevel, RCTLogLevelWarning);
|
||||
XCTAssertEqual(gLogCalledTimes, 1);
|
||||
XCTAssertEqualObjects(
|
||||
gLogMessage,
|
||||
@"RCTHost should not be creating a new instance if one already exists. This implies there is a bug with how/when this method is being called.");
|
||||
|
||||
RCT_MOCK_RESET(RCTHost, _RCTLogNativeInternal);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -15,9 +15,13 @@
|
||||
#import <React/RCTFabricSurface.h>
|
||||
#import <React/RCTJSThread.h>
|
||||
#import <React/RCTLog.h>
|
||||
#import <React/RCTMockDef.h>
|
||||
#import <React/RCTPerformanceLogger.h>
|
||||
#import <React/RCTReloadCommand.h>
|
||||
|
||||
RCT_MOCK_DEF(RCTHost, _RCTLogNativeInternal);
|
||||
#define _RCTLogNativeInternal RCT_MOCK_USE(RCTHost, _RCTLogNativeInternal)
|
||||
|
||||
using namespace facebook::react;
|
||||
|
||||
@interface RCTHost () <RCTReloadListener, RCTInstanceDelegate>
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
@interface ShimRCTInstance : NSObject
|
||||
|
||||
@property (assign) int initCount;
|
||||
@property (assign) int invalidateCount;
|
||||
|
||||
- (void)reset;
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ static __weak ShimRCTInstance *weakShim = nil;
|
||||
[ShimRCTInstance class],
|
||||
@selector(initWithDelegate:
|
||||
jsEngineInstance:bundleManager:turboModuleManagerDelegate:onInitialBundleLoad:moduleRegistry:));
|
||||
RCTSwizzleInstanceSelector([RCTInstance class], [ShimRCTInstance class], @selector(invalidate));
|
||||
weakShim = self;
|
||||
}
|
||||
return self;
|
||||
@@ -36,7 +37,9 @@ static __weak ShimRCTInstance *weakShim = nil;
|
||||
[ShimRCTInstance class],
|
||||
@selector(initWithDelegate:
|
||||
jsEngineInstance:bundleManager:turboModuleManagerDelegate:onInitialBundleLoad:moduleRegistry:));
|
||||
RCTSwizzleInstanceSelector([RCTInstance class], [ShimRCTInstance class], @selector(invalidate));
|
||||
_initCount = 0;
|
||||
_invalidateCount = 0;
|
||||
}
|
||||
|
||||
- (instancetype)initWithDelegate:(id<RCTInstanceDelegate>)delegate
|
||||
@@ -50,4 +53,9 @@ static __weak ShimRCTInstance *weakShim = nil;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)invalidate
|
||||
{
|
||||
weakShim.invalidateCount++;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user