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
This commit is contained in:
Samuel Susla authored and Facebook Github Bot committed 2020-01-20 05:48:32 -08:00
1 parent e5ba9f67d3
commit b9a9e8aed6
2 files changed
+65 -14

No files matched your search

@@ -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}));
}
+8 -14
View File
@@ -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>
@@ -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<const TestShadowNode>;
class TestShadowNode
: public ConcreteViewShadowNode<TestComponentName, TestProps> {
class TestShadowNode : public ConcreteViewShadowNode<
TestComponentName,
TestProps,
ViewEventEmitter,
TestState> {
public:
using ConcreteViewShadowNode::ConcreteViewShadowNode;