Skip to content

Commit

Permalink
initial commit for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kfiroo committed Sep 13, 2017
1 parent 622161a commit 78ae7ee
Show file tree
Hide file tree
Showing 6 changed files with 4,579 additions and 125 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["react-native"]
}
48 changes: 48 additions & 0 deletions __tests__/ImageCacheManager-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

jest.mock('react-native-fetch-blob', () => ({default: {fs: {}}}));
jest.mock('react-native-clcasher/MemoryCache', () => ({default: {}}));

import ImageCacheManager from '../ImageCacheManager';
import SimpleMemoryCache from './SimpleMemoryCache';
import SimpleMemoryFs from './SimpleMemoryFs';

const icm = ImageCacheManager({}, SimpleMemoryCache, SimpleMemoryFs);

describe('ImageCacheManager', () => {

beforeEach(() => icm.clearCache());

describe('downloadAndCacheUrl', () => {

it('should fail if URL is not cacheable', () => {
return icm.getCacheInfo()
.then(res => console.log(res))
.then(() => {
return expect(icm.downloadAndCacheUrl('not a real url')).rejects.toBeDefined();
});
});

it('should download a file when not in cache', () => {
return icm.getCacheInfo()
.then(res => console.log(res))
.then(() => icm.downloadAndCacheUrl('https://example.com/image.jpg'))
.then(() => icm.getCacheInfo())
.then(res => console.log(res))
});

it('should add new entry to the cache if not in cache', () => {

});

it('should return file name if image is in cache', () => {

});

it('should not return cached entry if expired', () => {

});

});

});
39 changes: 39 additions & 0 deletions __tests__/SimpleMemoryCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const DEFAULT_EXPIRES = 999999;

function currentTime() {
return Math.floor((new Date().getTime() / 1000));
}

let cache = {};

const SimpleMemoryCache = {};

SimpleMemoryCache.set = (key, value, expires = DEFAULT_EXPIRES) => {
cache[key] = {
value: value,
expires: currentTime() + parseInt(expires)
};
return Promise.resolve();
};

SimpleMemoryCache.get = (key) => {
const curTime = currentTime();
const v = cache[key];
if (v && v.expires && v.expires >= curTime) {
return Promise.resolve(v.value);
}
return Promise.resolve();
};

SimpleMemoryCache.remove = async (key) => {
delete cache[key];
return Promise.resolve();
};

SimpleMemoryCache.flush = async () => {
cache = {};
return Promise.resolve();
};
export default SimpleMemoryCache;
87 changes: 87 additions & 0 deletions __tests__/SimpleMemoryFs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'use strict';

const _ = require('lodash');

let fs = {};

/**
* wrapper around common filesystem actions
*/
module.exports = {

/**
* returns the local cache dir
* @returns {String}
*/
getCacheDir() {
return '/imagesCacheDir';
},

/**
* returns a promise that is resolved when the download of the requested file
* is complete and the file is saved.
* if the download fails, or was stopped the partial file is deleted, and the
* promise is rejected
* @param fromUrl String source url
* @param toFile String destination path
* @param headers Object with headers to use when downloading the file
* @returns {Promise}
*/
downloadFile(fromUrl, toFile, headers) {
fs[toFile] = fromUrl;
return Promise.resolve(toFile);
},

/**
* remove the file in filePath if it exists.
* this method always resolves
* @param filePath
* @returns {Promise}
*/
deleteFile(filePath) {
delete fs[filePath];
return Promise.resolve();
},

/**
* copy a file from fromFile to toFile
* @param fromFile
* @param toFile
* @returns {Promise}
*/
copyFile(fromFile, toFile) {
fs[toFile] = fs[fromFile] || fromFile;
return Promise.resolve();
},

/**
* remove the contents of dirPath
* @param dirPath
* @returns {Promise}
*/
cleanDir(dirPath) {
fs = _.omitBy(fs, (v, k) => _.startsWith(k, dirPath));
return Promise.resolve();
},

/**
* get info about files in a folder
* @param dirPath
* @returns {Promise.<{file:Array, size:Number}>}
*/
getDirInfo(dirPath) {
const files = _(fs)
.pickBy((v, k) => _.startsWith(k, dirPath))
.map((v, k) => ({
filename: k,
source: v,
size: 1
}))
.value();
return Promise.resolve({
files,
size: files.length
});
},

};
27 changes: 26 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@
"description": "CachedImage component for react-native",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "./node_modules/.bin/jest"
},
"jest": {
"preset": "react-native",
"globals": {
"NODE_ENV": "test"
},
"testPathIgnorePatterns": [
"/node_modules/",
"/CachedImageExample/"
],
"modulePathIgnorePatterns": [
"/CachedImageExample/"
],
"verbose": true
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -38,5 +52,16 @@
"react-native-clcasher": "1.0.0",
"react-native-fetch-blob": "0.10.8",
"url-parse": "1.1.9"
},
"devDependencies": {
"babel-jest": "^20.0.3",
"babel-preset-react-native": "^2.1.0",
"jest": "^20.0.4",
"jest-react-native": "^18.0.0",
"react": "16.0.0-alpha.12",
"react-native": "^0.47.1",
"react-test-renderer": "^15.6.1",
"regenerator-runtime": "^0.10.5",
"babel-preset-env": "*"
}
}
Loading

0 comments on commit 78ae7ee

Please sign in to comment.