-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor subclassing of RCTEventEmitter (#35106)
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
1 parent
ddba780
commit 25a0052
Showing
3 changed files
with
90 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/rn-tester/RNTesterUnitTests/RCTEventEmitterTests.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |