1
+ jest . mock ( 'NativeEventEmitter' , ( ) => {
2
+ return class NativeEventEmitter {
3
+ static listeners = { } ;
4
+
5
+ addListener ( channel , cb ) {
6
+ NativeEventEmitter . listeners [ channel ] = cb ;
7
+ }
8
+ } ;
9
+ } ) ;
10
+
1
11
import RNBackgroundDownload from '../index' ;
2
12
import DownloadTask from '../lib/downloadTask' ;
13
+ import { NativeEventEmitter , NativeModules } from 'react-native' ;
14
+
15
+ const RNBackgroundDownloadNative = NativeModules . RNBackgroundDownload ;
3
16
4
- test ( 'download should return a downloadTask' , ( ) => {
5
- expect ( RNBackgroundDownload . download ( {
17
+ let downloadTask ;
18
+
19
+ test ( 'download function' , ( ) => {
20
+ downloadTask = RNBackgroundDownload . download ( {
6
21
id : 'test' ,
7
22
url : 'test' ,
8
23
destination : 'test'
9
- } ) ) . toBeInstanceOf ( DownloadTask ) ;
10
- } ) ;
24
+ } ) ;
25
+ expect ( downloadTask ) . toBeInstanceOf ( DownloadTask ) ;
26
+ expect ( RNBackgroundDownloadNative . download ) . toHaveBeenCalled ( ) ;
27
+ } ) ;
28
+
29
+ test ( 'begin event' , ( ) => {
30
+ return new Promise ( resolve => {
31
+ const beginDT = RNBackgroundDownload . download ( {
32
+ id : 'testBegin' ,
33
+ url : 'test' ,
34
+ destination : 'test'
35
+ } ) . begin ( ( expectedBytes ) => {
36
+ expect ( expectedBytes ) . toBe ( 9001 ) ;
37
+ expect ( beginDT . state ) . toBe ( 'DOWNLOADING' ) ;
38
+ resolve ( ) ;
39
+ } ) ;
40
+ NativeEventEmitter . listeners . downloadBegin ( {
41
+ id : 'testBegin' ,
42
+ expctedBytes : 9001
43
+ } ) ;
44
+ } ) ;
45
+ } ) ;
46
+
47
+ test ( 'progress event' , ( ) => {
48
+ return new Promise ( resolve => {
49
+ RNBackgroundDownload . download ( {
50
+ id : 'testProgress' ,
51
+ url : 'test' ,
52
+ destination : 'test'
53
+ } ) . progress ( ( percent , bytesWritten , totalBytes ) => {
54
+ expect ( percent ) . toBeCloseTo ( 0.7 ) ;
55
+ expect ( bytesWritten ) . toBe ( 100 ) ;
56
+ expect ( totalBytes ) . toBe ( 200 ) ;
57
+ resolve ( ) ;
58
+ } ) ;
59
+ NativeEventEmitter . listeners . downloadProgress ( [ {
60
+ id : 'testProgress' ,
61
+ percent : 0.7 ,
62
+ written : 100 ,
63
+ total : 200
64
+ } ] ) ;
65
+ } ) ;
66
+ } ) ;
67
+
68
+ test ( 'done event' , ( ) => {
69
+ return new Promise ( resolve => {
70
+ const doneDT = RNBackgroundDownload . download ( {
71
+ id : 'testDone' ,
72
+ url : 'test' ,
73
+ destination : 'test'
74
+ } ) . done ( ( ) => {
75
+ expect ( doneDT . state ) . toBe ( 'DONE' ) ;
76
+ resolve ( ) ;
77
+ } ) ;
78
+ NativeEventEmitter . listeners . downloadComplete ( {
79
+ id : 'testDone'
80
+ } ) ;
81
+ } ) ;
82
+ } ) ;
83
+
84
+ test ( 'fail event' , ( ) => {
85
+ return new Promise ( resolve => {
86
+ const failDT = RNBackgroundDownload . download ( {
87
+ id : 'testFail' ,
88
+ url : 'test' ,
89
+ destination : 'test'
90
+ } ) . error ( ( error ) => {
91
+ expect ( error ) . toBeInstanceOf ( Error ) ;
92
+ expect ( failDT . state ) . toBe ( 'FAILED' ) ;
93
+ resolve ( ) ;
94
+ } ) ;
95
+ NativeEventEmitter . listeners . downloadFailed ( {
96
+ id : 'testFail' ,
97
+ error : new Error ( 'test' )
98
+ } ) ;
99
+ } ) ;
100
+ } ) ;
101
+
102
+ test ( 'pause' , ( ) => {
103
+ const pauseDT = RNBackgroundDownload . download ( {
104
+ id : 'testPause' ,
105
+ url : 'test' ,
106
+ destination : 'test'
107
+ } ) ;
108
+
109
+ pauseDT . pause ( ) ;
110
+ expect ( pauseDT . state ) . toBe ( 'PAUSED' ) ;
111
+ expect ( RNBackgroundDownloadNative . pauseTask ) . toHaveBeenCalled ( ) ;
112
+ } ) ;
113
+
114
+ test ( 'resume' , ( ) => {
115
+ const resumeDT = RNBackgroundDownload . download ( {
116
+ id : 'testResume' ,
117
+ url : 'test' ,
118
+ destination : 'test'
119
+ } ) ;
120
+
121
+ resumeDT . resume ( ) ;
122
+ expect ( resumeDT . state ) . toBe ( 'DOWNLOADING' ) ;
123
+ expect ( RNBackgroundDownloadNative . resumeTask ) . toHaveBeenCalled ( ) ;
124
+ } ) ;
125
+
126
+ test ( 'stop' , ( ) => {
127
+ const stopDT = RNBackgroundDownload . download ( {
128
+ id : 'testStop' ,
129
+ url : 'test' ,
130
+ destination : 'test'
131
+ } ) ;
132
+
133
+ stopDT . stop ( ) ;
134
+ expect ( stopDT . state ) . toBe ( 'STOPPED' ) ;
135
+ expect ( RNBackgroundDownloadNative . stopTask ) . toHaveBeenCalled ( ) ;
136
+ } ) ;
137
+
138
+ test ( 'checkForExistingDownloads' , ( ) => {
139
+ return RNBackgroundDownload . checkForExistingDownloads ( )
140
+ . then ( foundDownloads => {
141
+ expect ( RNBackgroundDownloadNative . checkForExistingDownloads ) . toHaveBeenCalled ( ) ;
142
+ expect ( foundDownloads . length ) . toBe ( 4 ) ;
143
+ foundDownloads . forEach ( foundDownload => {
144
+ expect ( foundDownload ) . toBeInstanceOf ( DownloadTask ) ;
145
+ expect ( foundDownload . state ) . not . toBe ( 'FAILED' ) ;
146
+ expect ( foundDownload . state ) . not . toBe ( 'STOPPED' ) ;
147
+ } ) ;
148
+ } )
149
+ } )
0 commit comments