Skip to content

Commit

Permalink
Refactor subclassing of RCTEventEmitter
Browse files Browse the repository at this point in the history
Summary:
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

Differential Revision: D40762253

fbshipit-source-id: 7284c190611c31edc1ff5f937d824f0350334a53
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Oct 27, 2022
1 parent 475310d commit 674b2a2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
13 changes: 2 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,8 @@ - (instancetype)initWithDisabledObservation

- (NSArray<NSString *> *)supportedEvents
{
NSString *className = NSStringFromClass(self.class);
[NSException raise:@"supportedEvents Not Implemented" format:@"%@ must implement the supportedEvents method", className];
return nil;
}

Expand Down
63 changes: 63 additions & 0 deletions packages/rn-tester/RNTesterUnitTests/RCTEventEmitterTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// RCTEventEmitterTests.m
// RNTesterUnitTests
//
// Created by Riccardo Cipolleschi on 27/10/2022.
// Copyright © 2022 Facebook. All rights reserved.
//

#import <XCTest/XCTest.h>
#import <React/RCTEventEmitter.h>

#pragma mark - Faulty EventEmitter

@interface RCTFaultyEventEmitter: RCTEventEmitter
@end

@implementation RCTFaultyEventEmitter
@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];

// Not using XCTAssertThrows to actually assert informations on the exception
@try {
NSArray<NSString *> * events = emitter.supportedEvents;
} @catch (NSException *exception) {
XCTAssertEqualObjects(exception.name, @"supportedEvents Not Implemented");
XCTAssertEqualObjects(exception.reason, @"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 674b2a2

Please sign in to comment.