unit tests impl.

This commit is contained in:
Elad Gil
2018-06-19 16:59:43 +03:00
parent 3289391956
commit a7cf5c2ee8
3 changed files with 209 additions and 6 deletions
+62 -1
View File
@@ -1,6 +1,67 @@
import { NativeModules } from 'react-native';
const listeners = {};
// states:
// 0 - Running
// 1 - Suspended / Paused
// 2 - Cancelled / Failed
// 3 - Completed (not necessarily successfully)
NativeModules.RNBackgroundDownload = {
download: jest.fn(),
addListener: jest.fn()
pauseTask: jest.fn(),
resumeTask: jest.fn(),
stopTask: jest.fn(),
TaskRunning: 0,
TaskSuspended: 1,
TaskCanceling: 2,
TaskCompleted: 3,
checkForExistingDownloads: jest.fn().mockImplementation(() => {
foundDownloads = [
{
id: 'taskRunning',
state: NativeModules.RNBackgroundDownload.TaskRunning,
percent: 0.5,
bytesWritten: 50,
totalBytes: 100
},
{
id: 'taskPaused',
state: NativeModules.RNBackgroundDownload.TaskSuspended,
percent: 0.7,
bytesWritten: 70,
totalBytes: 100
},
{
id: 'taskCancelled',
percent: 0.9,
state: NativeModules.RNBackgroundDownload.TaskCanceling,
bytesWritten: 90,
totalBytes: 100
},
{
id: 'taskCompletedExplicit',
state: NativeModules.RNBackgroundDownload.TaskCompleted,
percent: 1,
bytesWritten: 100,
totalBytes: 100
},
{
id: 'taskCompletedImplicit',
state: NativeModules.RNBackgroundDownload.TaskCompleted,
percent: 1,
bytesWritten: 100,
totalBytes: 100
},
{
id: 'taskFailed',
state: NativeModules.RNBackgroundDownload.TaskCompleted,
percent: 0.9,
bytesWritten: 90,
totalBytes: 100
}
]
return Promise.resolve(foundDownloads);
})
};
+143 -4
View File
@@ -1,10 +1,149 @@
jest.mock('NativeEventEmitter', () => {
return class NativeEventEmitter {
static listeners = {};
addListener(channel, cb) {
NativeEventEmitter.listeners[channel] = cb;
}
};
});
import RNBackgroundDownload from '../index';
import DownloadTask from '../lib/downloadTask';
import { NativeEventEmitter, NativeModules } from 'react-native';
test('download should return a downloadTask', () => {
expect(RNBackgroundDownload.download({
const RNBackgroundDownloadNative = NativeModules.RNBackgroundDownload;
let downloadTask;
test('download function', () => {
downloadTask = RNBackgroundDownload.download({
id: 'test',
url: 'test',
destination: 'test'
})).toBeInstanceOf(DownloadTask);
});
});
expect(downloadTask).toBeInstanceOf(DownloadTask);
expect(RNBackgroundDownloadNative.download).toHaveBeenCalled();
});
test('begin event', () => {
return new Promise(resolve => {
const beginDT = RNBackgroundDownload.download({
id: 'testBegin',
url: 'test',
destination: 'test'
}).begin((expectedBytes) => {
expect(expectedBytes).toBe(9001);
expect(beginDT.state).toBe('DOWNLOADING');
resolve();
});
NativeEventEmitter.listeners.downloadBegin({
id: 'testBegin',
expctedBytes: 9001
});
});
});
test('progress event', () => {
return new Promise(resolve => {
RNBackgroundDownload.download({
id: 'testProgress',
url: 'test',
destination: 'test'
}).progress((percent, bytesWritten, totalBytes) => {
expect(percent).toBeCloseTo(0.7);
expect(bytesWritten).toBe(100);
expect(totalBytes).toBe(200);
resolve();
});
NativeEventEmitter.listeners.downloadProgress([{
id: 'testProgress',
percent: 0.7,
written: 100,
total: 200
}]);
});
});
test('done event', () => {
return new Promise(resolve => {
const doneDT = RNBackgroundDownload.download({
id: 'testDone',
url: 'test',
destination: 'test'
}).done(() => {
expect(doneDT.state).toBe('DONE');
resolve();
});
NativeEventEmitter.listeners.downloadComplete({
id: 'testDone'
});
});
});
test('fail event', () => {
return new Promise(resolve => {
const failDT = RNBackgroundDownload.download({
id: 'testFail',
url: 'test',
destination: 'test'
}).error((error) => {
expect(error).toBeInstanceOf(Error);
expect(failDT.state).toBe('FAILED');
resolve();
});
NativeEventEmitter.listeners.downloadFailed({
id: 'testFail',
error: new Error('test')
});
});
});
test('pause', () => {
const pauseDT = RNBackgroundDownload.download({
id: 'testPause',
url: 'test',
destination: 'test'
});
pauseDT.pause();
expect(pauseDT.state).toBe('PAUSED');
expect(RNBackgroundDownloadNative.pauseTask).toHaveBeenCalled();
});
test('resume', () => {
const resumeDT = RNBackgroundDownload.download({
id: 'testResume',
url: 'test',
destination: 'test'
});
resumeDT.resume();
expect(resumeDT.state).toBe('DOWNLOADING');
expect(RNBackgroundDownloadNative.resumeTask).toHaveBeenCalled();
});
test('stop', () => {
const stopDT = RNBackgroundDownload.download({
id: 'testStop',
url: 'test',
destination: 'test'
});
stopDT.stop();
expect(stopDT.state).toBe('STOPPED');
expect(RNBackgroundDownloadNative.stopTask).toHaveBeenCalled();
});
test('checkForExistingDownloads', () => {
return RNBackgroundDownload.checkForExistingDownloads()
.then(foundDownloads => {
expect(RNBackgroundDownloadNative.checkForExistingDownloads).toHaveBeenCalled();
expect(foundDownloads.length).toBe(4);
foundDownloads.forEach(foundDownload => {
expect(foundDownload).toBeInstanceOf(DownloadTask);
expect(foundDownload.state).not.toBe('FAILED');
expect(foundDownload.state).not.toBe('STOPPED');
});
})
})
+4 -1
View File
@@ -20,6 +20,9 @@
},
"jest": {
"preset": "react-native",
"setupFiles": ["./__mocks__/RNBackgroundDownload.js"]
"setupFiles": [
"./__mocks__/RNBackgroundDownload.js",
"./node_modules/react-native/Libraries/EventEmitter/__mocks__/NativeEventEmitter.js"
]
}
}