Skip to content

Commit

Permalink
Fix assert Turbo Module example crash the app when tap Run all tests
Browse files Browse the repository at this point in the history
Summary:
Same as facebook#41521 for the Java / ObjC Turbo Module example.

[INTERNAL] [FIXED] - [RN-Tester] Fix assert Turbo Module example crash the app when tap `Run all tests`

Differential Revision: D57531736
  • Loading branch information
christophpurrer authored and facebook-github-bot committed May 19, 2024
1 parent 93c079b commit fb365e7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type State = {|

type Examples =
| 'callback'
| 'callbackWithSubscription'
| 'getArray'
| 'getBool'
| 'getConstants'
Expand Down Expand Up @@ -221,7 +222,7 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
};

_setResult(
name: string | Examples,
name: Examples | ErrorExamples,
result:
| $FlowFixMe
| void
Expand Down Expand Up @@ -271,6 +272,7 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
style={[styles.column, styles.button]}
onPress={() =>
Object.keys(this._tests).forEach(item =>
// $FlowFixMe
this._setResult(item, this._tests[item]()),
)
}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ type State = {|
},
|};

type Examples =
| 'callback'
| 'getArray'
| 'getBool'
| 'getConstants'
| 'getCustomEnum'
| 'getCustomHostObject'
| 'getBinaryTreeNode'
| 'getGraphNode'
| 'getNumEnum'
| 'getStrEnum'
| 'getMap'
| 'getNumber'
| 'getObject'
| 'getSet'
| 'getString'
| 'getUnion'
| 'getValue'
| 'promise'
| 'rejectPromise'
| 'voidFunc'
| 'setMenuItem'
| 'optionalArgs'
| 'emitDeviceEvent';

type ErrorExamples =
| 'voidFuncThrows'
| 'getObjectThrows'
| 'promiseThrows'
| 'voidFuncAssert'
| 'getObjectAssert'
| 'promiseAssert';

class SampleTurboModuleExample extends React.Component<{||}, State> {
static contextType: React$Context<RootTag> = RootTagContext;

Expand All @@ -55,7 +88,6 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
NativeSampleTurboModule.getValueWithPromise(true)
.then(() => {})
.catch(e => {
console.error(e);
this._setResult('rejectPromise', e.message);
}),
getConstants: () => NativeSampleTurboModule.getConstants(),
Expand All @@ -80,6 +112,10 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
getRootTag: () => NativeSampleTurboModule.getRootTag(this.context),
getValue: () =>
NativeSampleTurboModule.getValue(5, 'test', {a: 1, b: 'foo'}),
};

// $FlowFixMe[missing-local-annot]
_errorTests = {
voidFuncThrows: () => {
try {
NativeSampleTurboModule.voidFuncThrows?.();
Expand All @@ -101,7 +137,6 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
.then(() => {})
.catch(e => {
console.error(e);
this._setResult('promiseThrows', e.message);
});
},
voidFuncAssert: () => {
Expand All @@ -125,34 +160,12 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
.then(() => {})
.catch(e => {
console.error(e);
this._setResult('promiseAssert', e.message);
});
},
};

_setResult(
name:
| string
| 'callback'
| 'getArray'
| 'getBool'
| 'getEnum'
| 'getConstants'
| 'getNumber'
| 'getObject'
| 'getRootTag'
| 'getString'
| 'getUnsafeObject'
| 'getValue'
| 'promise'
| 'rejectPromise'
| 'voidFunc'
| 'voidFuncThrows'
| 'getObjectThrows'
| 'promiseThrows'
| 'voidFuncAssert'
| 'getObjectAssert'
| 'promiseAssert',
name: Examples | ErrorExamples,
result:
| $FlowFixMe
| void
Expand Down Expand Up @@ -204,6 +217,7 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
style={[styles.column, styles.button]}
onPress={() =>
Object.keys(this._tests).forEach(item =>
// $FlowFixMe[incompatible-call]
this._setResult(item, this._tests[item]()),
)
}>
Expand All @@ -216,9 +230,10 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
</TouchableOpacity>
</View>
<FlatList
// $FlowFixMe[incompatible-type-arg]
data={Object.keys(this._tests)}
keyExtractor={item => item}
renderItem={({item}) => (
renderItem={({item}: {item: Examples, ...}) => (
<View style={styles.item}>
<TouchableOpacity
style={[styles.column, styles.button]}
Expand All @@ -229,6 +244,24 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
</View>
)}
/>
<View style={styles.item}>
<Text style={styles.buttonTextLarge}>Report errors tests</Text>
</View>
<FlatList
// $FlowFixMe[incompatible-type-arg]
data={Object.keys(this._errorTests)}
keyExtractor={item => item}
renderItem={({item}: {item: ErrorExamples, ...}) => (
<View style={styles.item}>
<TouchableOpacity
style={[styles.column, styles.button]}
onPress={e => this._setResult(item, this._errorTests[item]())}>
<Text style={styles.buttonText}>{item}</Text>
</TouchableOpacity>
<View style={[styles.column]}>{this._renderResult(item)}</View>
</View>
)}
/>
</View>
);
}
Expand Down

0 comments on commit fb365e7

Please sign in to comment.