From c007f230f3283ddf11a2d51af35c38492269e876 Mon Sep 17 00:00:00 2001 From: Kesha Antonov Date: Mon, 30 Jan 2023 00:23:11 +0300 Subject: [PATCH] fix tests. android: add addListener, removeListeners to fix warnings --- __mocks__/RNBackgroundDownloader.js | 2 + __tests__/mainTest.js | 161 ++++++++---------- .../com/eko/RNBackgroundDownloaderModule.java | 10 ++ .../eko/RNBackgroundDownloaderPackage.java | 1 - 4 files changed, 87 insertions(+), 87 deletions(-) diff --git a/__mocks__/RNBackgroundDownloader.js b/__mocks__/RNBackgroundDownloader.js index 04690f7..2159fb7 100644 --- a/__mocks__/RNBackgroundDownloader.js +++ b/__mocks__/RNBackgroundDownloader.js @@ -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(), diff --git a/__tests__/mainTest.js b/__tests__/mainTest.js index 0b00ff7..ec7b091 100644 --- a/__tests__/mainTest.js +++ b/__tests__/mainTest.js @@ -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() +}) diff --git a/android/src/main/java/com/eko/RNBackgroundDownloaderModule.java b/android/src/main/java/com/eko/RNBackgroundDownloaderModule.java index ea0d068..d772c86 100644 --- a/android/src/main/java/com/eko/RNBackgroundDownloaderModule.java +++ b/android/src/main/java/com/eko/RNBackgroundDownloaderModule.java @@ -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 getConstants() { diff --git a/android/src/main/java/com/eko/RNBackgroundDownloaderPackage.java b/android/src/main/java/com/eko/RNBackgroundDownloaderPackage.java index f815dc5..642cd7b 100644 --- a/android/src/main/java/com/eko/RNBackgroundDownloaderPackage.java +++ b/android/src/main/java/com/eko/RNBackgroundDownloaderPackage.java @@ -1,4 +1,3 @@ - package com.eko; import java.util.Arrays;