Skip to content

Commit b5329ec

Browse files
NativeCxxModuleExample test for testing C++ TM E2E (#52477)
Summary: Pull Request resolved: #52477 Changelog: [Internal] This adds a Fantom test for https://github.com/facebook/react-native/blob/main/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js to test a C++ Turbo Module End 2 End (loading the C++ implementation in native code, accessing in JavaScript via Hermes VM and verifying the results of the API calls) Reviewed By: rshest Differential Revision: D77848654 fbshipit-source-id: 48a4ab88a330e9282ae8dab589743eaace62d124
1 parent 6892dde commit b5329ec

4 files changed

Lines changed: 379 additions & 7 deletions

File tree

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,22 @@
77

88
#include "NativeCxxModuleExample.h"
99
#include <react/debug/react_native_assert.h>
10+
#include <iomanip>
11+
#include <ostream>
12+
#include <sstream>
1013

1114
namespace facebook::react {
1215

16+
namespace {
17+
18+
std::string to_string_with_precision(double value, int precision = 2) {
19+
std::ostringstream oss;
20+
oss << std::setprecision(precision) << std::fixed << value;
21+
return oss.str();
22+
}
23+
24+
} // namespace
25+
1326
NativeCxxModuleExample::NativeCxxModuleExample(
1427
std::shared_ptr<CallInvoker> jsInvoker)
1528
: NativeCxxModuleExampleCxxSpec(std::move(jsInvoker)) {}
@@ -126,10 +139,12 @@ std::string NativeCxxModuleExample::getUnion(
126139
float x,
127140
std::string y,
128141
jsi::Object z) {
129-
std::string result = "x: " + std::to_string(x) + ", y: " + y + ", z: { ";
142+
std::string result =
143+
"x: " + to_string_with_precision(x) + ", y: " + y + ", z: { ";
130144
if (z.hasProperty(rt, "value")) {
131145
result += "value: ";
132-
result += std::to_string(z.getProperty(rt, "value").getNumber());
146+
result +=
147+
to_string_with_precision(z.getProperty(rt, "value").getNumber(), 0);
133148
} else if (z.hasProperty(rt, "low")) {
134149
result += "low: ";
135150
result += z.getProperty(rt, "low").getString(rt).utf8(rt);
@@ -201,11 +216,13 @@ void NativeCxxModuleExample::emitCustomDeviceEvent(
201216
eventName,
202217
[jsInvoker = jsInvoker_](
203218
jsi::Runtime& rt, std::vector<jsi::Value>& args) {
204-
args.emplace_back(jsi::Value(true));
205-
args.emplace_back(jsi::Value(42));
206-
args.emplace_back(jsi::String::createFromAscii(rt, "stringArg"));
207-
args.emplace_back(bridging::toJs(
208-
rt, CustomDeviceEvent{"one", 2, std::nullopt}, jsInvoker));
219+
args.emplace_back(jsi::Array::createWithElements(
220+
rt,
221+
jsi::Value(true),
222+
jsi::Value(42),
223+
jsi::String::createFromAscii(rt, "stringArg"),
224+
bridging::toJs(
225+
rt, CustomDeviceEvent{"one", 2, std::nullopt}, jsInvoker)));
209226
});
210227
}
211228

0 commit comments

Comments
 (0)