From a7cf5c2ee828ebcef34c98dc5017b217eeec8756 Mon Sep 17 00:00:00 2001 From: Elad Gil Date: Tue, 19 Jun 2018 16:59:43 +0300 Subject: [PATCH] unit tests impl. --- __mocks__/RNBackgroundDownload.js | 63 ++++++++++++- __tests__/mainTest.js | 147 +++++++++++++++++++++++++++++- package.json | 5 +- 3 files changed, 209 insertions(+), 6 deletions(-) diff --git a/__mocks__/RNBackgroundDownload.js b/__mocks__/RNBackgroundDownload.js index 96168d6..dfae056 100644 --- a/__mocks__/RNBackgroundDownload.js +++ b/__mocks__/RNBackgroundDownload.js @@ -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); + }) }; \ No newline at end of file diff --git a/__tests__/mainTest.js b/__tests__/mainTest.js index 3115b49..7f0c35e 100644 --- a/__tests__/mainTest.js +++ b/__tests__/mainTest.js @@ -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); -}); \ No newline at end of file + }); + 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'); + }); + }) +}) \ No newline at end of file diff --git a/package.json b/package.json index 0fdd0f7..3306ee7 100644 --- a/package.json +++ b/package.json @@ -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" + ] } }