mirror of
https://github.com/zoriya/react-native-background-downloader.git
synced 2026-05-28 08:23:25 +00:00
fix tests. android: add addListener, removeListeners to fix warnings
This commit is contained in:
@@ -9,6 +9,8 @@ import { NativeModules } from 'react-native';
|
||||
// 3 - Completed (not necessarily successfully)
|
||||
|
||||
NativeModules.RNBackgroundDownloader = {
|
||||
addListener: jest.fn(),
|
||||
removeListeners: jest.fn(),
|
||||
download: jest.fn(),
|
||||
pauseTask: jest.fn(),
|
||||
resumeTask: jest.fn(),
|
||||
|
||||
+75
-86
@@ -1,32 +1,21 @@
|
||||
/* eslint-disable */
|
||||
import RNBackgroundDownloader from '../index'
|
||||
import DownloadTask from '../lib/downloadTask'
|
||||
import { NativeModules, NativeEventEmitter } from 'react-native'
|
||||
|
||||
jest.mock('NativeEventEmitter', () => {
|
||||
return class NativeEventEmitter {
|
||||
static listeners = {};
|
||||
const RNBackgroundDownloaderNative = NativeModules.RNBackgroundDownloader
|
||||
const nativeEmitter = new NativeEventEmitter(RNBackgroundDownloaderNative)
|
||||
|
||||
addListener(channel, cb) {
|
||||
NativeEventEmitter.listeners[channel] = cb;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
import RNBackgroundDownloader from '../index';
|
||||
import DownloadTask from '../lib/downloadTask';
|
||||
import { NativeEventEmitter, NativeModules } from 'react-native';
|
||||
|
||||
const RNBackgroundDownloaderNative = NativeModules.RNBackgroundDownloader;
|
||||
|
||||
let downloadTask;
|
||||
let downloadTask
|
||||
|
||||
test('download function', () => {
|
||||
downloadTask = RNBackgroundDownloader.download({
|
||||
id: 'test',
|
||||
url: 'test',
|
||||
destination: 'test'
|
||||
});
|
||||
expect(downloadTask).toBeInstanceOf(DownloadTask);
|
||||
expect(RNBackgroundDownloaderNative.download).toHaveBeenCalled();
|
||||
});
|
||||
})
|
||||
expect(downloadTask).toBeInstanceOf(DownloadTask)
|
||||
expect(RNBackgroundDownloaderNative.download).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('begin event', () => {
|
||||
const mockedHeaders = { Etag: '123' }
|
||||
@@ -36,18 +25,18 @@ test('begin event', () => {
|
||||
url: 'test',
|
||||
destination: 'test'
|
||||
}).begin(({ expectedBytes, headers }) => {
|
||||
expect(expectedBytes).toBe(9001);
|
||||
expect(headers).toBe(mockedHeaders);
|
||||
expect(beginDT.state).toBe('DOWNLOADING');
|
||||
resolve();
|
||||
});
|
||||
NativeEventEmitter.listeners.downloadBegin({
|
||||
expect(expectedBytes).toBe(9001)
|
||||
expect(headers).toBe(mockedHeaders)
|
||||
expect(beginDT.state).toBe('DOWNLOADING')
|
||||
resolve()
|
||||
})
|
||||
nativeEmitter.emit('downloadBegin', {
|
||||
id: 'testBegin',
|
||||
expectedBytes: 9001,
|
||||
headers: mockedHeaders,
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('progress event', () => {
|
||||
return new Promise(resolve => {
|
||||
@@ -56,19 +45,19 @@ test('progress event', () => {
|
||||
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([{
|
||||
expect(percent).toBeCloseTo(0.7)
|
||||
expect(bytesWritten).toBe(100)
|
||||
expect(totalBytes).toBe(200)
|
||||
resolve()
|
||||
})
|
||||
nativeEmitter.emit('downloadProgress', [{
|
||||
id: 'testProgress',
|
||||
percent: 0.7,
|
||||
written: 100,
|
||||
total: 200
|
||||
}]);
|
||||
});
|
||||
});
|
||||
}])
|
||||
})
|
||||
})
|
||||
|
||||
test('done event', () => {
|
||||
return new Promise(resolve => {
|
||||
@@ -77,14 +66,14 @@ test('done event', () => {
|
||||
url: 'test',
|
||||
destination: 'test'
|
||||
}).done(() => {
|
||||
expect(doneDT.state).toBe('DONE');
|
||||
resolve();
|
||||
});
|
||||
NativeEventEmitter.listeners.downloadComplete({
|
||||
expect(doneDT.state).toBe('DONE')
|
||||
resolve()
|
||||
})
|
||||
nativeEmitter.emit('downloadComplete', {
|
||||
id: 'testDone'
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('fail event', () => {
|
||||
return new Promise(resolve => {
|
||||
@@ -93,86 +82,86 @@ test('fail event', () => {
|
||||
url: 'test',
|
||||
destination: 'test'
|
||||
}).error(error => {
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(failDT.state).toBe('FAILED');
|
||||
resolve();
|
||||
});
|
||||
NativeEventEmitter.listeners.downloadFailed({
|
||||
expect(error).toBeInstanceOf(Error)
|
||||
expect(failDT.state).toBe('FAILED')
|
||||
resolve()
|
||||
})
|
||||
nativeEmitter.emit('downloadFailed', {
|
||||
id: 'testFail',
|
||||
error: new Error('test')
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('pause', () => {
|
||||
const pauseDT = RNBackgroundDownloader.download({
|
||||
id: 'testPause',
|
||||
url: 'test',
|
||||
destination: 'test'
|
||||
});
|
||||
})
|
||||
|
||||
pauseDT.pause();
|
||||
expect(pauseDT.state).toBe('PAUSED');
|
||||
expect(RNBackgroundDownloaderNative.pauseTask).toHaveBeenCalled();
|
||||
});
|
||||
pauseDT.pause()
|
||||
expect(pauseDT.state).toBe('PAUSED')
|
||||
expect(RNBackgroundDownloaderNative.pauseTask).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('resume', () => {
|
||||
const resumeDT = RNBackgroundDownloader.download({
|
||||
id: 'testResume',
|
||||
url: 'test',
|
||||
destination: 'test'
|
||||
});
|
||||
})
|
||||
|
||||
resumeDT.resume();
|
||||
expect(resumeDT.state).toBe('DOWNLOADING');
|
||||
expect(RNBackgroundDownloaderNative.resumeTask).toHaveBeenCalled();
|
||||
});
|
||||
resumeDT.resume()
|
||||
expect(resumeDT.state).toBe('DOWNLOADING')
|
||||
expect(RNBackgroundDownloaderNative.resumeTask).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('stop', () => {
|
||||
const stopDT = RNBackgroundDownloader.download({
|
||||
id: 'testStop',
|
||||
url: 'test',
|
||||
destination: 'test'
|
||||
});
|
||||
})
|
||||
|
||||
stopDT.stop();
|
||||
expect(stopDT.state).toBe('STOPPED');
|
||||
expect(RNBackgroundDownloaderNative.stopTask).toHaveBeenCalled();
|
||||
});
|
||||
stopDT.stop()
|
||||
expect(stopDT.state).toBe('STOPPED')
|
||||
expect(RNBackgroundDownloaderNative.stopTask).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('checkForExistingDownloads', () => {
|
||||
return RNBackgroundDownloader.checkForExistingDownloads()
|
||||
.then(foundDownloads => {
|
||||
expect(RNBackgroundDownloaderNative.checkForExistingDownloads).toHaveBeenCalled();
|
||||
expect(foundDownloads.length).toBe(4);
|
||||
expect(RNBackgroundDownloaderNative.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');
|
||||
});
|
||||
expect(foundDownload).toBeInstanceOf(DownloadTask)
|
||||
expect(foundDownload.state).not.toBe('FAILED')
|
||||
expect(foundDownload.state).not.toBe('STOPPED')
|
||||
})
|
||||
})
|
||||
});
|
||||
})
|
||||
|
||||
test('wrong handler type', () => {
|
||||
let dt = RNBackgroundDownloader.download({
|
||||
id: 'test22222',
|
||||
url: 'test',
|
||||
destination: 'test'
|
||||
});
|
||||
})
|
||||
|
||||
expect(() => {
|
||||
dt.begin('not function');
|
||||
}).toThrow();
|
||||
dt.begin('not function')
|
||||
}).toThrow()
|
||||
|
||||
expect(() => {
|
||||
dt.progress(7);
|
||||
}).toThrow();
|
||||
dt.progress(7)
|
||||
}).toThrow()
|
||||
|
||||
expect(() => {
|
||||
dt.done({iamnota: 'function'});
|
||||
}).toThrow();
|
||||
dt.done({iamnota: 'function'})
|
||||
}).toThrow()
|
||||
|
||||
expect(() => {
|
||||
dt.error('not function');
|
||||
}).toThrow();
|
||||
});
|
||||
dt.error('not function')
|
||||
}).toThrow()
|
||||
})
|
||||
|
||||
@@ -122,6 +122,16 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule imp
|
||||
return true;
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void addListener(String eventName) {
|
||||
// Keep: Required for RN built in Event Emitter Calls.
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void removeListeners(Integer count) {
|
||||
// Keep: Required for RN built in Event Emitter Calls.
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Map<String, Object> getConstants() {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
package com.eko;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
Reference in New Issue
Block a user