-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
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
1 parent
475310d
commit 674b2a2
Showing
2 changed files
with
65 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
63 changes: 63 additions & 0 deletions
63
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,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 | ||
|