Skip to content
This repository was archived by the owner on Feb 27, 2022. It is now read-only.

Commit a7cf5c2

Browse files
author
Elad Gil
committed
unit tests impl.
1 parent 3289391 commit a7cf5c2

File tree

3 files changed

+209
-6
lines changed

3 files changed

+209
-6
lines changed

__mocks__/RNBackgroundDownload.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,67 @@
11
import { NativeModules } from 'react-native';
22

3+
const listeners = {};
4+
5+
// states:
6+
// 0 - Running
7+
// 1 - Suspended / Paused
8+
// 2 - Cancelled / Failed
9+
// 3 - Completed (not necessarily successfully)
10+
311
NativeModules.RNBackgroundDownload = {
412
download: jest.fn(),
5-
addListener: jest.fn()
13+
pauseTask: jest.fn(),
14+
resumeTask: jest.fn(),
15+
stopTask: jest.fn(),
16+
TaskRunning: 0,
17+
TaskSuspended: 1,
18+
TaskCanceling: 2,
19+
TaskCompleted: 3,
20+
checkForExistingDownloads: jest.fn().mockImplementation(() => {
21+
foundDownloads = [
22+
{
23+
id: 'taskRunning',
24+
state: NativeModules.RNBackgroundDownload.TaskRunning,
25+
percent: 0.5,
26+
bytesWritten: 50,
27+
totalBytes: 100
28+
},
29+
{
30+
id: 'taskPaused',
31+
state: NativeModules.RNBackgroundDownload.TaskSuspended,
32+
percent: 0.7,
33+
bytesWritten: 70,
34+
totalBytes: 100
35+
},
36+
{
37+
id: 'taskCancelled',
38+
percent: 0.9,
39+
state: NativeModules.RNBackgroundDownload.TaskCanceling,
40+
bytesWritten: 90,
41+
totalBytes: 100
42+
},
43+
{
44+
id: 'taskCompletedExplicit',
45+
state: NativeModules.RNBackgroundDownload.TaskCompleted,
46+
percent: 1,
47+
bytesWritten: 100,
48+
totalBytes: 100
49+
},
50+
{
51+
id: 'taskCompletedImplicit',
52+
state: NativeModules.RNBackgroundDownload.TaskCompleted,
53+
percent: 1,
54+
bytesWritten: 100,
55+
totalBytes: 100
56+
},
57+
{
58+
id: 'taskFailed',
59+
state: NativeModules.RNBackgroundDownload.TaskCompleted,
60+
percent: 0.9,
61+
bytesWritten: 90,
62+
totalBytes: 100
63+
}
64+
]
65+
return Promise.resolve(foundDownloads);
66+
})
667
};

__tests__/mainTest.js

Lines changed: 143 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,149 @@
1+
jest.mock('NativeEventEmitter', () => {
2+
return class NativeEventEmitter {
3+
static listeners = {};
4+
5+
addListener(channel, cb) {
6+
NativeEventEmitter.listeners[channel] = cb;
7+
}
8+
};
9+
});
10+
111
import RNBackgroundDownload from '../index';
212
import DownloadTask from '../lib/downloadTask';
13+
import { NativeEventEmitter, NativeModules } from 'react-native';
14+
15+
const RNBackgroundDownloadNative = NativeModules.RNBackgroundDownload;
316

