Skip to content

Update TurboModuleTestFixture to handle Promise types #52801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
#include <ReactCommon/TestCallInvoker.h>
#include <gtest/gtest.h>
#include <hermes/hermes.h>
#include <react/bridging/Bridging.h>
#include <react/debug/react_native_assert.h>
#include <memory>
#include <optional>

namespace facebook::react {

Expand All @@ -22,6 +25,75 @@ class TurboModuleTestFixture : public ::testing::Test {
jsInvoker_(std::make_shared<TestCallInvoker>(runtime_)),
module_(std::make_shared<T>(jsInvoker_, std::forward<Args>(args)...)) {}

void SetUp() override {
auto setImmediateName =
jsi::PropNameID::forAscii(*runtime_, "setImmediate");
runtime_->global().setProperty(
*runtime_,
setImmediateName,
jsi::Function::createFromHostFunction(
*runtime_,
setImmediateName,
1,
[jsInvoker = jsInvoker_](
jsi::Runtime& rt,
[[maybe_unused]] const jsi::Value& thisVal,
const jsi::Value* args,
size_t count) {
react_native_assert(count >= 1);
jsInvoker->invokeAsync([cb = std::make_shared<jsi::Value>(
rt, args[0])](jsi::Runtime& rt) {
cb->asObject(rt).asFunction(rt).call(rt);
});
return jsi::Value::undefined();
}));
}

void TearDown() override {
module_ = nullptr;
jsInvoker_ = nullptr;
runtime_ = nullptr;
}

template <typename TPromise>
std::optional<TPromise> resolvePromise(
const AsyncPromise<TPromise>& asyncPromise) {
auto promise = asyncPromise.get(*runtime_);
std::optional<TPromise> result = std::nullopt;
promise.getPropertyAsFunction(*runtime_, "then")
.callWithThis(
*runtime_,
promise,
bridging::toJs(
*runtime_,
[&](TPromise value) { result = std::move(value); },
jsInvoker_));
jsInvoker_->flushQueue();
return result;
}

template <typename TPromise>
std::optional<std::string> handleError(
const AsyncPromise<TPromise>& asyncPromise) {
auto promise = asyncPromise.get(*runtime_);
std::optional<std::string> message;
promise.getPropertyAsFunction(*runtime_, "catch")
.callWithThis(
*runtime_,
promise,
bridging::toJs(
*runtime_,
[&](jsi::Object error) {
message = bridging::fromJs<std::string>(
*runtime_,
error.getProperty(*runtime_, "message"),
jsInvoker_);
},
jsInvoker_));
jsInvoker_->flushQueue();
return message;
}

protected:
std::shared_ptr<jsi::Runtime> runtime_{};
std::shared_ptr<TestCallInvoker> jsInvoker_{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ TEST_F(NativeCxxModuleExampleTests, GetValueReturnsCorrectValues) {
EXPECT_EQ(result.z.c, "seven");
}

TEST_F(NativeCxxModuleExampleTests, GetValueWithPromiseReturnsCorrectValues) {
auto promise1 = module_->getValueWithPromise(*runtime_, false);
auto result = resolvePromise(promise1);
EXPECT_EQ(result, "result!");

auto promise2 = module_->getValueWithPromise(*runtime_, true);
auto message = handleError(promise2);
EXPECT_EQ(message, "intentional promise rejection");
}

TEST_F(
NativeCxxModuleExampleTests,
GetWithWithOptionalArgsReturnsCorrectValues) {
Expand Down
Loading