Skip to content

Commit

Permalink
Add unit test for ShadowNode::State
Browse files Browse the repository at this point in the history
Summary:
Add unit tests for ShadowNode::State

Changelog: [Internal]

Reviewed By: JoshuaGross, mdvacca

Differential Revision: D19345738

fbshipit-source-id: efb8b797e1a69b9d19631bf5cc4552283c05c107
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Jan 20, 2020
1 parent e5ba9f6 commit b9a9e8a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 14 deletions.
57 changes: 57 additions & 0 deletions ReactCommon/fabric/core/tests/ShadowNodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShadowNodeFamily>(
ShadowNodeFamilyFragment{
/* .tag = */ 9,
/* .surfaceId = */ surfaceId_,
/* .eventEmitter = */ nullptr,
},
componentDescriptor_);

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

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

auto firstNode = std::make_shared<TestShadowNode>(
ShadowNodeFragment{
/* .props = */ props,
/* .children = */ ShadowNode::emptySharedShadowNodeSharedList(),
/* .state = */ initialState},
family,
ShadowNodeTraits{});
auto secondNode = std::make_shared<TestShadowNode>(
ShadowNodeFragment{
/* .props = */ props,
/* .children = */ ShadowNode::emptySharedShadowNodeSharedList(),
/* .state = */ initialState},
family,
ShadowNodeTraits{});
auto thirdNode = std::make_shared<TestShadowNode>(
ShadowNodeFragment{
/* .props = */ props,
/* .children = */ ShadowNode::emptySharedShadowNodeSharedList(),
/* .state = */ initialState},
family,
ShadowNodeTraits{});

TestShadowNode::ConcreteState::Shared _state =
std::static_pointer_cast<TestShadowNode::ConcreteState const>(
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}));
}
22 changes: 8 additions & 14 deletions ReactCommon/fabric/core/tests/TestComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

#include <folly/dynamic.h>
#include <react/components/view/ConcreteViewShadowNode.h>
#include <react/components/view/ViewEventEmitter.h>
#include <react/components/view/ViewProps.h>
#include <react/core/ConcreteComponentDescriptor.h>
#include <react/core/LocalData.h>
#include <react/core/RawProps.h>
#include <react/core/ShadowNode.h>

Expand All @@ -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";
Expand All @@ -54,8 +45,11 @@ class TestShadowNode;

using SharedTestShadowNode = std::shared_ptr<const TestShadowNode>;

class TestShadowNode
: public ConcreteViewShadowNode<TestComponentName, TestProps> {
class TestShadowNode : public ConcreteViewShadowNode<
TestComponentName,
TestProps,
ViewEventEmitter,
TestState> {
public:
using ConcreteViewShadowNode::ConcreteViewShadowNode;

Expand Down

0 comments on commit b9a9e8a

Please sign in to comment.