Repository navigation
fix(textinput): restore macOS Fabric trait conversion #3173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jason Morse (JasonVMo)
wants to merge
1
commit into
main
Choose a base branch
from
user/jasonvmo/cpp-textinput-traits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
packages/react-native/React/Tests/Text/RCTTextInputPropsTests.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
| #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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
@@ -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; | ||
|
|
@@ -226,7 +233,7 @@ inline void fromRawValue( | |
| result = PastedTypesType::String; | ||
| return; | ||
| } | ||
| abort(); | ||
| throw std::invalid_argument("Unsupported pastedTypes value: " + string); | ||
| } | ||
| #endif // macOS] | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.