From a300a35f285260b5e8fd8927c2a8da7545c1e703 Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Mon, 12 Jun 2023 13:37:39 -0700 Subject: [PATCH] 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 --- .../react/test_utils/ios/RCTSwizzleHelpers.h | 19 +++++++++++++++++++ .../react/test_utils/ios/RCTSwizzleHelpers.m | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 packages/react-native/ReactCommon/react/test_utils/ios/RCTSwizzleHelpers.h create mode 100644 packages/react-native/ReactCommon/react/test_utils/ios/RCTSwizzleHelpers.m diff --git a/packages/react-native/ReactCommon/react/test_utils/ios/RCTSwizzleHelpers.h b/packages/react-native/ReactCommon/react/test_utils/ios/RCTSwizzleHelpers.h new file mode 100644 index 00000000000..79ca15d2a65 --- /dev/null +++ b/packages/react-native/ReactCommon/react/test_utils/ios/RCTSwizzleHelpers.h @@ -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 + +#import + +RCT_EXTERN_C_BEGIN + +void RCTSwizzleInstanceSelector( + Class targetClass, + Class swizzleClass, + SEL selector); + +RCT_EXTERN_C_END diff --git a/packages/react-native/ReactCommon/react/test_utils/ios/RCTSwizzleHelpers.m b/packages/react-native/ReactCommon/react/test_utils/ios/RCTSwizzleHelpers.m new file mode 100644 index 00000000000..989a3474b73 --- /dev/null +++ b/packages/react-native/ReactCommon/react/test_utils/ios/RCTSwizzleHelpers.m @@ -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 + +void RCTSwizzleInstanceSelector(Class targetClass, Class swizzleClass, SEL selector) +{ + Method originalMethod = class_getInstanceMethod(targetClass, selector); + Method swizzleMethod = class_getInstanceMethod(swizzleClass, selector); + method_exchangeImplementations(originalMethod, swizzleMethod); +}