-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
ImageCachePolicyTest.js
125 lines (111 loc) · 3.64 KB
/
ImageCachePolicyTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/
'use strict';
const React = require('react');
const ReactNative = require('react-native');
const {Image, View, Text, StyleSheet} = ReactNative;
const {TestModule} = ReactNative.NativeModules;
/*
* The reload and force-cache tests don't actually verify that the complete functionality.
*
* reload: Should have the server set a long cache header, then swap the image on next load
* with the test comparing the old image to the new image and making sure they are different.
*
* force-cache: Should do the above but set a no-cache header. The test should compare the first
* image with the new one and make sure they are the same.
*/
const TESTS = ['only-if-cached', 'default', 'reload', 'force-cache'];
type Props = {...};
type State = {
'only-if-cached'?: boolean,
default?: boolean,
reload?: boolean,
'force-cache'?: boolean,
...
};
class ImageCachePolicyTest extends React.Component<Props, $FlowFixMeState> {
state: $FlowFixMe | {...} = {};
shouldComponentUpdate(nextProps: Props, nextState: State): boolean {
const results: Array<?boolean> = TESTS.map(x => nextState[x]);
if (!results.includes(undefined)) {
const result: boolean = results.reduce(
(x, y) => (x === y) === true,
true,
);
TestModule.markTestPassed(result);
}
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>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
base: {
width: 100,
height: 100,
},
});
ImageCachePolicyTest.displayName = 'ImageCachePolicyTest';
module.exports = ImageCachePolicyTest;