From 24bc565bf42bd77e8bf72f679bb76b7b5cb55598 Mon Sep 17 00:00:00 2001 From: Yevhenii Date: Mon, 26 Oct 2026 22:23:42 +0200 Subject: [PATCH] Add unit tests for natural text alignment on iOS RCTNSTextAttributesFromTextAttributes resolves a paragraph that declares no textAlign to the start edge of its layout direction, left to right as well as right to left (#57634, #59249), and leaves it to UIKit when the text attributes carry no layout direction, as a text input's do. Cover the three cases. The left-to-right one fails without #59249, where the paragraph gets no paragraph style at all: a test that read only the alignment would pass there, since a nil paragraph style reads as NSTextAlignmentLeft. --- .../Tests/Text/RCTAttributedTextUtilsTest.mm | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/react-native/React/Tests/Text/RCTAttributedTextUtilsTest.mm b/packages/react-native/React/Tests/Text/RCTAttributedTextUtilsTest.mm index a02622d5d7d7..242e40e644ae 100644 --- a/packages/react-native/React/Tests/Text/RCTAttributedTextUtilsTest.mm +++ b/packages/react-native/React/Tests/Text/RCTAttributedTextUtilsTest.mm @@ -43,6 +43,15 @@ static NSTextAlignment NSTextAlignmentFromTextAlign(NSString *textAlign, LayoutD return paragraphStyle.alignment; } +static NSParagraphStyle *ParagraphStyleFromLayoutDirection(std::optional layoutDirection) +{ + TextAttributes textAttributes; + textAttributes.layoutDirection = layoutDirection; + + NSDictionary *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes); + return attributes[NSParagraphStyleAttributeName]; +} + static UIFont *FontFromSettings(std::optional settings, std::optional weight = std::nullopt) { TextAttributes textAttributes; @@ -133,6 +142,22 @@ - (void)testTextAlignmentStartAndEndResolveWithLayoutDirection XCTAssertEqual(NSTextAlignmentFromTextAlign(@"end", LayoutDirection::RightToLeft), NSTextAlignmentLeft); } +- (void)testUndeclaredTextAlignmentResolvesWithLayoutDirection +{ + NSParagraphStyle *leftToRight = ParagraphStyleFromLayoutDirection(LayoutDirection::LeftToRight); + XCTAssertNotNil(leftToRight); + XCTAssertEqual(leftToRight.alignment, NSTextAlignmentLeft); + + NSParagraphStyle *rightToLeft = ParagraphStyleFromLayoutDirection(LayoutDirection::RightToLeft); + XCTAssertNotNil(rightToLeft); + XCTAssertEqual(rightToLeft.alignment, NSTextAlignmentRight); +} + +- (void)testUndeclaredTextAlignmentWithoutLayoutDirectionStaysNatural +{ + XCTAssertNil(ParagraphStyleFromLayoutDirection(std::nullopt)); +} + - (void)testSamenessOfEmptyAttributedStrings { NSAttributedString *attributedString1 = [[NSAttributedString alloc] initWithString:@""];