Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit d67eccc

Browse files
committed
Commits that were supposed to be included with last commit...
1 parent e9bfa5f commit d67eccc

File tree

4 files changed

+156
-4
lines changed

4 files changed

+156
-4
lines changed

tests/CacheableImage.test.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
// Define globals for eslint.
3+
/* global describe it */
4+
5+
// Load dependencies
6+
import should from 'should'; // eslint-disable-line no-unused-vars
7+
import { mockData } from './mockData';
8+
import imageCacheHoc from '../lib/imageCacheHoc';
9+
import { Image } from 'react-native';
10+
11+
describe('CacheableImage', function() {
12+
13+
it('Component property type validation should exist.', () => {
14+
15+
const CacheableImage = imageCacheHoc(Image);
16+
17+
Object.keys(CacheableImage.propTypes).should.deepEqual([
18+
'fileHostWhitelist',
19+
'source',
20+
'permanent',
21+
'style'
22+
]);
23+
24+
});
25+
26+
it('#constructor should initialize class object properties correctly.', () => {
27+
28+
const CacheableImage = imageCacheHoc(Image);
29+
30+
const cacheableImage = new CacheableImage(mockData.mockCacheableImageProps);
31+
32+
// Ensure defaults set correctly
33+
cacheableImage.props.should.have.properties(mockData.mockCacheableImageProps);
34+
cacheableImage.state.should.have.properties({
35+
localFilePath: null
36+
});
37+
cacheableImage.options.should.have.properties({
38+
validProtocols: [ 'https' ],
39+
fileHostWhitelist: [],
40+
cachePruneTriggerLimit: 15728640,
41+
fileDirName: null
42+
});
43+
cacheableImage.fileSystem.should.have.properties({
44+
os: 'ios',
45+
cachePruneTriggerLimit: 15728640,
46+
baseFilePath: mockData.basePath + '/react-native-image-cache-hoc/'
47+
});
48+
49+
});
50+
51+
it('#_validateImageComponent should validate bad component props correctly.', () => {
52+
53+
// Verify source uri prop only accepts web accessible urls.
54+
55+
const CacheableImage = imageCacheHoc(Image);
56+
57+
try {
58+
59+
const cacheableImage = new CacheableImage({ // eslint-disable-line no-unused-vars
60+
source: {
61+
uri: './local-file.jpg'
62+
}
63+
});
64+
65+
throw new Error('Invalid source uri prop was accepted.');
66+
} catch (error) {
67+
error.should.deepEqual(new Error('Invalid source prop. <CacheableImage> props.source.uri should be a web accessible url.'));
68+
}
69+
70+
// Verify source uri prop only accepts web accessible urls from whitelist if whitelist set.
71+
72+
const CacheableImageWithOpts = imageCacheHoc(Image, {
73+
fileHostWhitelist: [ 'i.redd.it' ]
74+
});
75+
76+
try {
77+
78+
const cacheableImageWithOpts = new CacheableImageWithOpts({ // eslint-disable-line no-unused-vars
79+
source: {
80+
uri: 'https://www.google.com/logos/doodles/2017/day-of-the-dead-2017-6241959625621504-l.png'
81+
}
82+
});
83+
84+
throw new Error('Invalid source uri prop was accepted.');
85+
} catch (error) {
86+
error.should.deepEqual(new Error('Invalid source prop. <CacheableImage> props.source.uri should be a web accessible url.'));
87+
}
88+
89+
// Verify source uri prop only accepts web accessible urls from correct protocols if protocol list set.
90+
91+
const CacheableImageWithProtocolOpts = imageCacheHoc(Image, {
92+
validProtocols: [ 'http' ]
93+
});
94+
95+
try {
96+
97+
const cacheableImageWithProtocolOpts = new CacheableImageWithProtocolOpts({ // eslint-disable-line no-unused-vars
98+
source: {
99+
uri: 'https://www.google.com/logos/doodles/2017/day-of-the-dead-2017-6241959625621504-l.png'
100+
}
101+
});
102+
103+
throw new Error('Invalid source uri prop was accepted.');
104+
} catch (error) {
105+
error.should.deepEqual(new Error('Invalid source prop. <CacheableImage> props.source.uri should be a web accessible url.'));
106+
}
107+
108+
});
109+
110+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`CacheableImage renders correctly 1`] = `
4+
<View
5+
style={
6+
Object {
7+
"alignItems": "center",
8+
"flex": 1,
9+
"justifyContent": "center",
10+
}
11+
}
12+
>
13+
<Text
14+
accessible={true}
15+
allowFontScaling={true}
16+
disabled={false}
17+
ellipsizeMode="tail"
18+
style={
19+
Object {
20+
"fontSize": 20,
21+
"margin": 10,
22+
"textAlign": "center",
23+
}
24+
}
25+
>
26+
Test CacheableImage Component
27+
</Text>
28+
<Image
29+
style={
30+
Object {
31+
"height": 204,
32+
"width": 150,
33+
}
34+
}
35+
/>
36+
</View>
37+
`;

tests/imageCacheHoc.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import should from 'should'; // eslint-disable-line no-unused-vars
88
import React from 'react';
99
import 'react-native';
10-
import imageCacheHOC from '../lib/imageCacheHoc';
10+
import imageCacheHoc from '../lib/imageCacheHoc';
1111
import {
1212
StyleSheet,
1313
View,
@@ -24,7 +24,7 @@ describe('CacheableImage', function() {
2424

2525
it('renders correctly', () => {
2626

27-
const CacheableImage = imageCacheHOC(Image);
27+
const CacheableImage = imageCacheHoc(Image);
2828

2929
const styles = StyleSheet.create({
3030
container: {
@@ -46,7 +46,7 @@ describe('CacheableImage', function() {
4646
const tree = renderer.create(
4747
<View style={styles.container}>
4848
<Text style={styles.welcome}>Test CacheableImage Component</Text>
49-
<CacheableImage style={styles.image} source={{uri: mockData.externalImageResource}} permanent={false} />
49+
<CacheableImage style={styles.image} source={mockData.mockCacheableImageProps.source} permanent={mockData.mockCacheableImageProps.permanent} />
5050
</View>
5151
);
5252
expect(tree).toMatchSnapshot(); //If UI changes, this snapshot must be updated. See comment below.

tests/mockData.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@
77
*/
88
export const mockData = {
99
basePath: '/base/file/path',
10-
externalImageResource: 'https://i.redd.it/rc29s4bz61uz.png'
10+
mockCacheableImageProps: {
11+
source: {
12+
uri: 'https://i.redd.it/rc29s4bz61uz.png'
13+
},
14+
permanent: false
15+
}
1116
};

0 commit comments

Comments
 (0)