mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
6f97733bb8
Summary: ComponentName is used by many core component of React Native, such as ComponentDescriptor, ShadowNode, ShadowView and so on. In all those cases this value represents the actual name of the component which came from `concreteComponentName` template parameter of ConcreteShadowNode. In all of those cases, it's raw `char const *` type. So, we don't need to use owning representation of the string (std::string) in all those places. The only exception from this is a part where we receive the name of the component from JS side. In this case, the source string comes from JS and has to be analyzed as a character sequence to find corresponding ComponentDescriptor. In my experiments, 20% of the time during diffing is spent on copying (this) `std::string`. Reviewed By: mdvacca Differential Revision: D15844407 fbshipit-source-id: a2e71505e22d09107e001bdf661d4a826bcf2dea
92 lines
3.2 KiB
C++
92 lines
3.2 KiB
C++
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* 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>(nullptr);
|
|
|
|
ASSERT_EQ(descriptor->getComponentHandle(), TestShadowNode::Handle());
|
|
ASSERT_STREQ(descriptor->getComponentName(), TestShadowNode::Name());
|
|
ASSERT_STREQ(descriptor->getComponentName(), "Test");
|
|
|
|
const auto &raw = RawProps(folly::dynamic::object("nativeID", "abc"));
|
|
SharedProps props = descriptor->cloneProps(nullptr, raw);
|
|
SharedShadowNode node = descriptor->createShadowNode(ShadowNodeFragment{
|
|
/* .tag = */ 9,
|
|
/* .surfaceId = */ 1,
|
|
/* .props = */ props,
|
|
/* .eventEmitter = */ descriptor->createEventEmitter(0, 9),
|
|
});
|
|
|
|
ASSERT_EQ(node->getComponentHandle(), TestShadowNode::Handle());
|
|
ASSERT_STREQ(node->getComponentName(), TestShadowNode::Name());
|
|
ASSERT_STREQ(node->getComponentName(), "Test");
|
|
ASSERT_EQ(node->getTag(), 9);
|
|
ASSERT_EQ(node->getSurfaceId(), 1);
|
|
ASSERT_STREQ(node->getProps()->nativeId.c_str(), "abc");
|
|
}
|
|
|
|
TEST(ComponentDescriptorTest, cloneShadowNode) {
|
|
SharedComponentDescriptor descriptor =
|
|
std::make_shared<TestComponentDescriptor>(nullptr);
|
|
|
|
const auto &raw = RawProps(folly::dynamic::object("nativeID", "abc"));
|
|
SharedProps props = descriptor->cloneProps(nullptr, raw);
|
|
SharedShadowNode node = descriptor->createShadowNode(ShadowNodeFragment{
|
|
/* .tag = */ 9,
|
|
/* .surfaceId = */ 1,
|
|
/* .props = */ props,
|
|
/* .eventEmitter = */ descriptor->createEventEmitter(0, 9),
|
|
});
|
|
SharedShadowNode cloned = descriptor->cloneShadowNode(*node, {});
|
|
|
|
ASSERT_STREQ(cloned->getComponentName(), "Test");
|
|
ASSERT_EQ(cloned->getTag(), 9);
|
|
ASSERT_EQ(cloned->getSurfaceId(), 1);
|
|
ASSERT_STREQ(cloned->getProps()->nativeId.c_str(), "abc");
|
|
}
|
|
|
|
TEST(ComponentDescriptorTest, appendChild) {
|
|
SharedComponentDescriptor descriptor =
|
|
std::make_shared<TestComponentDescriptor>(nullptr);
|
|
|
|
const auto &raw = RawProps(folly::dynamic::object("nativeID", "abc"));
|
|
SharedProps props = descriptor->cloneProps(nullptr, raw);
|
|
SharedShadowNode node1 = descriptor->createShadowNode(ShadowNodeFragment{
|
|
/* .tag = */ 1,
|
|
/* .surfaceId = */ 1,
|
|
/* .props = */ props,
|
|
/* .eventEmitter = */ descriptor->createEventEmitter(0, 1),
|
|
});
|
|
SharedShadowNode node2 = descriptor->createShadowNode(ShadowNodeFragment{
|
|
/* .tag = */ 2,
|
|
/* .surfaceId = */ 1,
|
|
/* .props = */ props,
|
|
/* .eventEmitter = */ descriptor->createEventEmitter(0, 2),
|
|
});
|
|
SharedShadowNode node3 = descriptor->createShadowNode(ShadowNodeFragment{
|
|
/* .tag = */ 3,
|
|
/* .surfaceId = */ 1,
|
|
/* .props = */ props,
|
|
/* .eventEmitter = */ descriptor->createEventEmitter(0, 3),
|
|
});
|
|
|
|
descriptor->appendChild(node1, node2);
|
|
descriptor->appendChild(node1, node3);
|
|
|
|
auto node1Children = node1->getChildren();
|
|
ASSERT_EQ(node1Children.size(), 2);
|
|
ASSERT_EQ(node1Children.at(0), node2);
|
|
ASSERT_EQ(node1Children.at(1), node3);
|
|
}
|