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

Commit 318a54e

Browse files
author
Elad Gil
committed
valitate that handlers are indeed functions and added a test to test it
1 parent c206eef commit 318a54e

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

__mocks__/RNBackgroundDownload.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { NativeModules } from 'react-native';
1+
/* eslint-disable */
22

3-
const listeners = {};
3+
import { NativeModules } from 'react-native';
44

55
// states:
66
// 0 - Running

__tests__/mainTest.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable */
2+
13
jest.mock('NativeEventEmitter', () => {
24
return class NativeEventEmitter {
35
static listeners = {};
@@ -146,4 +148,28 @@ test('checkForExistingDownloads', () => {
146148
expect(foundDownload.state).not.toBe('STOPPED');
147149
});
148150
})
149-
})
151+
});
152+
153+
test('wrong handler type', () => {
154+
let dt = RNBackgroundDownload.download({
155+
id: 'test22222',
156+
url: 'test',
157+
destination: 'test'
158+
});
159+
160+
expect(() => {
161+
dt.begin('not function');
162+
}).toThrow('handler must be a function');
163+
164+
expect(() => {
165+
dt.progress(7);
166+
}).toThrow('handler must be a function');
167+
168+
expect(() => {
169+
dt.done({iamnota: 'function'});
170+
}).toThrow('handler must be a function');
171+
172+
expect(() => {
173+
dt.error('not function');
174+
}).toThrow('handler must be a function');
175+
});

lib/downloadTask.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,33 @@ export default class DownloadTask {
1919
}
2020

2121
begin(handler) {
22+
if (!(typeof handler === 'function')) {
23+
throw new TypeError('handler must be a function');
24+
}
2225
this._beginHandler = handler;
2326
return this;
2427
}
2528

2629
progress(handler) {
30+
if (!(typeof handler === 'function')) {
31+
throw new TypeError('handler must be a function');
32+
}
2733
this._progressHandler = handler;
2834
return this;
2935
}
3036

3137
done(handler) {
38+
if (!(typeof handler === 'function')) {
39+
throw new TypeError('handler must be a function');
40+
}
3241
this._doneHandler = handler;
3342
return this;
3443
}
3544

3645
error(handler) {
46+
if (!(typeof handler === 'function')) {
47+
throw new TypeError('handler must be a function');
48+
}
3749
this._errorHandler = handler;
3850
return this;
3951
}

0 commit comments

Comments
 (0)