Skip to content

Commit 1ee66ac

Browse files
authored
fix: account for broken types library and hardcode strings (#18)
1 parent 7d32e45 commit 1ee66ac

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

src/index.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
completeHandler,
77
download,
88
DownloadTask,
9-
DownloadTaskState,
109
} from "react-native-background-downloader";
1110
import RNFS from "react-native-fs";
1211
import uuid from "react-uuid";
@@ -700,30 +699,30 @@ export default class DownloadQueue {
700699
let shouldAddTask = true;
701700

702701
switch (task.state) {
703-
case DownloadTaskState.DOWNLOADING:
702+
case "DOWNLOADING":
704703
// Since we're already downloading, make sure the client at least
705704
// gets a notification that it's started.
706705
this.handlers?.onBegin?.(spec.url, task.totalBytes);
707706
if (!this.active) {
708707
task.pause();
709708
}
710709
break;
711-
case DownloadTaskState.PAUSED:
710+
case "PAUSED":
712711
this.handlers?.onBegin?.(spec.url, task.totalBytes);
713712
if (this.active) {
714713
task.resume(); // Assuming checkForExistingDownloads() hasn't already
715714
}
716715
break;
717-
case DownloadTaskState.DONE:
716+
case "DONE":
718717
this.handlers?.onBegin?.(spec.url, task.totalBytes);
719718
this.handlers?.onDone?.(spec.url, spec.path);
720719
shouldAddTask = false;
721720
break;
722-
case DownloadTaskState.STOPPED:
721+
case "STOPPED":
723722
this.start(spec);
724723
shouldAddTask = false;
725724
break;
726-
case DownloadTaskState.FAILED:
725+
case "FAILED":
727726
default:
728727
this.handlers?.onError?.(
729728
spec.url,
@@ -739,11 +738,7 @@ export default class DownloadQueue {
739738
this.addTask(spec.url, task);
740739
}
741740
} else {
742-
if (
743-
[DownloadTaskState.DOWNLOADING, DownloadTaskState.PAUSED].includes(
744-
task.state
745-
)
746-
) {
741+
if (["DOWNLOADING", "PAUSED"].includes(task.state)) {
747742
task.stop();
748743
}
749744
}

test/index.spec.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ import {
1313
DoneHandler,
1414
download,
1515
DownloadTask,
16-
DownloadTaskState,
1716
ErrorHandler,
18-
ProgressHandler
17+
ProgressHandler,
1918
} from "react-native-background-downloader";
2019
import RNFS, { exists, readdir, stat, unlink } from "react-native-fs";
2120
import DownloadQueue, { DownloadQueueHandlers } from "../src";
@@ -270,7 +269,7 @@ describe("DownloadQueue", () => {
270269
onBegin: jest.fn(),
271270
};
272271

273-
task.state = "DOWNLOADING" as DownloadTaskState.DOWNLOADING;
272+
task.state = "DOWNLOADING";
274273
task.totalBytes = 8675309;
275274
(checkForExistingDownloads as jest.Mock).mockReturnValue([task]);
276275

@@ -297,7 +296,7 @@ describe("DownloadQueue", () => {
297296
onBegin: jest.fn(),
298297
};
299298

300-
task.state = "PAUSED" as DownloadTaskState.PAUSED;
299+
task.state = "PAUSED";
301300
task.totalBytes = 8675309;
302301
(checkForExistingDownloads as jest.Mock).mockReturnValue([task]);
303302

@@ -325,7 +324,7 @@ describe("DownloadQueue", () => {
325324
onDone: jest.fn(),
326325
};
327326

328-
task.state = "DONE" as DownloadTaskState.DONE;
327+
task.state = "DONE";
329328
task.totalBytes = 8675309;
330329
(checkForExistingDownloads as jest.Mock).mockReturnValue([task]);
331330

@@ -343,7 +342,10 @@ describe("DownloadQueue", () => {
343342
"http://foo.com",
344343
task.totalBytes
345344
);
346-
expect(handlers.onDone).toHaveBeenCalledWith("http://foo.com", `${RNFS.DocumentDirectoryPath}/DownloadQueue/mydomain/foo`);
345+
expect(handlers.onDone).toHaveBeenCalledWith(
346+
"http://foo.com",
347+
`${RNFS.DocumentDirectoryPath}/DownloadQueue/mydomain/foo`
348+
);
347349
expect(download).not.toHaveBeenCalled();
348350
});
349351

@@ -354,7 +356,7 @@ describe("DownloadQueue", () => {
354356
onDone: jest.fn(),
355357
};
356358

357-
task.state = "STOPPED" as DownloadTaskState.STOPPED;
359+
task.state = "STOPPED";
358360
(checkForExistingDownloads as jest.Mock).mockReturnValue([task]);
359361

360362
await kvfs.write("/mydomain/foo", {
@@ -377,7 +379,7 @@ describe("DownloadQueue", () => {
377379
onError: jest.fn(),
378380
};
379381

380-
task.state = "FAILED" as DownloadTaskState.FAILED;
382+
task.state = "FAILED";
381383
(checkForExistingDownloads as jest.Mock).mockReturnValue([task]);
382384

383385
await kvfs.write("/mydomain/foo", {
@@ -389,7 +391,7 @@ describe("DownloadQueue", () => {
389391

390392
expect(jest.getTimerCount()).toEqual(0);
391393
await queue.init({ domain: "mydomain", handlers });
392-
expect(jest.getTimerCount()).toEqual(1); // error retry timer
394+
expect(jest.getTimerCount()).toEqual(1); // error retry timer
393395

394396
await advanceThroughNextTimersAndPromises();
395397
expect(download).toHaveBeenCalledTimes(1);
@@ -407,15 +409,14 @@ describe("DownloadQueue", () => {
407409
expect(task.stop).not.toHaveBeenCalled();
408410

409411
queue.terminate();
410-
task.state = "PAUSED" as DownloadTaskState.PAUSED;
412+
task.state = "PAUSED";
411413
await queue.init({ domain: "mydomain" });
412414
expect(task.stop).toHaveBeenCalledTimes(1);
413415

414416
queue.terminate();
415-
task.state = "DOWNLOADING" as DownloadTaskState.DOWNLOADING;
417+
task.state = "DOWNLOADING";
416418
await queue.init({ domain: "mydomain" });
417419
expect(task.stop).toHaveBeenCalledTimes(2);
418-
419420
});
420421

421422
it("starts downloads for specs without tasks or files", async () => {
@@ -442,7 +443,7 @@ describe("DownloadQueue", () => {
442443
it("should stop all active tasks", async () => {
443444
const queue = new DownloadQueue();
444445

445-
task.state = "DOWNLOADING" as DownloadTaskState.DOWNLOADING;
446+
task.state = "DOWNLOADING";
446447
(checkForExistingDownloads as jest.Mock).mockReturnValue([task]);
447448

448449
await kvfs.write("/mydomain/foo", {
@@ -467,7 +468,7 @@ describe("DownloadQueue", () => {
467468
onBegin: jest.fn(),
468469
};
469470

470-
task.state = "DOWNLOADING" as DownloadTaskState.DOWNLOADING;
471+
task.state = "DOWNLOADING";
471472
(checkForExistingDownloads as jest.Mock).mockReturnValue([task]);
472473

473474
await kvfs.write("/mydomain/foo", {
@@ -540,10 +541,10 @@ describe("DownloadQueue", () => {
540541
(checkForExistingDownloads as jest.Mock).mockReturnValue([
541542
Object.assign(task, {
542543
id: assignedId,
543-
})
544+
}),
544545
]);
545546

546-
task.state = "PAUSED" as DownloadTaskState.PAUSED;
547+
task.state = "PAUSED";
547548

548549
// Pretend app got launched again by using another queue
549550
const relaunchQueue = new DownloadQueue();
@@ -1050,7 +1051,7 @@ describe("DownloadQueue", () => {
10501051
await queue.addUrl("http://foo.com");
10511052

10521053
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1053-
task._begin!({expectedBytes: 42, headers: {}});
1054+
task._begin!({ expectedBytes: 42, headers: {} });
10541055
expect(handlers.onBegin).toHaveBeenCalledTimes(1);
10551056

10561057
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -1071,7 +1072,7 @@ describe("DownloadQueue", () => {
10711072
it("should start paused if requested on background downloading task", async () => {
10721073
const queue = new DownloadQueue();
10731074

1074-
task.state = "DOWNLOADING" as DownloadTaskState.DOWNLOADING;
1075+
task.state = "DOWNLOADING";
10751076
(checkForExistingDownloads as jest.Mock).mockReturnValue([task]);
10761077

10771078
await kvfs.write("/mydomain/foo", {
@@ -1094,7 +1095,7 @@ describe("DownloadQueue", () => {
10941095
it("should start paused if requested on paused background task", async () => {
10951096
const queue = new DownloadQueue();
10961097

1097-
task.state = "PAUSED" as DownloadTaskState.PAUSED;
1098+
task.state = "PAUSED";
10981099
(checkForExistingDownloads as jest.Mock).mockReturnValue([task]);
10991100

11001101
await kvfs.write("/mydomain/foo", {

0 commit comments

Comments
 (0)