@@ -16,7 +16,7 @@ import {
16
16
ErrorHandler ,
17
17
ProgressHandler ,
18
18
} from "react-native-background-downloader" ;
19
- import RNFS , { exists , readdir , unlink } from "react-native-fs" ;
19
+ import RNFS , { exists , readdir , stat , unlink } from "react-native-fs" ;
20
20
import DownloadQueue , { DownloadQueueHandlers } from "../src" ;
21
21
22
22
jest . mock ( "@react-native-async-storage/async-storage" , ( ) => {
@@ -170,6 +170,7 @@ describe("DownloadQueue", () => {
170
170
( exists as jest . Mock ) . mockReturnValue ( false ) ;
171
171
( readdir as jest . Mock ) . mockReturnValue ( [ ] ) ;
172
172
( unlink as jest . Mock ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
173
+ ( stat as jest . Mock ) . mockReturnValue ( { size : 8675309 } ) ;
173
174
174
175
( download as jest . Mock ) . mockReturnValue ( task ) ;
175
176
( checkForExistingDownloads as jest . Mock ) . mockReturnValue ( [ ] ) ;
@@ -873,6 +874,47 @@ describe("DownloadQueue", () => {
873
874
await queue . addUrl ( "http://foo.com" ) ;
874
875
expect ( download ) . toHaveBeenCalledTimes ( 2 ) ;
875
876
} ) ;
877
+
878
+ it ( "should send notifications for revived lazy-deletions" , async ( ) => {
879
+ const queue = new DownloadQueue ( ) ;
880
+ const handlers : DownloadQueueHandlers = {
881
+ onBegin : jest . fn ( ) ,
882
+ onDone : jest . fn ( ) ,
883
+ } ;
884
+
885
+ Object . assign ( task , {
886
+ id : "foo" ,
887
+ begin : jest . fn ( handler => {
888
+ task . _begin = handler ;
889
+ return task ;
890
+ } ) ,
891
+ done : jest . fn ( handler => {
892
+ task . _done = handler ;
893
+ return task ;
894
+ } ) ,
895
+ } ) ;
896
+
897
+ ( exists as jest . Mock ) . mockResolvedValue ( true ) ;
898
+
899
+ await queue . init ( { domain : "mydomain" , handlers } ) ;
900
+ await queue . addUrl ( "http://foo.com" ) ;
901
+
902
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
903
+ task . _begin ! ( "http://foo.com" ) ;
904
+ expect ( handlers . onBegin ) . toHaveBeenCalledTimes ( 1 ) ;
905
+
906
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
907
+ await task . _done ! ( ) ;
908
+ expect ( handlers . onDone ) . toHaveBeenCalledTimes ( 1 ) ;
909
+
910
+ await queue . removeUrl ( "http://foo.com" , 0 ) ;
911
+ await queue . addUrl ( "http://foo.com" ) ;
912
+
913
+ // When we re-add a lazy-deleted download that was already downloaded,
914
+ // we expect to get notifications for begin/done again.
915
+ expect ( handlers . onBegin ) . toHaveBeenCalledTimes ( 2 ) ;
916
+ expect ( handlers . onDone ) . toHaveBeenCalledTimes ( 2 ) ;
917
+ } ) ;
876
918
} ) ;
877
919
878
920
describe ( "Pause / resume" , ( ) => {
@@ -1057,8 +1099,6 @@ describe("DownloadQueue", () => {
1057
1099
expect ( task . pause ) . toHaveBeenCalledTimes ( 1 ) ; // no change!
1058
1100
} ) ;
1059
1101
1060
-
1061
-
1062
1102
it ( "should unsubscribe when terminated" , async ( ) => {
1063
1103
const queue = new DownloadQueue ( ) ;
1064
1104
const unsubscriber = jest . fn ( ) ;
@@ -1426,27 +1466,23 @@ describe("DownloadQueue", () => {
1426
1466
onError : jest . fn ( ) ,
1427
1467
} ;
1428
1468
const queue = new DownloadQueue ( ) ;
1429
- let beginner : BeginHandler ;
1430
- let progresser : ProgressHandler ;
1431
- let doner : DoneHandler ;
1432
- let errorer : ErrorHandler ;
1433
1469
1434
1470
Object . assign ( task , {
1435
1471
id : "foo" ,
1436
1472
begin : jest . fn ( handler => {
1437
- beginner = handler ;
1473
+ task . _begin = handler ;
1438
1474
return task ;
1439
1475
} ) ,
1440
1476
progress : jest . fn ( handler => {
1441
- progresser = handler ;
1477
+ task . _progress = handler ;
1442
1478
return task ;
1443
1479
} ) ,
1444
1480
done : jest . fn ( handler => {
1445
- doner = handler ;
1481
+ task . _done = handler ;
1446
1482
return task ;
1447
1483
} ) ,
1448
1484
error : jest . fn ( handler => {
1449
- errorer = handler ;
1485
+ task . _error = handler ;
1450
1486
return task ;
1451
1487
} ) ,
1452
1488
} ) ;
@@ -1455,13 +1491,13 @@ describe("DownloadQueue", () => {
1455
1491
await queue . addUrl ( "http://foo.com" ) ;
1456
1492
1457
1493
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1458
- beginner ! ( { expectedBytes : 300 , headers : { } } ) ;
1494
+ task . _begin ! ( { expectedBytes : 300 , headers : { } } ) ;
1459
1495
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1460
- progresser ! ( 0.5 , 500 , 1000 ) ;
1496
+ task . _progress ! ( 0.5 , 500 , 1000 ) ;
1461
1497
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1462
- await doner ! ( ) ;
1498
+ await task . _done ! ( ) ;
1463
1499
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1464
- errorer ! ( "foo" , 500 ) ;
1500
+ task . _error ! ( "foo" , 500 ) ;
1465
1501
1466
1502
expect ( handlers . onBegin ) . toHaveBeenCalledTimes ( 1 ) ;
1467
1503
expect ( handlers . onProgress ) . toHaveBeenCalledTimes ( 1 ) ;
0 commit comments