Skip to content

Commit

Permalink
Fix react unit tests on Android (#37409)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #37409

Changelog: [internal]

Reviewed By: NickGerleman

Differential Revision: D45812628

fbshipit-source-id: 9074b872425ee771eaa8a34289f5ffa596272927
  • Loading branch information
javache authored and facebook-github-bot committed May 12, 2023
1 parent 9c51de8 commit 98f6e75
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ class ConcreteState : public State {
return getData().getDynamic();
}

void updateState(folly::dynamic data) const override {
updateState(std::move(Data(getData(), data)));
void updateState(folly::dynamic &&data) const override {
updateState(Data(getData(), std::move(data)));
}

MapBuffer getMapBuffer() const override {
return getData().getMapBuffer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class State {
#ifdef ANDROID
virtual folly::dynamic getDynamic() const = 0;
virtual MapBuffer getMapBuffer() const = 0;
virtual void updateState(folly::dynamic data) const = 0;
virtual void updateState(folly::dynamic &&data) const = 0;
#endif

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,11 @@ TEST_F(ShadowNodeTest, handleCloneFunction) {
// Those two nodes are *not* same.
EXPECT_NE(nodeAB_, nodeABClone);

#ifndef ANDROID
// `secondNodeClone` is an instance of `TestShadowNode`.
EXPECT_NE(
std::dynamic_pointer_cast<const TestShadowNode>(nodeABClone), nullptr);
#endif

// Both nodes have same content.
EXPECT_EQ(nodeAB_->getTag(), nodeABClone->getTag());
Expand All @@ -244,13 +246,9 @@ TEST_F(ShadowNodeTest, handleState) {
auto traits = TestShadowNode::BaseTraits();

auto props = std::make_shared<const TestProps>();
auto fragment = ShadowNodeFragment{
/* .props = */ props,
/* .children = */ ShadowNode::emptySharedShadowNodeSharedList(),
/* .state = */ {}};

auto const initialState =
componentDescriptor_.createInitialState(fragment, family);
componentDescriptor_.createInitialState(props, family);

auto firstNode = std::make_shared<TestShadowNode>(
ShadowNodeFragment{
Expand All @@ -277,18 +275,18 @@ TEST_F(ShadowNodeTest, handleState) {
TestShadowNode::ConcreteState::Shared _state =
std::static_pointer_cast<TestShadowNode::ConcreteState const>(
initialState);
_state->updateState(TestState{42});
_state->updateState(TestState());

thirdNode->setStateData({9001});
thirdNode->setStateData(TestState());
// State object are compared by pointer, not by value.
EXPECT_EQ(firstNode->getState(), secondNode->getState());
EXPECT_NE(firstNode->getState(), thirdNode->getState());
secondNode->setStateData(TestState{42});
secondNode->setStateData(TestState());
EXPECT_NE(firstNode->getState(), secondNode->getState());

// State cannot be changed for sealed shadow node.
secondNode->sealRecursive();
EXPECT_DEATH_IF_SUPPORTED(
{ secondNode->setStateData(TestState{42}); },
{ secondNode->setStateData(TestState()); },
"Attempt to mutate a sealed object.");
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,33 @@
#include <react/renderer/core/RawProps.h>
#include <react/renderer/core/ShadowNode.h>

#ifdef ANDROID
#include <folly/dynamic.h>
#include <react/renderer/mapbuffer/MapBuffer.h>
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
#endif

/**
* This defines a set of TestComponent classes: Props, ShadowNode,
* ComponentDescriptor. To be used for testing purpose.
*/

namespace facebook::react {

class TestState {
public:
int number;
struct TestState {
TestState() = default;

#ifdef ANDROID
TestState(TestState const &previousState, folly::dynamic &&data){};

folly::dynamic getDynamic() const {
return {};
}

MapBuffer getMapBuffer() const {
return MapBufferBuilder::EMPTY();
}
#endif
};

static const char TestComponentName[] = "Test";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ TEST(UITemplateProcessorTest, testSimpleBytecode) {
#ifndef NDEBUG
LOG(INFO) << std::endl << root1->getDebugDescription();
#endif
auto props1 = std::dynamic_pointer_cast<const ViewProps>(root1->getProps());
auto props1 = std::static_pointer_cast<const ViewProps>(root1->getProps());
EXPECT_NEAR(props1->opacity, 0.5, 0.001);
ASSERT_STREQ(props1->testId.c_str(), "root");
auto children1 = root1->getChildren();
EXPECT_EQ(children1.size(), 1);
auto child_props1 =
std::dynamic_pointer_cast<const ViewProps>(children1.at(0)->getProps());
std::static_pointer_cast<const ViewProps>(children1.at(0)->getProps());
ASSERT_STREQ(child_props1->testId.c_str(), "child");
}

Expand Down Expand Up @@ -143,12 +143,12 @@ TEST(UITemplateProcessorTest, testConditionalBytecode) {
#ifndef NDEBUG
LOG(INFO) << std::endl << root1->getDebugDescription();
#endif
auto props1 = std::dynamic_pointer_cast<const ViewProps>(root1->getProps());
auto props1 = std::static_pointer_cast<const ViewProps>(root1->getProps());
ASSERT_STREQ(props1->testId.c_str(), "root");
auto children1 = root1->getChildren();
EXPECT_EQ(children1.size(), 1);
auto child_props1 =
std::dynamic_pointer_cast<const ViewProps>(children1.at(0)->getProps());
std::static_pointer_cast<const ViewProps>(children1.at(0)->getProps());
ASSERT_STREQ(child_props1->testId.c_str(), "cond_true");

mockSimpleTestValue_ = false;
Expand All @@ -160,7 +160,7 @@ TEST(UITemplateProcessorTest, testConditionalBytecode) {
*componentDescriptorRegistry,
nativeModuleRegistry,
mockReactNativeConfig_);
auto child_props2 = std::dynamic_pointer_cast<const ViewProps>(
auto child_props2 = std::static_pointer_cast<const ViewProps>(
root2->getChildren().at(0)->getProps());
ASSERT_STREQ(child_props2->testId.c_str(), "cond_false");
}

0 comments on commit 98f6e75

Please sign in to comment.