@@ -17,7 +17,7 @@ import TextInputState from '../../../../../../Libraries/Components/TextInput/Tex
1717import * as Fantom from '@react-native/fantom' ;
1818import * as React from 'react' ;
1919import { createRef } from 'react' ;
20- import { ScrollView , TextInput , View } from 'react-native' ;
20+ import { ScrollView , Text , TextInput , View } from 'react-native' ;
2121import {
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