Skip to content

Commit

Permalink
Refactor subclassing of RCTEventEmitter (#35106)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #35106

This Diff remove the assert on the initializer in the EventEmitter in place of a more idiomatic check when the method is invoked.

It aims to solve an internal error and promotes best practices for the iOS platform.

## Changelog:
[iOS][Changed] Refactor RCTEventEmitter initialization

Reviewed By: sammy-SC

Differential Revision: D40762253

fbshipit-source-id: 83d26616ce147914948e536e9e4b1010758fb690
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Nov 8, 2022
1 parent ddba780 commit 25a0052
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 11 deletions.
25 changes: 14 additions & 11 deletions React/Modules/RCTEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ + (NSString *)moduleName
return @"";
}

+ (void)initialize
{
[super initialize];
if (self != [RCTEventEmitter class]) {
RCTAssert(
RCTClassOverridesInstanceMethod(self, @selector(supportedEvents)),
@"You must override the `supportedEvents` method of %@",
self);
}
}

- (instancetype)initWithDisabledObservation
{
self = [super init];
Expand All @@ -43,6 +32,9 @@ - (instancetype)initWithDisabledObservation

- (NSArray<NSString *> *)supportedEvents
{
NSString *message =
[NSString stringWithFormat:@"%@ must implement the supportedEvents method", NSStringFromClass(self.class)];
[self _log:message];
return nil;
}

Expand Down Expand Up @@ -147,4 +139,15 @@ - (void)invalidate
}
}

#pragma mark - Test utilities

// For testing purposes only.
// This is supposed to be overriden by a subclass in the Tests
// to verified that the error message is actually emitted.
// This is the less intrusive way found to mock the RCTLogError function in unit tests.
- (void)_log:(NSString *)message
{
RCTLogError(@"%@", message);
}

@end
4 changes: 4 additions & 0 deletions packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
5C60EB1C226440DB0018C04F /* AppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5C60EB1B226440DB0018C04F /* AppDelegate.mm */; };
5CB07C9B226467E60039471C /* RNTesterTurboModuleProvider.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5CB07C99226467E60039471C /* RNTesterTurboModuleProvider.mm */; };
8145AE06241172D900A3F8DA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8145AE05241172D900A3F8DA /* LaunchScreen.storyboard */; };
CD10C7A5290BD4EB0033E1ED /* RCTEventEmitterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CD10C7A4290BD4EB0033E1ED /* RCTEventEmitterTests.m */; };
D19DB1AC8A1EBBFA0D14DB66 /* libPods-RNTester.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 25B78D39CC03C49968A739B2 /* libPods-RNTester.a */; };
DCD006323AC907670B0D60A1 /* libPods-RNTesterUnitTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D4E0A9AD185CE086FAC9BD09 /* libPods-RNTesterUnitTests.a */; };
E7C1241A22BEC44B00DA25C0 /* RNTesterIntegrationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7C1241922BEC44B00DA25C0 /* RNTesterIntegrationTests.m */; };
Expand Down Expand Up @@ -101,6 +102,7 @@
65B6EA573FF170102920BEF4 /* libPods-RNTesterIntegrationTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-RNTesterIntegrationTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
8145AE05241172D900A3F8DA /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = RNTester/LaunchScreen.storyboard; sourceTree = "<group>"; };
8A0B7257DD8B2945456B0F61 /* Pods-RNTesterUnitTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RNTesterUnitTests.release.xcconfig"; path = "Target Support Files/Pods-RNTesterUnitTests/Pods-RNTesterUnitTests.release.xcconfig"; sourceTree = "<group>"; };
CD10C7A4290BD4EB0033E1ED /* RCTEventEmitterTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTEventEmitterTests.m; sourceTree = "<group>"; };
D4E0A9AD185CE086FAC9BD09 /* libPods-RNTesterUnitTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-RNTesterUnitTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
E771AEEA22B44E3100EA1189 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = RNTester/Info.plist; sourceTree = "<group>"; };
E7C1241922BEC44B00DA25C0 /* RNTesterIntegrationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNTesterIntegrationTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -329,6 +331,7 @@
E7DB20A022B2BA84005AC45F /* RNTesterUnitTests */ = {
isa = PBXGroup;
children = (
CD10C7A4290BD4EB0033E1ED /* RCTEventEmitterTests.m */,
E7DB20A322B2BA84005AC45F /* Info.plist */,
E7DB20C622B2BAA5005AC45F /* RCTAllocationTests.m */,
E7DB20B222B2BAA4005AC45F /* RCTAnimationUtilsTests.m */,
Expand Down Expand Up @@ -774,6 +777,7 @@
E7DB20D722B2BAA6005AC45F /* RCTModuleInitTests.m in Sources */,
E7DB20E522B2BAA6005AC45F /* RCTDevMenuTests.m in Sources */,
E7DB20DE22B2BAA6005AC45F /* RCTUnicodeDecodeTests.m in Sources */,
CD10C7A5290BD4EB0033E1ED /* RCTEventEmitterTests.m in Sources */,
E7DB20E422B2BAA6005AC45F /* RCTFormatErrorTests.m in Sources */,
E7DB20EB22B2BAA6005AC45F /* RCTConvert_YGValueTests.m in Sources */,
E7DB20E922B2BAA6005AC45F /* RCTComponentPropsTests.m in Sources */,
Expand Down
72 changes: 72 additions & 0 deletions packages/rn-tester/RNTesterUnitTests/RCTEventEmitterTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 <React/RCTEventEmitter.h>
#import <XCTest/XCTest.h>

#pragma mark - Faulty EventEmitter

@interface RCTFaultyEventEmitter : RCTEventEmitter
@end

@implementation RCTFaultyEventEmitter {
NSString *_capturedMessage;
}

- (NSString *)capturedMessage
{
return _capturedMessage;
}

- (void)_log:(NSString *)message
{
_capturedMessage = message;
}
@end

#pragma mark - Proper EventEmitter

@interface RCTProperEventEmitter : RCTEventEmitter
@end

@implementation RCTProperEventEmitter

- (NSArray<NSString *> *)supportedEvents
{
return @[ @"myEvent" ];
}
@end

#pragma mark - Tests Code

@interface RCTEventEmitterTests : XCTestCase

@end

@implementation RCTEventEmitterTests

- (void)testEventEmitterSubclass_whenFaultySubclassInvokesSupportedEvents_raiseException
{
RCTEventEmitter *emitter = [[RCTFaultyEventEmitter alloc] init];

NSArray<NSString *> *events = emitter.supportedEvents;
XCTAssertNil(events);
XCTAssertEqualObjects(
((RCTFaultyEventEmitter *)emitter).capturedMessage,
@"RCTFaultyEventEmitter must implement the supportedEvents method");
}

- (void)testEventEmitterSubclass_whenProperSubclassInvokesSupportedEvents_itreturnsTheEvents
{
RCTEventEmitter *emitter = [[RCTProperEventEmitter alloc] init];

NSArray<NSString *> *events = emitter.supportedEvents;

XCTAssertEqualObjects(events, @[ @"myEvent" ]);
}

@end

0 comments on commit 25a0052

Please sign in to comment.