mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-03 22:48:25 +08:00
Summary: The new interface of ComponentDescriptor makes ShadowNode creation/cloning process a bit more explicit: Now customers (UIManager) must prepare Props object explicitly before creation or cloning. Besides general clarity, we need this to prepare for a new virtual `ShadowNode::clone()` method which will serve "virtual constructor" role, redirecting execution to concrete ComponentDescriptor instance. Actually, the whole purpose of concrete ComponentDescriptor instance is serve "virtual constructor" role (and all this code should be "templated"). Reviewed By: mdvacca Differential Revision: D7591714 fbshipit-source-id: 8793b3ef70ed7ae113efb36ad1eee20573360dc8
70 lines
2.5 KiB
C++
70 lines
2.5 KiB
C++
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "TestComponent.h"
|
|
|
|
using namespace facebook::react;
|
|
|
|
TEST(ComponentDescriptorTest, createShadowNode) {
|
|
SharedComponentDescriptor descriptor = std::make_shared<TestComponentDescriptor>();
|
|
|
|
ASSERT_EQ(descriptor->getComponentHandle(), typeid(TestShadowNode).hash_code());
|
|
ASSERT_STREQ(descriptor->getComponentName().c_str(), "Test");
|
|
|
|
RawProps raw;
|
|
raw["nativeID"] = "abc";
|
|
SharedProps props = descriptor->cloneProps(nullptr, raw);
|
|
SharedShadowNode node = descriptor->createShadowNode(9, 1, (void *)NULL, props);
|
|
|
|
ASSERT_EQ(node->getComponentHandle(), typeid(TestShadowNode).hash_code());
|
|
ASSERT_STREQ(node->getComponentName().c_str(), "Test");
|
|
ASSERT_EQ(node->getTag(), 9);
|
|
ASSERT_EQ(node->getRootTag(), 1);
|
|
|
|
// TODO(#27369757): getProps() doesn't work
|
|
// ASSERT_STREQ(node->getProps()->getNativeId().c_str(), "testNativeID");
|
|
}
|
|
|
|
TEST(ComponentDescriptorTest, cloneShadowNode) {
|
|
SharedComponentDescriptor descriptor = std::make_shared<TestComponentDescriptor>();
|
|
|
|
RawProps raw;
|
|
raw["nativeID"] = "abc";
|
|
SharedProps props = descriptor->cloneProps(nullptr, raw);
|
|
SharedShadowNode node = descriptor->createShadowNode(9, 1, (void *)NULL, props);
|
|
SharedShadowNode cloned = descriptor->cloneShadowNode(node);
|
|
|
|
ASSERT_EQ(cloned->getComponentHandle(), typeid(TestShadowNode).hash_code());
|
|
ASSERT_STREQ(cloned->getComponentName().c_str(), "Test");
|
|
ASSERT_EQ(cloned->getTag(), 9);
|
|
ASSERT_EQ(cloned->getRootTag(), 1);
|
|
|
|
// TODO(#27369757): getProps() doesn't work
|
|
// ASSERT_STREQ(cloned->getProps()->getNativeId().c_str(), "testNativeID");
|
|
}
|
|
|
|
TEST(ComponentDescriptorTest, appendChild) {
|
|
SharedComponentDescriptor descriptor = std::make_shared<TestComponentDescriptor>();
|
|
|
|
RawProps raw;
|
|
raw["nativeID"] = "abc";
|
|
SharedProps props = descriptor->cloneProps(nullptr, raw);
|
|
SharedShadowNode node1 = descriptor->createShadowNode(1, 1, (void *)NULL, props);
|
|
SharedShadowNode node2 = descriptor->createShadowNode(2, 1, (void *)NULL, props);
|
|
SharedShadowNode node3 = descriptor->createShadowNode(3, 1, (void *)NULL, props);
|
|
|
|
descriptor->appendChild(node1, node2);
|
|
descriptor->appendChild(node1, node3);
|
|
|
|
SharedShadowNodeSharedList node1Children = node1->getChildren();
|
|
ASSERT_EQ(node1Children->size(), 2);
|
|
ASSERT_EQ(node1Children->at(0), node2);
|
|
ASSERT_EQ(node1Children->at(1), node3);
|
|
}
|