From e8a64fd6388d19e0f60554ba5ae25f4929a2b937 Mon Sep 17 00:00:00 2001 From: Fabrizio Cucci Date: Wed, 15 Jan 2025 06:11:21 -0800 Subject: [PATCH] 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 --- .../IntegrationTests/ImageCachePolicyTest.js | 145 ++++++++---------- 1 file changed, 64 insertions(+), 81 deletions(-) diff --git a/packages/rn-tester/IntegrationTests/ImageCachePolicyTest.js b/packages/rn-tester/IntegrationTests/ImageCachePolicyTest.js index 808da56ce8e980..6c9d3789b2cd1d 100644 --- a/packages/rn-tester/IntegrationTests/ImageCachePolicyTest.js +++ b/packages/rn-tester/IntegrationTests/ImageCachePolicyTest.js @@ -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 { - state: $FlowFixMe | {...} = {}; + const testComplete = ( + name: $NonMaybeType, + pass: boolean, + ) => { + setState(prevState => ({ + ...prevState, + [name]: pass, + })); + }; - shouldComponentUpdate(nextProps: Props, nextState: State): boolean { - const results: Array = 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 ( - - Hello - this.testComplete('only-if-cached', false)} - onError={() => this.testComplete('only-if-cached', true)} - style={styles.base} - /> - this.testComplete('default', true)} - onError={() => this.testComplete('default', false)} - style={styles.base} - /> - this.testComplete('reload', true)} - onError={() => this.testComplete('reload', false)} - style={styles.base} - /> - this.testComplete('force-cache', true)} - onError={() => this.testComplete('force-cache', false)} - style={styles.base} - /> - - ); - } + return ( + + Hello + testComplete('only-if-cached', false)} + onError={() => testComplete('only-if-cached', true)} + style={styles.base} + /> + testComplete('default', true)} + onError={() => testComplete('default', false)} + style={styles.base} + /> + testComplete('reload', true)} + onError={() => testComplete('reload', false)} + style={styles.base} + /> + testComplete('force-cache', true)} + onError={() => testComplete('force-cache', false)} + style={styles.base} + /> + + ); } +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;