diff --git a/__tests__/mainTest.js b/__tests__/mainTest.js index 4097053..80c42de 100644 --- a/__tests__/mainTest.js +++ b/__tests__/mainTest.js @@ -41,7 +41,7 @@ test('begin event', () => { }); NativeEventEmitter.listeners.downloadBegin({ id: 'testBegin', - expctedBytes: 9001 + expectedBytes: 9001 }); }); }); @@ -159,17 +159,17 @@ test('wrong handler type', () => { expect(() => { dt.begin('not function'); - }).toThrow('handler must be a function'); + }).toThrow(); expect(() => { dt.progress(7); - }).toThrow('handler must be a function'); + }).toThrow(); expect(() => { dt.done({iamnota: 'function'}); - }).toThrow('handler must be a function'); + }).toThrow(); expect(() => { dt.error('not function'); - }).toThrow('handler must be a function'); + }).toThrow(); }); \ No newline at end of file diff --git a/index.js b/index.js index c572c3e..492114e 100644 --- a/index.js +++ b/index.js @@ -53,6 +53,7 @@ export function checkForExistingDownloads() { if (taskInfo.bytesWritten === taskInfo.totalBytes) { task.state = 'DONE'; } else { + // IOS completed the download but it was not done. return null; } } diff --git a/lib/downloadTask.js b/lib/downloadTask.js index 6b15d33..72e5a39 100644 --- a/lib/downloadTask.js +++ b/lib/downloadTask.js @@ -1,6 +1,11 @@ import { NativeModules } from 'react-native'; const { RNBackgroundDownload } = NativeModules; +function validateHandler(handler) { + if (!(typeof handler === 'function')) { + throw new TypeError(`[RNBackgroundDownload] expected argument to be a function, got: ${typeof handler}`); + } +} export default class DownloadTask { state = 'PENDING' percent = 0 @@ -19,33 +24,25 @@ export default class DownloadTask { } begin(handler) { - if (!(typeof handler === 'function')) { - throw new TypeError('handler must be a function'); - } + validateHandler(handler); this._beginHandler = handler; return this; } progress(handler) { - if (!(typeof handler === 'function')) { - throw new TypeError('handler must be a function'); - } + validateHandler(handler); this._progressHandler = handler; return this; } done(handler) { - if (!(typeof handler === 'function')) { - throw new TypeError('handler must be a function'); - } + validateHandler(handler); this._doneHandler = handler; return this; } error(handler) { - if (!(typeof handler === 'function')) { - throw new TypeError('handler must be a function'); - } + validateHandler(handler); this._errorHandler = handler; return this; }