diff --git a/packages/react-native/src/private/webapis/dom/nodes/__tests__/ReactNativeElement-itest.js b/packages/react-native/src/private/webapis/dom/nodes/__tests__/ReactNativeElement-itest.js index 4d1f5c82b66..1f650b95a3c 100644 --- a/packages/react-native/src/private/webapis/dom/nodes/__tests__/ReactNativeElement-itest.js +++ b/packages/react-native/src/private/webapis/dom/nodes/__tests__/ReactNativeElement-itest.js @@ -17,7 +17,7 @@ import TextInputState from '../../../../../../Libraries/Components/TextInput/Tex import * as Fantom from '@react-native/fantom'; import * as React from 'react'; import {createRef} from 'react'; -import {ScrollView, TextInput, View} from 'react-native'; +import {ScrollView, Text, TextInput, View} from 'react-native'; import { NativeText, NativeVirtualText, @@ -868,6 +868,123 @@ describe('ReactNativeElement', () => { expect(boundingClientRect.width).toBeCloseTo(50.33); expect(boundingClientRect.height).toBeCloseTo(100.33); }); + + it('returns a DOMRect for Text elements', () => { + const textRef = createRef(); + + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render( + + + Hello World + + , + ); + }); + + const textElement = ensureReactNativeElement(textRef.current); + + // Text element should have a valid bounding rect + const textBoundingRect = textElement.getBoundingClientRect(); + expect(textBoundingRect).toBeInstanceOf(DOMRect); + expect(textBoundingRect.width).toBe(100); + expect(textBoundingRect.height).toBe(50); + + // After unmounting, should return empty DOMRect + Fantom.runTask(() => { + root.render(); + }); + + const textBoundingRectAfterUnmount = + textElement.getBoundingClientRect(); + expect(textBoundingRectAfterUnmount).toBeInstanceOf(DOMRect); + expect(textBoundingRectAfterUnmount.x).toBe(0); + expect(textBoundingRectAfterUnmount.y).toBe(0); + expect(textBoundingRectAfterUnmount.width).toBe(0); + expect(textBoundingRectAfterUnmount.height).toBe(0); + }); + + it('returns a DOMRect for nested Text elements', () => { + const outerTextRef = createRef(); + const nestedTextRef = createRef(); + + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render( + + + Hello + World + + , + ); + }); + + const outerTextElement = ensureReactNativeElement(outerTextRef.current); + const nestedTextElement = ensureReactNativeElement( + nestedTextRef.current, + ); + + // Outer text element should have a valid bounding rect + const outerTextBoundingRect = outerTextElement.getBoundingClientRect(); + expect(outerTextBoundingRect).toBeInstanceOf(DOMRect); + expect(outerTextBoundingRect.width).toBe(100); + expect(outerTextBoundingRect.height).toBe(50); + + // Nested text (virtual text) returns a DOMRect with zero values + // since it doesn't have its own independent layout + const nestedTextBoundingRect = + nestedTextElement.getBoundingClientRect(); + expect(nestedTextBoundingRect).toBeInstanceOf(DOMRect); + expect(nestedTextBoundingRect.x).toBe(0); + expect(nestedTextBoundingRect.y).toBe(0); + expect(nestedTextBoundingRect.width).toBe(0); + expect(nestedTextBoundingRect.height).toBe(0); + + // After unmounting, both should return empty DOMRects + Fantom.runTask(() => { + root.render(); + }); + + const outerTextBoundingRectAfterUnmount = + outerTextElement.getBoundingClientRect(); + expect(outerTextBoundingRectAfterUnmount).toBeInstanceOf(DOMRect); + expect(outerTextBoundingRectAfterUnmount.x).toBe(0); + expect(outerTextBoundingRectAfterUnmount.y).toBe(0); + expect(outerTextBoundingRectAfterUnmount.width).toBe(0); + expect(outerTextBoundingRectAfterUnmount.height).toBe(0); + + const nestedTextBoundingRectAfterUnmount = + nestedTextElement.getBoundingClientRect(); + expect(nestedTextBoundingRectAfterUnmount).toBeInstanceOf(DOMRect); + expect(nestedTextBoundingRectAfterUnmount.x).toBe(0); + expect(nestedTextBoundingRectAfterUnmount.y).toBe(0); + expect(nestedTextBoundingRectAfterUnmount.width).toBe(0); + expect(nestedTextBoundingRectAfterUnmount.height).toBe(0); + }); }); describe('scrollLeft / scrollTop', () => {