Skip to content

Commit 89b8628

Browse files
andrewdacenkometa-codesync[bot]
authored andcommitted
Add getBoundingClientRect tests for Text elements (#55294)
Summary: Pull Request resolved: #55294 Changelog: [Internal] Add test coverage for `getBoundingClientRect` API when used with Text components. This includes two new test cases: 1. A test for standalone Text elements verifying they return valid DOMRect with correct dimensions 2. A test for nested Text elements (virtual text) verifying that while the outer Text returns valid dimensions, nested Text elements return zero values since they don't have their own independent layout Reviewed By: vzaidman Differential Revision: D91474498 fbshipit-source-id: bbff0316d26f9a2789fadfcf6807b507d59f214e
1 parent 3368e9e commit 89b8628

1 file changed

Lines changed: 118 additions & 1 deletion

File tree

packages/react-native/src/private/webapis/dom/nodes/__tests__/ReactNativeElement-itest.js

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import TextInputState from '../../../../../../Libraries/Components/TextInput/Tex
1717
import * as Fantom from '@react-native/fantom';
1818
import * as React from 'react';
1919
import {createRef} from 'react';
20-
import {ScrollView, TextInput, View} from 'react-native';
20+
import {ScrollView, Text, TextInput, View} from 'react-native';
2121
import {
2222
NativeText,
2323
NativeVirtualText,
@@ -868,6 +868,123 @@ describe('ReactNativeElement', () => {
868868
expect(boundingClientRect.width).toBeCloseTo(50.33);
869869
expect(boundingClientRect.height).toBeCloseTo(100.33);
870870
});
871+
872+
it('returns a DOMRect for Text elements', () => {
873+
const textRef = createRef<HostInstance>();
874+
875+
const root = Fantom.createRoot();
876+
877+
Fantom.runTask(() => {
878+
root.render(
879+
<View
880+
style={{
881+
position: 'absolute',
882+
left: 10,
883+
top: 20,
884+
}}>
885+
<Text
886+
style={{
887+
width: 100,
888+
height: 50,
889+
}}
890+
ref={textRef}>
891+
Hello World
892+
</Text>
893+
</View>,
894+
);
895+
});
896+
897+
const textElement = ensureReactNativeElement(textRef.current);
898+
899+
// Text element should have a valid bounding rect
900+
const textBoundingRect = textElement.getBoundingClientRect();
901+
expect(textBoundingRect).toBeInstanceOf(DOMRect);
902+
expect(textBoundingRect.width).toBe(100);
903+
expect(textBoundingRect.height).toBe(50);
904+
905+
// After unmounting, should return empty DOMRect
906+
Fantom.runTask(() => {
907+
root.render(<View key="otherParent" />);
908+
});
909+
910+
const textBoundingRectAfterUnmount =
911+
textElement.getBoundingClientRect();
912+
expect(textBoundingRectAfterUnmount).toBeInstanceOf(DOMRect);
913+
expect(textBoundingRectAfterUnmount.x).toBe(0);
914+
expect(textBoundingRectAfterUnmount.y).toBe(0);
915+
expect(textBoundingRectAfterUnmount.width).toBe(0);
916+
expect(textBoundingRectAfterUnmount.height).toBe(0);
917+
});
918+
919+
it('returns a DOMRect for nested Text elements', () => {
920+
const outerTextRef = createRef<HostInstance>();
921+
const nestedTextRef = createRef<HostInstance>();
922+
923+
const root = Fantom.createRoot();
924+
925+
Fantom.runTask(() => {
926+
root.render(
927+
<View
928+
style={{
929+
position: 'absolute',
930+
left: 10,
931+
top: 20,
932+
}}>
933+
<Text
934+
style={{
935+
width: 100,
936+
height: 50,
937+
}}
938+
ref={outerTextRef}>
939+
Hello
940+
<Text ref={nestedTextRef}> World</Text>
941+
</Text>
942+
</View>,
943+
);
944+
});
945+
946+
const outerTextElement = ensureReactNativeElement(outerTextRef.current);
947+
const nestedTextElement = ensureReactNativeElement(
948+
nestedTextRef.current,
949+
);
950+
951+
// Outer text element should have a valid bounding rect
952+
const outerTextBoundingRect = outerTextElement.getBoundingClientRect();
953+
expect(outerTextBoundingRect).toBeInstanceOf(DOMRect);
954+
expect(outerTextBoundingRect.width).toBe(100);
955+
expect(outerTextBoundingRect.height).toBe(50);
956+
957+
// Nested text (virtual text) returns a DOMRect with zero values
958+
// since it doesn't have its own independent layout
959+
const nestedTextBoundingRect =
960+
nestedTextElement.getBoundingClientRect();
961+
expect(nestedTextBoundingRect).toBeInstanceOf(DOMRect);
962+
expect(nestedTextBoundingRect.x).toBe(0);
963+
expect(nestedTextBoundingRect.y).toBe(0);
964+
expect(nestedTextBoundingRect.width).toBe(0);
965+
expect(nestedTextBoundingRect.height).toBe(0);
966+
967+
// After unmounting, both should return empty DOMRects
968+
Fantom.runTask(() => {
969+
root.render(<View key="otherParent" />);
970+
});
971+
972+
const outerTextBoundingRectAfterUnmount =
973+
outerTextElement.getBoundingClientRect();
974+
expect(outerTextBoundingRectAfterUnmount).toBeInstanceOf(DOMRect);
975+
expect(outerTextBoundingRectAfterUnmount.x).toBe(0);
976+
expect(outerTextBoundingRectAfterUnmount.y).toBe(0);
977+
expect(outerTextBoundingRectAfterUnmount.width).toBe(0);
978+
expect(outerTextBoundingRectAfterUnmount.height).toBe(0);
979+
980+
const nestedTextBoundingRectAfterUnmount =
981+
nestedTextElement.getBoundingClientRect();
982+
expect(nestedTextBoundingRectAfterUnmount).toBeInstanceOf(DOMRect);
983+
expect(nestedTextBoundingRectAfterUnmount.x).toBe(0);
984+
expect(nestedTextBoundingRectAfterUnmount.y).toBe(0);
985+
expect(nestedTextBoundingRectAfterUnmount.width).toBe(0);
986+
expect(nestedTextBoundingRectAfterUnmount.height).toBe(0);
987+
});
871988
});
872989

873990
describe('scrollLeft / scrollTop', () => {

0 commit comments

Comments
 (0)