@@ -6,14 +6,14 @@ import {
66 DownloadTask ,
77 ensureDownloadsAreRunning ,
88 ErrorHandler ,
9- ProgressHandler
9+ ProgressHandler ,
1010} from "@kesha-antonov/react-native-background-downloader" ;
1111import AsyncStorage from "@react-native-async-storage/async-storage" ;
1212import {
1313 addEventListener ,
1414 fetch ,
1515 NetInfoState ,
16- NetInfoStateType
16+ NetInfoStateType ,
1717} from "@react-native-community/netinfo" ;
1818import { mock } from "jest-mock-extended" ;
1919import KVFS from "key-value-file-system" ;
@@ -309,6 +309,44 @@ describe("DownloadQueue", () => {
309309 expect ( unlink ) . toHaveBeenCalledTimes ( 1 ) ;
310310 } ) ;
311311
312+ it ( "removes duplicate specs with the same URLs" , async ( ) => {
313+ const queue = new DownloadQueue ( ) ;
314+
315+ ( readdir as jest . Mock ) . mockImplementation ( ( ) => [
316+ "foo.mp3" ,
317+ "bar.mp3" ,
318+ "foo.mp3" ,
319+ ] ) ;
320+
321+ await kvfs . write ( "/mydomain/foo" , {
322+ id : "foo" ,
323+ url : "http://foo.com/a.mp3" ,
324+ path : `${ RNFS . DocumentDirectoryPath } /DownloadQueue/mydomain/foo.mp3` ,
325+ createTime : Date . now ( ) - 1000 ,
326+ } ) ;
327+ await kvfs . write ( "/mydomain/boo" , {
328+ id : "boo" ,
329+ url : "http://foo.com/b.mp3" ,
330+ path : `${ RNFS . DocumentDirectoryPath } /DownloadQueue/mydomain/boo.mp3` ,
331+ createTime : Date . now ( ) - 1000 ,
332+ } ) ;
333+ await kvfs . write ( "/mydomain/foo2" , {
334+ id : "foo2" ,
335+ url : "http://foo.com/a.mp3" ,
336+ path : `${ RNFS . DocumentDirectoryPath } /DownloadQueue/mydomain/foo.mp3` ,
337+ createTime : Date . now ( ) - 800 ,
338+ } ) ;
339+ expect ( ( await kvfs . readMulti ( "/mydomain/*" ) ) . length ) . toEqual ( 3 ) ;
340+
341+ await queue . init ( { domain : "mydomain" } ) ;
342+ const specs = await kvfs . readMulti ( "/mydomain/*" ) ;
343+
344+ expect ( specs . length ) . toEqual ( 2 ) ;
345+ expect (
346+ specs . some ( spec => "id" in spec && spec . id === "foo2" )
347+ ) . toBeFalsy ( ) ;
348+ } ) ;
349+
312350 it ( "revives still-downloading specs from previous launches" , async ( ) => {
313351 const queue = new DownloadQueue ( ) ;
314352 const handlers : DownloadQueueHandlers = {
@@ -853,7 +891,7 @@ describe("DownloadQueue", () => {
853891 progress : ( handler : ProgressHandler ) => {
854892 progresser = handler ;
855893 return task ;
856- }
894+ } ,
857895 } ) ,
858896 ] ) ;
859897
0 commit comments