Skip to content

Commit

Permalink
Commits that were supposed to be included with last commit...
Browse files Browse the repository at this point in the history
  • Loading branch information
billmalarky committed Nov 2, 2017
1 parent e9bfa5f commit d67eccc
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 4 deletions.
110 changes: 110 additions & 0 deletions tests/CacheableImage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

// Define globals for eslint.
/* global describe it */

// Load dependencies
import should from 'should'; // eslint-disable-line no-unused-vars
import { mockData } from './mockData';
import imageCacheHoc from '../lib/imageCacheHoc';
import { Image } from 'react-native';

describe('CacheableImage', function() {

it('Component property type validation should exist.', () => {

const CacheableImage = imageCacheHoc(Image);

Object.keys(CacheableImage.propTypes).should.deepEqual([
'fileHostWhitelist',
'source',
'permanent',
'style'
]);

});

it('#constructor should initialize class object properties correctly.', () => {

const CacheableImage = imageCacheHoc(Image);

const cacheableImage = new CacheableImage(mockData.mockCacheableImageProps);

// Ensure defaults set correctly
cacheableImage.props.should.have.properties(mockData.mockCacheableImageProps);
cacheableImage.state.should.have.properties({
localFilePath: null
});
cacheableImage.options.should.have.properties({
validProtocols: [ 'https' ],
fileHostWhitelist: [],
cachePruneTriggerLimit: 15728640,
fileDirName: null
});
cacheableImage.fileSystem.should.have.properties({
os: 'ios',
cachePruneTriggerLimit: 15728640,
baseFilePath: mockData.basePath + '/react-native-image-cache-hoc/'
});

});

it('#_validateImageComponent should validate bad component props correctly.', () => {

// Verify source uri prop only accepts web accessible urls.

const CacheableImage = imageCacheHoc(Image);

try {

const cacheableImage = new CacheableImage({ // eslint-disable-line no-unused-vars
source: {
uri: './local-file.jpg'
}
});

throw new Error('Invalid source uri prop was accepted.');
} catch (error) {
error.should.deepEqual(new Error('Invalid source prop. <CacheableImage> props.source.uri should be a web accessible url.'));
}

// Verify source uri prop only accepts web accessible urls from whitelist if whitelist set.

const CacheableImageWithOpts = imageCacheHoc(Image, {
fileHostWhitelist: [ 'i.redd.it' ]
});

try {

const cacheableImageWithOpts = new CacheableImageWithOpts({ // eslint-disable-line no-unused-vars
source: {
uri: 'https://www.google.com/logos/doodles/2017/day-of-the-dead-2017-6241959625621504-l.png'
}
});

throw new Error('Invalid source uri prop was accepted.');
} catch (error) {
error.should.deepEqual(new Error('Invalid source prop. <CacheableImage> props.source.uri should be a web accessible url.'));
}

// Verify source uri prop only accepts web accessible urls from correct protocols if protocol list set.

const CacheableImageWithProtocolOpts = imageCacheHoc(Image, {
validProtocols: [ 'http' ]
});

try {

const cacheableImageWithProtocolOpts = new CacheableImageWithProtocolOpts({ // eslint-disable-line no-unused-vars
source: {
uri: 'https://www.google.com/logos/doodles/2017/day-of-the-dead-2017-6241959625621504-l.png'
}
});

throw new Error('Invalid source uri prop was accepted.');
} catch (error) {
error.should.deepEqual(new Error('Invalid source prop. <CacheableImage> props.source.uri should be a web accessible url.'));
}

});

});
37 changes: 37 additions & 0 deletions tests/__snapshots__/imageCacheHoc.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CacheableImage renders correctly 1`] = `
<View
style={
Object {
"alignItems": "center",
"flex": 1,
"justifyContent": "center",
}
}
>
<Text
accessible={true}
allowFontScaling={true}
disabled={false}
ellipsizeMode="tail"
style={
Object {
"fontSize": 20,
"margin": 10,
"textAlign": "center",
}
}
>
Test CacheableImage Component
</Text>
<Image
style={
Object {
"height": 204,
"width": 150,
}
}
/>
</View>
`;
6 changes: 3 additions & 3 deletions tests/imageCacheHoc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import should from 'should'; // eslint-disable-line no-unused-vars
import React from 'react';
import 'react-native';
import imageCacheHOC from '../lib/imageCacheHoc';
import imageCacheHoc from '../lib/imageCacheHoc';
import {
StyleSheet,
View,
Expand All @@ -24,7 +24,7 @@ describe('CacheableImage', function() {

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

const CacheableImage = imageCacheHOC(Image);
const CacheableImage = imageCacheHoc(Image);

const styles = StyleSheet.create({
container: {
Expand All @@ -46,7 +46,7 @@ describe('CacheableImage', function() {
const tree = renderer.create(
<View style={styles.container}>
<Text style={styles.welcome}>Test CacheableImage Component</Text>
<CacheableImage style={styles.image} source={{uri: mockData.externalImageResource}} permanent={false} />
<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.
Expand Down
7 changes: 6 additions & 1 deletion tests/mockData.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@
*/
export const mockData = {
basePath: '/base/file/path',
externalImageResource: 'https://i.redd.it/rc29s4bz61uz.png'
mockCacheableImageProps: {
source: {
uri: 'https://i.redd.it/rc29s4bz61uz.png'
},
permanent: false
}
};

0 comments on commit d67eccc

Please sign in to comment.