introduce RCTTestUtils (#37810)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/37810

Changelog: [Internal]

in this change, i introduce a helper file for swizzling selectors that can be used for building custom shims and fakes. i tried putting this in the cxx target, but i struggled a lot with managing the dependencies and understanding the extra behavior that was happening from being in a `rn_xplat_cxx_library`. this file is also not obj-c++, so i feel like we should decouple if possible.

it does seem like this is a new practice i'm introducing, so let me know if there's something that i'm missing.

Reviewed By: cipolleschi

Differential Revision: D46507803

fbshipit-source-id: 9be63d992b9838e9a98ee0c1c6d684fc59c3fc80
This commit is contained in:
Phillip Pan
2023-06-12 13:37:39 -07:00
committed by Facebook GitHub Bot
parent ee38c4a40c
commit a300a35f28
2 changed files with 36 additions and 0 deletions
@@ -0,0 +1,19 @@
/*
* 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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
RCT_EXTERN_C_BEGIN
void RCTSwizzleInstanceSelector(
Class targetClass,
Class swizzleClass,
SEL selector);
RCT_EXTERN_C_END
@@ -0,0 +1,17 @@
/*
* 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 "RCTSwizzleHelpers.h"
#import <objc/runtime.h>
void RCTSwizzleInstanceSelector(Class targetClass, Class swizzleClass, SEL selector)
{
Method originalMethod = class_getInstanceMethod(targetClass, selector);
Method swizzleMethod = class_getInstanceMethod(swizzleClass, selector);
method_exchangeImplementations(originalMethod, swizzleMethod);
}