From b9a9e8aed6c09b267d648aa88ffeff21751d8d87 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Mon, 20 Jan 2020 05:46:03 -0800 Subject: [PATCH] Add unit test for ShadowNode::State Summary: Add unit tests for ShadowNode::State Changelog: [Internal] Reviewed By: JoshuaGross, mdvacca Differential Revision: D19345738 fbshipit-source-id: efb8b797e1a69b9d19631bf5cc4552283c05c107 --- .../fabric/core/tests/ShadowNodeTest.cpp | 57 +++++++++++++++++++ ReactCommon/fabric/core/tests/TestComponent.h | 22 +++---- 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp b/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp index bab859649d3..e1d9c2ae809 100644 --- a/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp +++ b/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp @@ -233,3 +233,60 @@ TEST_F(ShadowNodeTest, handleBacktracking) { EXPECT_EQ(&ancestors2[0].first.get(), nodeA_.get()); EXPECT_EQ(&ancestors2[1].first.get(), nodeAB_.get()); } + +TEST_F(ShadowNodeTest, handleState) { + auto family = std::make_shared( + ShadowNodeFamilyFragment{ + /* .tag = */ 9, + /* .surfaceId = */ surfaceId_, + /* .eventEmitter = */ nullptr, + }, + componentDescriptor_); + + auto props = std::make_shared(); + auto fragment = ShadowNodeFragment{ + /* .props = */ props, + /* .children = */ ShadowNode::emptySharedShadowNodeSharedList(), + /* .state = */ {}}; + + auto const initialState = + componentDescriptor_.createInitialState(fragment, surfaceId_); + + auto firstNode = std::make_shared( + ShadowNodeFragment{ + /* .props = */ props, + /* .children = */ ShadowNode::emptySharedShadowNodeSharedList(), + /* .state = */ initialState}, + family, + ShadowNodeTraits{}); + auto secondNode = std::make_shared( + ShadowNodeFragment{ + /* .props = */ props, + /* .children = */ ShadowNode::emptySharedShadowNodeSharedList(), + /* .state = */ initialState}, + family, + ShadowNodeTraits{}); + auto thirdNode = std::make_shared( + ShadowNodeFragment{ + /* .props = */ props, + /* .children = */ ShadowNode::emptySharedShadowNodeSharedList(), + /* .state = */ initialState}, + family, + ShadowNodeTraits{}); + + TestShadowNode::ConcreteState::Shared _state = + std::static_pointer_cast( + initialState); + _state->updateState(TestState{42}); + + thirdNode->setStateData({9001}); + // 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}); + EXPECT_NE(firstNode->getState(), secondNode->getState()); + + // State cannot be changed for sealed shadow node. + secondNode->sealRecursive(); + EXPECT_ANY_THROW(secondNode->setStateData(TestState{42})); +} diff --git a/ReactCommon/fabric/core/tests/TestComponent.h b/ReactCommon/fabric/core/tests/TestComponent.h index 9e0616923d2..3f468459e51 100644 --- a/ReactCommon/fabric/core/tests/TestComponent.h +++ b/ReactCommon/fabric/core/tests/TestComponent.h @@ -11,9 +11,9 @@ #include #include +#include #include #include -#include #include #include @@ -24,18 +24,9 @@ using namespace facebook::react; * ComponentDescriptor. To be used for testing purpose. */ -class TestLocalData : public LocalData { +class TestState { public: - void setNumber(const int &number) { - number_ = number; - } - - int getNumber() const { - return number_; - } - - private: - int number_{0}; + int number; }; static const char TestComponentName[] = "Test"; @@ -54,8 +45,11 @@ class TestShadowNode; using SharedTestShadowNode = std::shared_ptr; -class TestShadowNode - : public ConcreteViewShadowNode { +class TestShadowNode : public ConcreteViewShadowNode< + TestComponentName, + TestProps, + ViewEventEmitter, + TestState> { public: using ConcreteViewShadowNode::ConcreteViewShadowNode;