Skip to content

Commit

Permalink
Migrate rn-tester/IntegrationTests/ImageCachePolicyTest.js to functio…
Browse files Browse the repository at this point in the history
…n components (facebook#48701)

Summary:
Pull Request resolved: facebook#48701

As per title.

Changelog: [Internal]

Reviewed By: rshest

Differential Revision: D68153254

fbshipit-source-id: 24789c814550cd592cb68a0576c5037088734a75
  • Loading branch information
fabriziocucci authored and facebook-github-bot committed Jan 15, 2025
1 parent 8c64e08 commit e8a64fd
Showing 1 changed file with 64 additions and 81 deletions.
145 changes: 64 additions & 81 deletions packages/rn-tester/IntegrationTests/ImageCachePolicyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -120,6 +105,4 @@ const styles = StyleSheet.create({
},
});

ImageCachePolicyTest.displayName = 'ImageCachePolicyTest';

module.exports = ImageCachePolicyTest;
export default ImageCachePolicyTest;

0 comments on commit e8a64fd

Please sign in to comment.