-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathimageCacheHoc.test.js
207 lines (176 loc) · 6.36 KB
/
imageCacheHoc.test.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Define globals for eslint.
/* global describe it */
/* global expect require */
// Load dependencies
import should from 'should'; // eslint-disable-line no-unused-vars
import React from 'react';
import 'react-native';
import imageCacheHoc from '../lib/imageCacheHoc';
import {
StyleSheet,
View,
Text,
Image
} from 'react-native';
import { mockData } from './mockData';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
// Ensure component can mount successfully.
describe('CacheableImage', function() {
it('renders correctly', () => {
//Mock values for local/remote file request logic.
const RNFetchBlob = require('rn-fetch-blob');
RNFetchBlob.fs.exists
.mockReturnValueOnce(false) // mock not exist in local permanent dir
.mockReturnValueOnce(false) // mock not exist in local cache dir
.mockReturnValueOnce(false) // mock does not exist to get past clobber
.mockReturnValue(true);
RNFetchBlob.fetch
.mockReturnValue({
path: () => {
return '/this/is/path/to/file.jpg';
}
});
const CacheableImage = imageCacheHoc(Image);
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
},
image: {
width:150,
height: 204
}
});
const tree = renderer.create(
<View style={styles.container}>
<Text style={styles.welcome}>Test CacheableImage Component</Text>
<CacheableImage style={styles.image} source={mockData.mockCacheableImageProps.source} permanent={mockData.mockCacheableImageProps.permanent} />
</View>
);
expect(tree).toMatchSnapshot(); //If UI changes, this snapshot must be updated. See comment below.
/**
The next time you run the tests, the rendered output will be compared to the previously created snapshot.
The snapshot should be committed along code changes. When a snapshot test fails, you need to inspect whether it is an intended or unintended change.
If the change is expected you can invoke Jest with npm test -- -u to overwrite the existing snapshot.
*/
});
it('renders correctly with placeholder prop set', () => {
//Mock values for local/remote file request logic.
const RNFetchBlob = require('rn-fetch-blob');
RNFetchBlob.fs.exists
.mockReturnValueOnce(false) // mock not exist in local permanent dir
.mockReturnValueOnce(false) // mock not exist in local cache dir
.mockReturnValueOnce(false) // mock does not exist to get past clobber
.mockReturnValue(true);
RNFetchBlob.fetch
.mockReturnValue({
path: () => {
return '/this/is/path/to/file.jpg';
}
});
const CacheableImage = imageCacheHoc(Image);
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
},
image: {
width:150,
height: 204
},
placeholderImage: { // eslint-disable-line react-native/no-color-literals
width:150,
height: 204,
backgroundColor: '#00ffff'
}
});
const propPlaceholder = {
component: Image,
props: {
style: styles.propPlaceholder
}
};
const tree = renderer.create(
<View style={styles.container}>
<Text style={styles.welcome}>Test CacheableImage Component</Text>
<CacheableImage style={styles.image} source={mockData.mockCacheableImageProps.source} permanent={mockData.mockCacheableImageProps.permanent} placeholder={propPlaceholder} />
</View>
);
expect(tree).toMatchSnapshot(); //If UI changes, this snapshot must be updated. See comment below.
/**
The next time you run the tests, the rendered output will be compared to the previously created snapshot.
The snapshot should be committed along code changes. When a snapshot test fails, you need to inspect whether it is an intended or unintended change.
If the change is expected you can invoke Jest with npm test -- -u to overwrite the existing snapshot.
*/
});
it('renders correctly with placeholder option set', () => {
//Mock values for local/remote file request logic.
const RNFetchBlob = require('rn-fetch-blob');
RNFetchBlob.fs.exists
.mockReturnValueOnce(false) // mock not exist in local permanent dir
.mockReturnValueOnce(false) // mock not exist in local cache dir
.mockReturnValueOnce(false) // mock does not exist to get past clobber
.mockReturnValue(true);
RNFetchBlob.fetch
.mockReturnValue({
path: () => {
return '/this/is/path/to/file.jpg';
}
});
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
},
image: {
width:150,
height: 204
},
optionPlaceholder: { // eslint-disable-line react-native/no-color-literals
width:150,
height: 204,
backgroundColor: '#dc143c'
}
});
const optionPlaceholder = {
component: Image,
props: {
style: styles.optionPlaceholder
}
};
const CacheableImage = imageCacheHoc(Image, {
defaultPlaceholder: optionPlaceholder
});
const tree = renderer.create(
<View style={styles.container}>
<Text style={styles.welcome}>Test CacheableImage Component</Text>
<CacheableImage style={styles.image} source={mockData.mockCacheableImageProps.source} permanent={mockData.mockCacheableImageProps.permanent} />
</View>
);
expect(tree).toMatchSnapshot(); //If UI changes, this snapshot must be updated. See comment below.
/**
The next time you run the tests, the rendered output will be compared to the previously created snapshot.
The snapshot should be committed along code changes. When a snapshot test fails, you need to inspect whether it is an intended or unintended change.
If the change is expected you can invoke Jest with npm test -- -u to overwrite the existing snapshot.
*/
});
});