Skip to content

Commit c23e84a

Browse files
rshestfacebook-github-bot
authored andcommitted
Factor out "common props" testing and add testID test for Text (#53292)
Summary: Pull Request resolved: #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
1 parent 63335e5 commit c23e84a

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

packages/react-native/Libraries/Image/__tests__/Image-itest.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as React from 'react';
1919
import {createRef} from 'react';
2020
import {Image} from 'react-native';
2121
import accessibilityPropsSuite from 'react-native/src/private/__tests__/utilities/accessibilityPropsSuite';
22+
import {testIDPropSuite} from 'react-native/src/private/__tests__/utilities/commonPropsSuite';
2223
import ensureInstance from 'react-native/src/private/__tests__/utilities/ensureInstance';
2324
import NativeFantom from 'react-native/src/private/testing/fantom/specs/NativeFantom';
2425
import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement';
@@ -582,20 +583,6 @@ describe('<Image>', () => {
582583
});
583584
});
584585

585-
describe('testID', () => {
586-
it('can be set', () => {
587-
const root = Fantom.createRoot();
588-
589-
Fantom.runTask(() => {
590-
root.render(<Image testID="test" source={LOGO_SOURCE} />);
591-
});
592-
593-
expect(root.getRenderedOutput({props: ['testID']}).toJSX()).toEqual(
594-
<rn-image testID="test" />,
595-
);
596-
});
597-
});
598-
599586
describe('tintColor', () => {
600587
it('can be set', () => {
601588
const root = Fantom.createRoot();
@@ -610,11 +597,12 @@ describe('<Image>', () => {
610597
});
611598
});
612599

613-
component ComponentWithAccessibilityProps(...props: AccessibilityProps) {
614-
return <Image {...props} source={LOGO_SOURCE} />;
600+
component TestComponent(testID?: ?string, ...props: AccessibilityProps) {
601+
return <Image {...props} testID={testID} source={LOGO_SOURCE} />;
615602
}
616603

617-
accessibilityPropsSuite(ComponentWithAccessibilityProps, false);
604+
accessibilityPropsSuite(TestComponent, false);
605+
testIDPropSuite(TestComponent);
618606
});
619607

620608
describe('ref', () => {

packages/react-native/Libraries/Text/__tests__/Text-itest.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {Text} from 'react-native';
2121
import accessibilityPropsSuite, {
2222
rolePropSuite,
2323
} from 'react-native/src/private/__tests__/utilities/accessibilityPropsSuite';
24+
import {testIDPropSuite} from 'react-native/src/private/__tests__/utilities/commonPropsSuite';
2425
import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement';
2526
import ReadOnlyText from 'react-native/src/private/webapis/dom/nodes/ReadOnlyText';
2627

@@ -418,9 +419,15 @@ describe('<Text>', () => {
418419
});
419420
});
420421

421-
component ComponentWithAccessibilityProps(...props: AccessibilityProps) {
422-
return <Text {...props}>{TEST_TEXT}</Text>;
422+
component TestComponent(testID?: ?string, ...props: AccessibilityProps) {
423+
return (
424+
<Text testID={testID} {...props}>
425+
{TEST_TEXT}
426+
</Text>
427+
);
423428
}
424-
accessibilityPropsSuite(ComponentWithAccessibilityProps, false);
425-
rolePropSuite(ComponentWithAccessibilityProps);
429+
accessibilityPropsSuite(TestComponent, false);
430+
rolePropSuite(TestComponent);
431+
432+
testIDPropSuite(TestComponent);
426433
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import type {Root} from '@react-native/fantom';
12+
13+
import * as Fantom from '@react-native/fantom';
14+
import * as React from 'react';
15+
16+
export function testIDPropSuite(
17+
Component: component(...{testID?: ?string}),
18+
): void {
19+
let root: Root;
20+
21+
beforeEach(() => {
22+
root = Fantom.createRoot();
23+
});
24+
25+
afterEach(() => {
26+
root.destroy();
27+
});
28+
29+
describe('testID', () => {
30+
it('can be set', () => {
31+
Fantom.runTask(() => {
32+
root.render(<Component testID="test" />);
33+
});
34+
35+
expect(
36+
root.getRenderedOutput({props: ['testID']}).toJSONObject().props.testID,
37+
).toEqual('test');
38+
});
39+
});
40+
}

0 commit comments

Comments
 (0)