Skip to content

Commit 0cd6a29

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
TextSharedExamples and empty text E2E test (#51464)
Summary: Pull Request resolved: #51464 Add a module of shared examples, like `TextInputSharedExamples`, to avoid copy/paste between these. Modifies the empty test example a bit and adds an E2E test. Changelog: [Internal] Reviewed By: rshest Differential Revision: D74780847 fbshipit-source-id: 30c2830ef0b638680fe75b4bcf9f138f5c01e190
1 parent ce43342 commit 0cd6a29

6 files changed

Lines changed: 97 additions & 85 deletions

File tree

packages/rn-tester/js/components/RNTesterTheme.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {ColorValue, ImageSource} from 'react-native';
1212

1313
import * as React from 'react';
1414
import {createContext} from 'react';
15+
import {use} from 'react';
1516
import {Appearance} from 'react-native';
1617

1718
export type RNTesterTheme = {
@@ -141,3 +142,6 @@ export const themes = {light: RNTesterLightTheme, dark: RNTesterDarkTheme};
141142
export const RNTesterThemeContext: React.Context<RNTesterTheme> = createContext(
142143
Appearance.getColorScheme() === 'dark' ? themes.dark : themes.light,
143144
);
145+
export function useTheme(): RNTesterTheme {
146+
return use(RNTesterThemeContext);
147+
}

packages/rn-tester/js/examples/Text/TextExample.android.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import hotdog from '../../assets/hotdog.jpg';
1616
import RNTesterText from '../../components/RNTesterText';
1717
import TextLegend from '../../components/TextLegend';
1818
import TextAdjustsDynamicLayoutExample from './TextAdjustsDynamicLayoutExample';
19-
import TextInlineViewsExample from './TextInlineViewsExample';
19+
import TextSharedExamples from './TextSharedExamples';
2020

2121
const TextInlineView = require('../../components/TextInlineView');
2222
const React = require('react');
@@ -1461,13 +1461,6 @@ const examples = [
14611461
return <LetterSpacingExample />;
14621462
},
14631463
},
1464-
{
1465-
title: 'Empty Text',
1466-
name: 'emptyText',
1467-
render(): React.Node {
1468-
return <Text />;
1469-
},
1470-
},
14711464
{
14721465
title: 'Toggling Attributes',
14731466
name: 'togglingAttributes',
@@ -1713,7 +1706,7 @@ const examples = [
17131706
);
17141707
},
17151708
},
1716-
TextInlineViewsExample,
1709+
...TextSharedExamples,
17171710
];
17181711

17191712
const styles = StyleSheet.create({

packages/rn-tester/js/examples/Text/TextExample.ios.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {RNTesterModule} from '../../types/RNTesterTypes';
1515
import hotdog from '../../assets/hotdog.jpg';
1616
import RNTesterText from '../../components/RNTesterText';
1717
import TextLegend from '../../components/TextLegend';
18-
import TextInlineViewsExample from './TextInlineViewsExample';
18+
import TextSharedExamples from './TextSharedExamples';
1919

2020
const TextInlineView = require('../../components/TextInlineView');
2121
const React = require('react');
@@ -1063,13 +1063,6 @@ const examples = [
10631063
);
10641064
},
10651065
},
1066-
{
1067-
title: 'Empty Text',
1068-
description: "It's ok to have Text with zero or null children.",
1069-
render: function (): React.Node {
1070-
return <Text />;
1071-
},
1072-
},
10731066
{
10741067
title: 'Toggling Attributes',
10751068
render: function (): React.MixedElement {
@@ -1590,7 +1583,7 @@ const examples = [
15901583
);
15911584
},
15921585
},
1593-
TextInlineViewsExample,
1586+
...TextSharedExamples,
15941587
];
15951588

15961589
module.exports = ({

packages/rn-tester/js/examples/Text/TextInlineViewsExample.js

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
* @format
8+
* @flow
9+
*/
10+
11+
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
12+
13+
import RNTesterText from '../../components/RNTesterText';
14+
import {useTheme} from '../../components/RNTesterTheme';
15+
import {View} from 'react-native';
16+
17+
function InlineView(props: {
18+
textAlign: 'auto' | 'left' | 'right' | 'center' | 'justify',
19+
long?: boolean,
20+
}): React.Node {
21+
return (
22+
<View style={{margin: 4}}>
23+
<RNTesterText
24+
style={{textAlign: props.textAlign, backgroundColor: 'cyan'}}>
25+
Parent
26+
<RNTesterText style={{fontWeight: 'bold'}}>Child</RNTesterText>
27+
<View style={{width: 30, height: 30, backgroundColor: 'red'}} />
28+
<RNTesterText style={{fontWeight: 'bold'}}>Child</RNTesterText>
29+
{props !== null && props.long === true && (
30+
<RNTesterText style={{fontWeight: 'bold'}}>
31+
aaaa a aaaa aaaaaa aaa a a a aaaaa sdsds dsdSAD asd ASDasd ASDas
32+
</RNTesterText>
33+
)}
34+
</RNTesterText>
35+
</View>
36+
);
37+
}
38+
39+
function TextInlineViewsExample(props: {}): React.Node {
40+
return (
41+
<View style={{flex: 1}} testID="view-test-inline-text-alignment">
42+
<RNTesterText style={{textAlign: 'center', fontSize: 14}}>
43+
BoringLayout
44+
</RNTesterText>
45+
<InlineView textAlign="left" />
46+
<InlineView textAlign="center" />
47+
<InlineView textAlign="right" />
48+
<InlineView textAlign="justify" />
49+
50+
<RNTesterText style={{textAlign: 'center', fontSize: 14}}>
51+
StaticLayout
52+
</RNTesterText>
53+
<InlineView textAlign="left" long />
54+
<InlineView textAlign="center" long />
55+
<InlineView textAlign="right" long />
56+
<InlineView textAlign="justify" long />
57+
</View>
58+
);
59+
}
60+
61+
function EmptyTextExample(): React.Node {
62+
const {BorderColor} = useTheme();
63+
return (
64+
<RNTesterText
65+
testID="empty-text"
66+
style={{
67+
borderWidth: 1,
68+
borderColor: BorderColor,
69+
}}
70+
/>
71+
);
72+
}
73+
74+
export default [
75+
{
76+
title: 'Empty Text',
77+
name: 'emptyText',
78+
render: EmptyTextExample,
79+
},
80+
{
81+
title: 'TextInlineViewsExample',
82+
name: 'inlineViews',
83+
description:
84+
'Shows how inline views are rendered when text is subject to alignment.',
85+
expect: 'The red box should align correctly with the rest of the text.',
86+
render: TextInlineViewsExample,
87+
},
88+
] as $ReadOnlyArray<RNTesterModuleExample>;

packages/rn-tester/js/types/RNTesterTypes.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
* @flow strict-local
99
*/
1010

11-
import * as React from 'react';
12-
1311
export type RNTesterModuleExample = $ReadOnly<{
1412
name?: string,
1513
title: string,
@@ -18,7 +16,7 @@ export type RNTesterModuleExample = $ReadOnly<{
1816
expect?: string,
1917
hidden?: boolean,
2018
scrollable?: boolean,
21-
render: ({testID?: ?string}) => React.Node,
19+
render: component(),
2220
}>;
2321

2422
export type RNTesterModule = $ReadOnly<{

0 commit comments

Comments
 (0)