Migrate rn-tester/IntegrationTests/ImageCachePolicyTest.js to function components (#48701)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/48701

As per title.

Changelog: [Internal]

Reviewed By: rshest

Differential Revision: D68153254

fbshipit-source-id: 24789c814550cd592cb68a0576c5037088734a75
This commit is contained in:
Fabrizio Cucci
2025-01-15 06:11:21 -08:00
committed by Facebook GitHub Bot
parent 8c64e0868e
commit e8a64fd638
@@ -10,10 +10,13 @@
'use strict';
const React = require('react');
const ReactNative = require('react-native');
const {Image, View, Text, StyleSheet} = ReactNative;
const {TestModule} = ReactNative.NativeModules;
import type {ImageURISource} from 'react-native/Libraries/Image/ImageSource';
import * as React from 'react';
import {useEffect, useState} from 'react';
import {Image, NativeModules, StyleSheet, Text, View} from 'react-native';
const {TestModule} = NativeModules;
/*
* The reload and force-cache tests don't actually verify that the complete functionality.
@@ -27,89 +30,71 @@ const {TestModule} = ReactNative.NativeModules;
const TESTS = ['only-if-cached', 'default', 'reload', 'force-cache'];
type Props = {...};
type State = {
'only-if-cached'?: boolean,
default?: boolean,
reload?: boolean,
'force-cache'?: boolean,
...
};
function ImageCachePolicyTest(): React.Node {
const [state, setState] = useState({
'only-if-cached': undefined,
default: undefined,
reload: undefined,
'force-cache': undefined,
});
class ImageCachePolicyTest extends React.Component<Props, $FlowFixMeState> {
state: $FlowFixMe | {...} = {};
const testComplete = (
name: $NonMaybeType<ImageURISource['cache']>,
pass: boolean,
) => {
setState(prevState => ({
...prevState,
[name]: pass,
}));
};
shouldComponentUpdate(nextProps: Props, nextState: State): boolean {
const results: Array<?boolean> = TESTS.map(x => nextState[x]);
useEffect(() => {
const results = TESTS.map(key => state[key]);
if (!results.includes(undefined)) {
const result: boolean = results.reduce(
(x, y) => (x === y) === true,
true,
);
const result = results.reduce((x, y) => (x === y) === true, true);
TestModule.markTestPassed(result);
}
}, [state]);
return false;
}
testComplete(name: string, pass: boolean) {
this.setState({[name]: pass});
}
render(): React.Node {
return (
<View style={styles.container}>
<Text>Hello</Text>
<Image
source={{
uri:
'https://raw.githubusercontent.com/facebook/react-native/HEAD/Libraries/NewAppScreen/components/logo.png?cacheBust=notinCache' +
Date.now(),
cache: 'only-if-cached',
}}
onLoad={() => this.testComplete('only-if-cached', false)}
onError={() => this.testComplete('only-if-cached', true)}
style={styles.base}
/>
<Image
source={{
uri:
'https://raw.githubusercontent.com/facebook/react-native/HEAD/Libraries/NewAppScreen/components/logo.png?cacheBust=notinCache' +
Date.now(),
cache: 'default',
}}
onLoad={() => this.testComplete('default', true)}
onError={() => this.testComplete('default', false)}
style={styles.base}
/>
<Image
source={{
uri:
'https://raw.githubusercontent.com/facebook/react-native/HEAD/Libraries/NewAppScreen/components/logo.png?cacheBust=notinCache' +
Date.now(),
cache: 'reload',
}}
onLoad={() => this.testComplete('reload', true)}
onError={() => this.testComplete('reload', false)}
style={styles.base}
/>
<Image
source={{
uri:
'https://raw.githubusercontent.com/facebook/react-native/HEAD/Libraries/NewAppScreen/components/logo.png?cacheBust=notinCache' +
Date.now(),
cache: 'force-cache',
}}
onLoad={() => this.testComplete('force-cache', true)}
onError={() => this.testComplete('force-cache', false)}
style={styles.base}
/>
</View>
);
}
return (
<View style={styles.container}>
<Text>Hello</Text>
<Image
source={getImageSource('only-if-cached')}
onLoad={() => testComplete('only-if-cached', false)}
onError={() => testComplete('only-if-cached', true)}
style={styles.base}
/>
<Image
source={getImageSource('default')}
onLoad={() => testComplete('default', true)}
onError={() => testComplete('default', false)}
style={styles.base}
/>
<Image
source={getImageSource('reload')}
onLoad={() => testComplete('reload', true)}
onError={() => testComplete('reload', false)}
style={styles.base}
/>
<Image
source={getImageSource('force-cache')}
onLoad={() => testComplete('force-cache', true)}
onError={() => testComplete('force-cache', false)}
style={styles.base}
/>
</View>
);
}
const getImageSource = (cache: ImageURISource['cache']) => ({
uri:
'https://raw.githubusercontent.com/facebook/react-native/HEAD/Libraries/NewAppScreen/components/logo.png?cacheBust=notinCache' +
Date.now(),
cache,
});
const styles = StyleSheet.create({
container: {
flex: 1,
@@ -120,6 +105,4 @@ const styles = StyleSheet.create({
},
});
ImageCachePolicyTest.displayName = 'ImageCachePolicyTest';
module.exports = ImageCachePolicyTest;
export default ImageCachePolicyTest;