From c23e84ae9fd51bc66ca51e718dac68bb00b04d91 Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Fri, 15 Aug 2025 09:35:44 -0700 Subject: [PATCH] Factor out "common props" testing and add testID test for Text (#53292) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/53292 # Changelog: [Internal] - Adds a `testID` test for the Text component, by means of extracting and reusing already existing one for Image. Reviewed By: andrewdacenko Differential Revision: D80332473 fbshipit-source-id: 8e8bc2eae12f5f340817f3788f320aad3a6fff45 --- .../Libraries/Image/__tests__/Image-itest.js | 22 +++------- .../Libraries/Text/__tests__/Text-itest.js | 15 +++++-- .../__tests__/utilities/commonPropsSuite.js | 40 +++++++++++++++++++ 3 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 packages/react-native/src/private/__tests__/utilities/commonPropsSuite.js diff --git a/packages/react-native/Libraries/Image/__tests__/Image-itest.js b/packages/react-native/Libraries/Image/__tests__/Image-itest.js index 1cd5750d2a6..2c02271372c 100644 --- a/packages/react-native/Libraries/Image/__tests__/Image-itest.js +++ b/packages/react-native/Libraries/Image/__tests__/Image-itest.js @@ -19,6 +19,7 @@ import * as React from 'react'; import {createRef} from 'react'; import {Image} from 'react-native'; import accessibilityPropsSuite from 'react-native/src/private/__tests__/utilities/accessibilityPropsSuite'; +import {testIDPropSuite} from 'react-native/src/private/__tests__/utilities/commonPropsSuite'; import ensureInstance from 'react-native/src/private/__tests__/utilities/ensureInstance'; import NativeFantom from 'react-native/src/private/testing/fantom/specs/NativeFantom'; import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement'; @@ -582,20 +583,6 @@ describe('', () => { }); }); - describe('testID', () => { - it('can be set', () => { - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - root.render(); - }); - - expect(root.getRenderedOutput({props: ['testID']}).toJSX()).toEqual( - , - ); - }); - }); - describe('tintColor', () => { it('can be set', () => { const root = Fantom.createRoot(); @@ -610,11 +597,12 @@ describe('', () => { }); }); - component ComponentWithAccessibilityProps(...props: AccessibilityProps) { - return ; + component TestComponent(testID?: ?string, ...props: AccessibilityProps) { + return ; } - accessibilityPropsSuite(ComponentWithAccessibilityProps, false); + accessibilityPropsSuite(TestComponent, false); + testIDPropSuite(TestComponent); }); describe('ref', () => { diff --git a/packages/react-native/Libraries/Text/__tests__/Text-itest.js b/packages/react-native/Libraries/Text/__tests__/Text-itest.js index 2a05b002bb3..785fe029342 100644 --- a/packages/react-native/Libraries/Text/__tests__/Text-itest.js +++ b/packages/react-native/Libraries/Text/__tests__/Text-itest.js @@ -21,6 +21,7 @@ import {Text} from 'react-native'; import accessibilityPropsSuite, { rolePropSuite, } from 'react-native/src/private/__tests__/utilities/accessibilityPropsSuite'; +import {testIDPropSuite} from 'react-native/src/private/__tests__/utilities/commonPropsSuite'; import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement'; import ReadOnlyText from 'react-native/src/private/webapis/dom/nodes/ReadOnlyText'; @@ -418,9 +419,15 @@ describe('', () => { }); }); - component ComponentWithAccessibilityProps(...props: AccessibilityProps) { - return {TEST_TEXT}; + component TestComponent(testID?: ?string, ...props: AccessibilityProps) { + return ( + + {TEST_TEXT} + + ); } - accessibilityPropsSuite(ComponentWithAccessibilityProps, false); - rolePropSuite(ComponentWithAccessibilityProps); + accessibilityPropsSuite(TestComponent, false); + rolePropSuite(TestComponent); + + testIDPropSuite(TestComponent); }); diff --git a/packages/react-native/src/private/__tests__/utilities/commonPropsSuite.js b/packages/react-native/src/private/__tests__/utilities/commonPropsSuite.js new file mode 100644 index 00000000000..1690bf5c384 --- /dev/null +++ b/packages/react-native/src/private/__tests__/utilities/commonPropsSuite.js @@ -0,0 +1,40 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +import type {Root} from '@react-native/fantom'; + +import * as Fantom from '@react-native/fantom'; +import * as React from 'react'; + +export function testIDPropSuite( + Component: component(...{testID?: ?string}), +): void { + let root: Root; + + beforeEach(() => { + root = Fantom.createRoot(); + }); + + afterEach(() => { + root.destroy(); + }); + + describe('testID', () => { + it('can be set', () => { + Fantom.runTask(() => { + root.render(); + }); + + expect( + root.getRenderedOutput({props: ['testID']}).toJSONObject().props.testID, + ).toEqual('test'); + }); + }); +}