Skip to content

Commit 455f58d

Browse files
committed
test(scrollview): cover iOS anchor after content shrink
1 parent cb9ce13 commit 455f58d

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎packages/react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,65 @@
2525

2626
@interface RCTScrollViewComponentView (Tests)
2727
- (void)_keyboardWillChangeFrame:(NSNotification *)notification;
28+
- (void)_prepareForMaintainVisibleScrollPosition;
29+
- (void)_adjustForMaintainVisibleContentPosition;
2830
@end
2931

3032
@interface RCTScrollViewComponentViewTests : XCTestCase
3133
@end
3234

3335
@implementation RCTScrollViewComponentViewTests
3436

37+
- (void)testMaintainVisibleContentPositionAfterVerticalShrink
38+
{
39+
RCTScrollViewComponentView *view = [[RCTScrollViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
40+
auto props = std::make_shared<ScrollViewProps>();
41+
props->maintainVisibleContentPosition = facebook::react::ScrollViewMaintainVisibleContentPosition{};
42+
[view updateProps:props oldProps:ScrollViewShadowNode::defaultSharedProps()];
43+
44+
RCTViewComponentView *contentView = [[RCTViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 100, 1000)];
45+
[view mountChildComponentView:contentView index:0];
46+
UIView *anchor = [[UIView alloc] initWithFrame:CGRectMake(0, 800, 100, 40)];
47+
anchor.tag = 42;
48+
[contentView addSubview:anchor];
49+
50+
view.scrollView.contentSize = CGSizeMake(100, 1000);
51+
view.scrollView.contentOffset = CGPointMake(0, 800);
52+
[view _prepareForMaintainVisibleScrollPosition];
53+
54+
anchor.frame = CGRectMake(0, 300, 100, 40);
55+
view.scrollView.contentSize = CGSizeMake(100, 400);
56+
view.scrollView.contentOffset = CGPointZero;
57+
[view _adjustForMaintainVisibleContentPosition];
58+
59+
XCTAssertEqualWithAccuracy(view.scrollView.contentOffset.y, 300, 0.5);
60+
}
61+
62+
- (void)testMaintainVisibleContentPositionAfterHorizontalShrink
63+
{
64+
RCTScrollViewComponentView *view = [[RCTScrollViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
65+
auto props = std::make_shared<ScrollViewProps>();
66+
props->maintainVisibleContentPosition = facebook::react::ScrollViewMaintainVisibleContentPosition{};
67+
[view updateProps:props oldProps:ScrollViewShadowNode::defaultSharedProps()];
68+
69+
RCTViewComponentView *contentView = [[RCTViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 1000, 100)];
70+
[view mountChildComponentView:contentView index:0];
71+
UIView *anchor = [[UIView alloc] initWithFrame:CGRectMake(800, 0, 40, 100)];
72+
anchor.tag = 42;
73+
[contentView addSubview:anchor];
74+
75+
view.scrollView.contentSize = CGSizeMake(1000, 100);
76+
view.scrollView.contentOffset = CGPointMake(800, 0);
77+
[view _prepareForMaintainVisibleScrollPosition];
78+
79+
anchor.frame = CGRectMake(300, 0, 40, 100);
80+
view.scrollView.contentSize = CGSizeMake(400, 100);
81+
view.scrollView.contentOffset = CGPointZero;
82+
[view _adjustForMaintainVisibleContentPosition];
83+
84+
XCTAssertEqualWithAccuracy(view.scrollView.contentOffset.x, 300, 0.5);
85+
}
86+
3587
- (void)testAutomaticallyAdjustKeyboardInsetsAcrossRecycling
3688
{
3789
RCTScrollViewComponentView *view = [[RCTScrollViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

‎packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
79B29C2E2E607A99007612A5 /* SceneDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 79B29C2D2E607A99007612A5 /* SceneDelegate.mm */; };
1919
8145AE06241172D900A3F8DA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8145AE05241172D900A3F8DA /* LaunchScreen.storyboard */; };
2020
832F45BB2A8A6E1F0097B4E6 /* SwiftTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 832F45BA2A8A6E1F0097B4E6 /* SwiftTest.swift */; };
21+
86178A1D4F95F48D0DC933E2 /* RCTScrollViewComponentViewTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6250ED894CD7F3B5A761FE63 /* RCTScrollViewComponentViewTests.mm */; };
2122
A975CA6C2C05EADF0043F72A /* RCTNetworkTaskTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A975CA6B2C05EADE0043F72A /* RCTNetworkTaskTests.m */; };
2223
C175B6D9ED9336FB66637943 /* libPods-RNTester.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C706D402EE4AF9BE838CBA9 /* libPods-RNTester.a */; };
2324
CD10C7A5290BD4EB0033E1ED /* RCTEventEmitterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CD10C7A4290BD4EB0033E1ED /* RCTEventEmitterTests.m */; };
@@ -93,6 +94,7 @@
9394
4C706D402EE4AF9BE838CBA9 /* libPods-RNTester.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-RNTester.a"; sourceTree = BUILT_PRODUCTS_DIR; };
9495
51BC9297B6C3163C14532020 /* Pods-RNTester.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RNTester.release.xcconfig"; path = "Target Support Files/Pods-RNTester/Pods-RNTester.release.xcconfig"; sourceTree = "<group>"; };
9596
5C60EB1B226440DB0018C04F /* AppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = AppDelegate.mm; path = RNTester/AppDelegate.mm; sourceTree = "<group>"; };
97+
6250ED894CD7F3B5A761FE63 /* RCTScrollViewComponentViewTests.mm */ = {isa = PBXFileReference; includeInIndex = 1; name = RCTScrollViewComponentViewTests.mm; path = "../react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm"; sourceTree = "<group>"; };
9698
79B29C2C2E607A99007612A5 /* SceneDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SceneDelegate.h; path = RNTester/SceneDelegate.h; sourceTree = "<group>"; };
9799
79B29C2D2E607A99007612A5 /* SceneDelegate.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = SceneDelegate.mm; path = RNTester/SceneDelegate.mm; sourceTree = "<group>"; };
98100
8145AE05241172D900A3F8DA /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = RNTester/LaunchScreen.storyboard; sourceTree = "<group>"; };
@@ -267,6 +269,14 @@
267269
name = Frameworks;
268270
sourceTree = "<group>";
269271
};
272+
46F51AC598EF6515ADF3A7D9 /* LocalScrollViewTests */ = {
273+
isa = PBXGroup;
274+
children = (
275+
6250ED894CD7F3B5A761FE63 /* RCTScrollViewComponentViewTests.mm */,
276+
);
277+
name = LocalScrollViewTests;
278+
sourceTree = "<group>";
279+
};
270280
680759612239798500290469 /* Fabric */ = {
271281
isa = PBXGroup;
272282
children = (
@@ -284,6 +294,7 @@
284294
83CBBA001A601CBA00E9B192 /* Products */,
285295
2DE7E7D81FB2A4F3009E225D /* Frameworks */,
286296
E23BD6487B06BD71F1A86914 /* Pods */,
297+
46F51AC598EF6515ADF3A7D9 /* LocalScrollViewTests */,
287298
);
288299
indentWidth = 2;
289300
sourceTree = "<group>";
@@ -774,6 +785,7 @@
774785
E7DB20EB22B2BAA6005AC45F /* RCTConvert_YGValueTests.m in Sources */,
775786
E7DB20E922B2BAA6005AC45F /* RCTComponentPropsTests.m in Sources */,
776787
E7DB20D822B2BAA6005AC45F /* RCTJSONTests.m in Sources */,
788+
86178A1D4F95F48D0DC933E2 /* RCTScrollViewComponentViewTests.mm in Sources */,
777789
);
778790
runOnlyForDeploymentPostprocessing = 0;
779791
};

0 commit comments

Comments
 (0)