Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions packages/react-native/React/Tests/Text/RCTTextInputPropsTests.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright (c) Microsoft Corporation.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// [macOS]

#import <TargetConditionals.h>

#if TARGET_OS_OSX
#import <XCTest/XCTest.h>

#include <react/renderer/components/iostextinput/TextInputProps.h>
Comment on lines +8 to +15

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This path I think is more for paper tests, but also we really don't need to be adding tests I think. I don't really see native tests like these much in React Native much less its' forks. I'm not sure what any of theses tests would catch vs hwo brittle they are to upstream refactors.

#include <react/renderer/core/RawPropsParser.h>

using namespace facebook::react;

static TextInputProps textInputPropsFromDynamic(const folly::dynamic &value, const TextInputProps &sourceProps = {})
{
ContextContainer contextContainer;
PropsParserContext context{-1, contextContainer};
RawPropsParser parser;
parser.prepare<TextInputProps>();
RawProps rawProps{value};
rawProps.parse(parser);
return TextInputProps{context, sourceProps, rawProps};
}

@interface RCTTextInputPropsTests : XCTestCase
@end

@implementation RCTTextInputPropsTests

- (void)testSubmitKeyModifiersAndClearText
{
const auto props = textInputPropsFromDynamic(
folly::dynamic::object("clearTextOnSubmit", true)(
"submitKeyEvents",
folly::dynamic::array(
folly::dynamic::object("key", "Enter")("metaKey", true),
folly::dynamic::object("key", "F1")("altKey", true)("shiftKey", true)("ctrlKey", true)(
"functionKey", true))));

XCTAssertTrue(props.traits.clearTextOnSubmit);
XCTAssertEqual(props.traits.submitKeyEvents.size(), 2u);
if (props.traits.submitKeyEvents.size() != 2) {
return;
}
const auto &commandEnter = props.traits.submitKeyEvents[0];
XCTAssertTrue(commandEnter.key == "Enter");
XCTAssertTrue(commandEnter.metaKey);
XCTAssertFalse(commandEnter.altKey || commandEnter.shiftKey || commandEnter.ctrlKey || commandEnter.functionKey);
const auto &functionKey = props.traits.submitKeyEvents[1];
XCTAssertTrue(functionKey.key == "F1");
XCTAssertTrue(functionKey.altKey && functionKey.shiftKey && functionKey.ctrlKey && functionKey.functionKey);
XCTAssertFalse(functionKey.metaKey);
}

- (void)testSubmitTraitsUpdatesAndRemoval
{
const auto initial = textInputPropsFromDynamic(
folly::dynamic::object("clearTextOnSubmit", true)(
"submitKeyEvents", folly::dynamic::array(folly::dynamic::object("key", "Enter")("metaKey", true))));
const auto unchanged = textInputPropsFromDynamic(folly::dynamic::object(), initial);
XCTAssertTrue(unchanged.traits.clearTextOnSubmit);
XCTAssertEqual(unchanged.traits.submitKeyEvents.size(), 1u);

const auto updated = textInputPropsFromDynamic(
folly::dynamic::object("clearTextOnSubmit", false)(
"submitKeyEvents", folly::dynamic::array(folly::dynamic::object("key", "Enter")("shiftKey", true))),
initial);
XCTAssertFalse(updated.traits.clearTextOnSubmit);
XCTAssertEqual(updated.traits.submitKeyEvents.size(), 1u);
if (updated.traits.submitKeyEvents.size() == 1) {
XCTAssertTrue(updated.traits.submitKeyEvents[0].shiftKey);
XCTAssertFalse(updated.traits.submitKeyEvents[0].metaKey);
}

const auto removed = textInputPropsFromDynamic(
folly::dynamic::object("clearTextOnSubmit", nullptr)("submitKeyEvents", nullptr), initial);
XCTAssertFalse(removed.traits.clearTextOnSubmit);
XCTAssertTrue(removed.traits.submitKeyEvents.empty());

const auto emptyKeys =
textInputPropsFromDynamic(folly::dynamic::object("submitKeyEvents", folly::dynamic::array()), initial);
XCTAssertTrue(emptyKeys.traits.clearTextOnSubmit);
XCTAssertTrue(emptyKeys.traits.submitKeyEvents.empty());
}

- (void)testPastedTypesSupportsStringsAndArrays
{
const auto scalar = textInputPropsFromDynamic(folly::dynamic::object("pastedTypes", "string"));
XCTAssertTrue(scalar.traits.pastedTypes == std::vector<PastedTypesType>{PastedTypesType::String});

const auto array = textInputPropsFromDynamic(
folly::dynamic::object("pastedTypes", folly::dynamic::array("fileUrl", "image", "string")));
const std::vector<PastedTypesType> expected{
PastedTypesType::FileUrl, PastedTypesType::Image, PastedTypesType::String};
XCTAssertTrue(array.traits.pastedTypes == expected);
}

- (void)testInvalidPastedTypesUseDefault
{
const auto initial = textInputPropsFromDynamic(folly::dynamic::object("pastedTypes", "image"));
for (const auto &invalid :
{folly::dynamic("unsupported"),
folly::dynamic::array("string", "unsupported"),
folly::dynamic(42),
folly::dynamic::array("string", 42)}) {
const auto props = textInputPropsFromDynamic(folly::dynamic::object("pastedTypes", invalid), initial);
XCTAssertTrue(props.traits.pastedTypes.empty());
}
}

@end
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/propsConversions.h>

#if TARGET_OS_OSX // [macOS
#include <stdexcept>
#endif // macOS]
Comment on lines +14 to +16

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no other more standard way to throw errors in RN's C++ code?


namespace facebook::react {

inline void fromRawValue(const PropsParserContext &context, const RawValue &value, AutocapitalizationType &result)
Expand Down Expand Up @@ -213,6 +217,9 @@ inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
PastedTypesType &result) {
if (!value.hasType<std::string>()) {
throw std::invalid_argument("Expected a string for pastedTypes");
}
auto string = (std::string)value;
if (string == "fileUrl") {
result = PastedTypesType::FileUrl;
Expand All @@ -226,7 +233,7 @@ inline void fromRawValue(
result = PastedTypesType::String;
return;
}
abort();
throw std::invalid_argument("Unsupported pastedTypes value: " + string);
}
#endif // macOS]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

namespace facebook::react {

#if TARGET_OS_OSX // [macOS
inline void fromRawValue(
const PropsParserContext& context,
const RawValue& value,
std::vector<SubmitKeyEvent>& result);
#endif // macOS]

static TextInputTraits convertRawProp(
const PropsParserContext &context,
const RawProps &rawProps,
Expand Down Expand Up @@ -77,6 +84,18 @@ static TextInputTraits convertRawProp(
"pastedTypes",
sourceTraits.pastedTypes,
defaultTraits.pastedTypes);
traits.submitKeyEvents = convertRawProp(
context,
rawProps,
"submitKeyEvents",
sourceTraits.submitKeyEvents,
defaultTraits.submitKeyEvents);
traits.clearTextOnSubmit = convertRawProp(
context,
rawProps,
"clearTextOnSubmit",
sourceTraits.clearTextOnSubmit,
defaultTraits.clearTextOnSubmit);
#endif // macOS]

traits.dataDetectorTypes = convertRawProp(
Expand Down
Loading