@@ -164,4 +164,42 @@ describe('MongoBinaryDownload', () => {
164164 const path = await du . getPath ( ) ;
165165 expect ( path ) . toEqual ( `${ downloadDir } /mongod-x64-ubuntu-4.0.20` ) ;
166166 } ) ;
167+
168+ it ( 'should print the download progress (printDownloadProgress)' , ( ) => {
169+ const version = '4.0.20' ;
170+ process . stdout . isTTY = false ;
171+ jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => void 0 ) ;
172+ jest . spyOn ( process . stdout , 'write' ) . mockImplementation ( ( ) => true ) ;
173+
174+ const du = new MongoBinaryDownload ( {
175+ downloadDir : '/' ,
176+ checkMD5 : false ,
177+ version,
178+ platform : 'linux' ,
179+ } ) ;
180+ expect ( du . dlProgress . current ) . toBe ( 0 ) ;
181+ du . dlProgress . length = 5242880 ;
182+ du . dlProgress . totalMb = Math . round ( ( du . dlProgress . length / 1048576 ) * 10 ) / 10 ;
183+
184+ du . printDownloadProgress ( { length : 1048576 } ) ;
185+ expect ( console . log ) . toHaveBeenCalledWith (
186+ `Downloading MongoDB "${ version } ": 20% (1mb / ${ du . dlProgress . totalMb } mb)\r`
187+ ) ;
188+ expect ( console . log ) . toBeCalledTimes ( 1 ) ;
189+ expect ( process . stdout . write ) . not . toHaveBeenCalled ( ) ;
190+
191+ du . printDownloadProgress ( { length : 10 } ) ;
192+ expect ( console . log ) . toHaveBeenCalledTimes ( 1 ) ;
193+ expect ( process . stdout . write ) . not . toHaveBeenCalled ( ) ;
194+ expect ( du . dlProgress . current ) . toBe ( 1048576 + 10 ) ;
195+
196+ process . stdout . isTTY = true ;
197+ du . printDownloadProgress ( { length : 0 } , true ) ;
198+ expect ( console . log ) . toHaveBeenCalledTimes ( 1 ) ;
199+ expect ( process . stdout . write ) . toHaveBeenCalledWith (
200+ // its still "20" and "1" because of rounding
201+ `Downloading MongoDB "${ version } ": 20% (1mb / ${ du . dlProgress . totalMb } mb)\r`
202+ ) ;
203+ expect ( process . stdout . write ) . toHaveBeenCalledTimes ( 1 ) ;
204+ } ) ;
167205} ) ;
0 commit comments