4-
test('download should return a downloadTask', () => {
5-
expect(RNBackgroundDownload.download({
17+
let downloadTask;
18+
19+
test('download function', () => {
20+
downloadTask = RNBackgroundDownload.download({
621
id: 'test',
722
url: 'test',
823
destination: 'test'
9-
})).toBeInstanceOf(DownloadTask);
10-
});
24+
});
25+
expect(downloadTask).toBeInstanceOf(DownloadTask);
26+
expect(RNBackgroundDownloadNative.download).toHaveBeenCalled();
27+
});
28+
29+
test('begin event', () => {
30+
return new Promise(resolve => {
31+
const beginDT = RNBackgroundDownload.download({
32+
id: 'testBegin',
33+
url: 'test',
34+
destination: 'test'
35+
}).begin((expectedBytes) => {
36+
expect(expectedBytes).toBe(9001);
37+
expect(beginDT.state).toBe('DOWNLOADING');
38+
resolve();
39+
});
40+
NativeEventEmitter.listeners.downloadBegin({
41+
id: 'testBegin',
42+
expctedBytes: 9001
43+
});
44+
});
45+
});
46+
47+
test('progress event', () => {
48+
return new Promise(resolve => {
49+
RNBackgroundDownload.download({
50+
id: 'testProgress',
51+
url: 'test',
52+
destination: 'test'
53+
}).progress((percent, bytesWritten, totalBytes) => {
54+
expect(percent).toBeCloseTo(0.7);
55+
expect(bytesWritten).toBe(100);
56+
expect(totalBytes).toBe(200);
57+
resolve();
58+
});
59+
NativeEventEmitter.listeners.downloadProgress([{
60+
id: 'testProgress',
61+
percent: 0.7,
62+
written: 100,
63+
total: 200
64+
}]);
65+
});
66+
});
67+
68+
test('done event', () => {
69+
return new Promise(resolve => {
70+
const doneDT = RNBackgroundDownload.download({
71+
id: 'testDone',
72+
url: 'test',
73+
destination: 'test'
74+
}).done(() => {
75+
expect(doneDT.state).toBe('DONE');
76+
resolve();
77+
});
78+
NativeEventEmitter.listeners.downloadComplete({
79+
id: 'testDone'
80+
});
81+
});
82+
});
83+
84+
test('fail event', () => {
85+
return new Promise(resolve => {
86+
const failDT = RNBackgroundDownload.download({
87+
id: 'testFail',
88+
url: 'test',
89+
destination: 'test'
90+
}).error((error) => {
91+
expect(error).toBeInstanceOf(Error);
92+
expect(failDT.state).toBe('FAILED');
93+
resolve();
94+
});
95+
NativeEventEmitter.listeners.downloadFailed({
96+
id: 'testFail',
97+
error: new Error('test')
98+
});
99+
});
100+
});
101+
102+
test('pause', () => {
103+
const pauseDT = RNBackgroundDownload.download({
104+
id: 'testPause',
105+
url: 'test',
106+
destination: 'test'
107+
});
108+
109+
pauseDT.pause();
110+
expect(pauseDT.state).toBe('PAUSED');
111+
expect(RNBackgroundDownloadNative.pauseTask).toHaveBeenCalled();
112+
});
113+
114+
test('resume', () => {
115+
const resumeDT = RNBackgroundDownload.download({
116+
id: 'testResume',
117+
url: 'test',
118+
destination: 'test'
119+
});
120+
121+
resumeDT.resume();
122+
expect(resumeDT.state).toBe('DOWNLOADING');
123+
expect(RNBackgroundDownloadNative.resumeTask).toHaveBeenCalled();
124+
});
125+
126+
test('stop', () => {
127+
const stopDT = RNBackgroundDownload.download({
128+
id: 'testStop',
129+
url: 'test',
130+
destination: 'test'
131+
});
132+
133+
stopDT.stop();
134+
expect(stopDT.state).toBe('STOPPED');
135+
expect(RNBackgroundDownloadNative.stopTask).toHaveBeenCalled();
136+
});
137+
138+
test('checkForExistingDownloads', () => {
139+
return RNBackgroundDownload.checkForExistingDownloads()
140+
.then(foundDownloads => {
141+
expect(RNBackgroundDownloadNative.checkForExistingDownloads).toHaveBeenCalled();
142+
expect(foundDownloads.length).toBe(4);
143+
foundDownloads.forEach(foundDownload => {
144+
expect(foundDownload).toBeInstanceOf(DownloadTask);
145+
expect(foundDownload.state).not.toBe('FAILED');
146+
expect(foundDownload.state).not.toBe('STOPPED');
147+
});
148+
})
149+
})

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
},
2121
"jest": {
2222
"preset": "react-native",
23-
"setupFiles": ["./__mocks__/RNBackgroundDownload.js"]
23+
"setupFiles": [
24+
"./__mocks__/RNBackgroundDownload.js",
25+
"./node_modules/react-native/Libraries/EventEmitter/__mocks__/NativeEventEmitter.js"
26+
]
2427
}
2528
}

0 commit comments

Comments
 (0)