Skip to content

Commit

Permalink
Migrate rn-tester/IntegrationTests/IntegrationTestHarnessTest.js to f…
Browse files Browse the repository at this point in the history
…unction components (#48671)

Summary:
Pull Request resolved: #48671

As per title.

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D68151272

fbshipit-source-id: 5d42da93ab4fe8aaf34b8916a6d0a234f51c235a
  • Loading branch information
fabriziocucci authored and facebook-github-bot committed Jan 14, 2025
1 parent 2204ec9 commit 9d1e24c
Showing 1 changed file with 34 additions and 52 deletions.
86 changes: 34 additions & 52 deletions packages/rn-tester/IntegrationTests/IntegrationTestHarnessTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,49 @@

'use strict';

const React = require('react');
const ReactNative = require('react-native');
import * as React from 'react';
import {useEffect, useState} from 'react';
import {NativeModules, StyleSheet, Text, View} from 'react-native';

const {Text, View, StyleSheet} = ReactNative;
const {TestModule} = ReactNative.NativeModules;
const {TestModule} = NativeModules;

type Props = $ReadOnly<{|
type Props = $ReadOnly<{
shouldThrow?: boolean,
waitOneFrame?: boolean,
|}>;
}>;

type State = {|
done: boolean,
|};
function IntegrationTestHarnessTest(props: Props): React.Node {
const [done, setDone] = useState(false);

class IntegrationTestHarnessTest extends React.Component<Props, State> {
state: State = {
done: false,
};
useEffect(() => {
const runTest = () => {
if (props.shouldThrow) {
throw new Error('Throwing error because shouldThrow');
}
if (!TestModule) {
throw new Error('RCTTestModule is not registered.');
} else if (!TestModule.markTestCompleted) {
throw new Error('RCTTestModule.markTestCompleted not defined.');
}
setDone(true);
TestModule.markTestCompleted();
};

componentDidMount() {
if (this.props.waitOneFrame) {
requestAnimationFrame(this.runTest);
if (props.waitOneFrame) {
requestAnimationFrame(runTest);
} else {
this.runTest();
}
}

runTest: () => void = () => {
if (this.props.shouldThrow) {
throw new Error('Throwing error because shouldThrow');
runTest();
}
if (!TestModule) {
throw new Error('RCTTestModule is not registered.');
} else if (!TestModule.markTestCompleted) {
throw new Error('RCTTestModule.markTestCompleted not defined.');
}
this.setState({done: true}, () => {
TestModule.markTestCompleted();
});
};
}, [props.shouldThrow, props.waitOneFrame]);

render(): React.Node {
return (
<View style={styles.container}>
<Text>
{
/* $FlowFixMe[incompatible-type] (>=0.54.0 site=react_native_fb,react_
* native_oss) This comment suppresses an error found when Flow v0.54
* was deployed. To see the error delete this comment and run Flow.
*/
// $FlowFixMe[unsafe-addition]
this.constructor.displayName + ': '
}
{this.state.done ? 'Done' : 'Testing...'}
</Text>
</View>
);
}
return (
<View style={styles.container}>
<Text>
{IntegrationTestHarnessTest.name + ': '}
{done ? 'Done' : 'Testing...'}
</Text>
</View>
);
}

const styles = StyleSheet.create({
Expand All @@ -78,6 +62,4 @@ const styles = StyleSheet.create({
},
});

IntegrationTestHarnessTest.displayName = 'IntegrationTestHarnessTest';

module.exports = IntegrationTestHarnessTest;
export default IntegrationTestHarnessTest;

0 comments on commit 9d1e24c

Please sign in to comment.