@@ -825,6 +825,54 @@ describe("DownloadQueue", () => {
825
825
expect ( task . resume ) . toHaveBeenCalledTimes ( 1 ) ;
826
826
} ) ;
827
827
828
+ it ( "should pause revived url when relaunched inactive" , async ( ) => {
829
+ const queue = new DownloadQueue ( ) ;
830
+ let assignedId = "tbd" ;
831
+ let progresser : ProgressHandler ;
832
+
833
+ ( download as jest . Mock ) . mockImplementation ( ( spec : { id : string } ) => {
834
+ assignedId = spec . id ;
835
+ return task ;
836
+ } ) ;
837
+ ( ensureDownloadsAreRunning as jest . Mock ) . mockImplementation ( ( ) => {
838
+ // This is exactly what the actual implementation does, to work around
839
+ // some bug in the library.
840
+ task . pause ( ) ;
841
+ task . resume ( ) ;
842
+ } ) ;
843
+
844
+ await queue . init ( { domain : "mydomain" } ) ;
845
+ await queue . addUrl ( "http://foo.com/a.mp3" ) ;
846
+
847
+ expect ( download ) . toHaveBeenCalledTimes ( 1 ) ;
848
+ expect ( task . resume ) . not . toHaveBeenCalled ( ) ;
849
+
850
+ ( checkForExistingDownloads as jest . Mock ) . mockReturnValue ( [
851
+ Object . assign ( task , {
852
+ id : assignedId ,
853
+ progress : ( handler : ProgressHandler ) => {
854
+ progresser = handler ;
855
+ return task ;
856
+ }
857
+ } ) ,
858
+ ] ) ;
859
+
860
+ task . state = "PAUSED" ;
861
+
862
+ // Pretend app got launched again by using another queue
863
+ const relaunchQueue = new DownloadQueue ( ) ;
864
+
865
+ await relaunchQueue . init ( { domain : "mydomain" , startActive : false } ) ;
866
+ expect ( task . resume ) . toHaveBeenCalledTimes ( 1 ) ;
867
+
868
+ // We now call the progress() handler because we inserted a workaround for
869
+ // https://github.com/kesha-antonov/react-native-background-downloader/issues/23
870
+ // Revived tasks get progress() without begin(), so we also need to pause
871
+ // those cases.
872
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
873
+ progresser ! ( 42 , 420 , 8675309 ) ;
874
+ } ) ;
875
+
828
876
it ( "shouldn't re-download revived spec if file already downloaded" , async ( ) => {
829
877
const queue = new DownloadQueue ( ) ;
830
878
let doner : DoneHandler | undefined ;
@@ -1376,6 +1424,7 @@ describe("DownloadQueue", () => {
1376
1424
describe ( "Pause / resume" , ( ) => {
1377
1425
it ( "should start paused if requested on background downloading task" , async ( ) => {
1378
1426
const queue = new DownloadQueue ( ) ;
1427
+ let beginner : BeginHandler ;
1379
1428
1380
1429
task . state = "DOWNLOADING" ;
1381
1430
( checkForExistingDownloads as jest . Mock ) . mockReturnValue ( [ task ] ) ;
@@ -1390,18 +1439,35 @@ describe("DownloadQueue", () => {
1390
1439
expect ( task . resume ) . not . toHaveBeenCalled ( ) ;
1391
1440
// We expect pause TWO times because, according to downloader docs, we
1392
1441
// should explicitly pause before reattaching handlers; we then pause
1393
- // gain because we specified !startActive.
1442
+ // again because we specified !startActive.
1394
1443
expect ( task . pause ) . toHaveBeenCalledTimes ( 2 ) ;
1395
1444
expect ( download ) . not . toHaveBeenCalled ( ) ;
1396
1445
1397
- ( download as jest . Mock ) . mockReturnValue ( task ) ;
1446
+ ( download as jest . Mock ) . mockImplementation (
1447
+ ( spec : { id : string } ) : TaskWithHandlers => {
1448
+ return Object . assign ( task , {
1449
+ id : spec . id ,
1450
+ begin : ( handler : BeginHandler ) => {
1451
+ beginner = handler ;
1452
+ return task ;
1453
+ } ,
1454
+ } ) ;
1455
+ }
1456
+ ) ;
1398
1457
1399
1458
await queue . addUrl ( "http://boo.com/a.mp3" ) ;
1459
+
1460
+ // We now call the begin() handler because we inserted a workaround for
1461
+ // https://github.com/kesha-antonov/react-native-background-downloader/issues/23
1462
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1463
+ beginner ! ( { expectedBytes : 42 , headers : { } } ) ;
1464
+
1400
1465
expect ( task . pause ) . toHaveBeenCalledTimes ( 3 ) ; // for the added download
1401
1466
} ) ;
1402
1467
1403
1468
it ( "should start paused if requested on paused background task" , async ( ) => {
1404
1469
const queue = new DownloadQueue ( ) ;
1470
+ let beginner : BeginHandler ;
1405
1471
1406
1472
task . state = "PAUSED" ;
1407
1473
( checkForExistingDownloads as jest . Mock ) . mockReturnValue ( [ task ] ) ;
@@ -1417,9 +1483,25 @@ describe("DownloadQueue", () => {
1417
1483
expect ( task . pause ) . toHaveBeenCalledTimes ( 1 ) ;
1418
1484
expect ( download ) . not . toHaveBeenCalled ( ) ;
1419
1485
1420
- ( download as jest . Mock ) . mockReturnValue ( task ) ;
1486
+ ( download as jest . Mock ) . mockImplementation (
1487
+ ( spec : { id : string } ) : TaskWithHandlers => {
1488
+ return Object . assign ( task , {
1489
+ id : spec . id ,
1490
+ begin : ( handler : BeginHandler ) => {
1491
+ beginner = handler ;
1492
+ return task ;
1493
+ } ,
1494
+ } ) ;
1495
+ }
1496
+ ) ;
1421
1497
1422
1498
await queue . addUrl ( "http://boo.com/a.mp3" ) ;
1499
+
1500
+ // We now call the begin() handler because we inserted a workaround for
1501
+ // https://github.com/kesha-antonov/react-native-background-downloader/issues/23
1502
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1503
+ beginner ! ( { expectedBytes : 42 , headers : { } } ) ;
1504
+
1423
1505
expect ( task . pause ) . toHaveBeenCalledTimes ( 2 ) ; // for the added download
1424
1506
} ) ;
1425
1507
@@ -1568,13 +1650,32 @@ describe("DownloadQueue", () => {
1568
1650
1569
1651
it ( "should respect the user's !startActive during network change" , async ( ) => {
1570
1652
const queue = new DownloadQueue ( ) ;
1653
+ let beginner : BeginHandler ;
1654
+
1571
1655
await queue . init ( {
1572
1656
domain : "mydomain" ,
1573
1657
netInfoAddEventListener : addEventListener ,
1574
1658
netInfoFetchState : fetch ,
1575
1659
startActive : false ,
1576
1660
} ) ;
1661
+
1662
+ ( download as jest . Mock ) . mockImplementation (
1663
+ ( spec : { id : string } ) : TaskWithHandlers => {
1664
+ return Object . assign ( task , {
1665
+ id : spec . id ,
1666
+ begin : ( handler : BeginHandler ) => {
1667
+ beginner = handler ;
1668
+ return task ;
1669
+ } ,
1670
+ } ) ;
1671
+ }
1672
+ ) ;
1673
+
1577
1674
await queue . addUrl ( "http://foo.com/a.mp3" ) ;
1675
+ // We now call the begin() handler because we inserted a workaround for
1676
+ // https://github.com/kesha-antonov/react-native-background-downloader/issues/23
1677
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1678
+ beginner ! ( { expectedBytes : 42 , headers : { } } ) ;
1578
1679
expect ( task . pause ) . toHaveBeenCalledTimes ( 1 ) ;
1579
1680
1580
1681
netInfoHandler ( createNetState ( false ) ) ;
0 commit comments