@@ -315,7 +315,7 @@ describe('FileController', () => {
315
315
} ) ;
316
316
} ) ;
317
317
318
- jest . spyOn ( defaultController , 'saveBase64' ) ;
318
+ const spy2 = jest . spyOn ( defaultController , 'saveBase64' ) ;
319
319
await file . save ( ) ;
320
320
expect ( defaultController . download ) . toHaveBeenCalledTimes ( 1 ) ;
321
321
expect ( defaultController . saveBase64 ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -324,6 +324,25 @@ describe('FileController', () => {
324
324
format : 'base64' , base64 : 'ParseA==' , type : 'image/png'
325
325
} ) ;
326
326
spy . mockRestore ( ) ;
327
+ spy2 . mockRestore ( ) ;
328
+ } ) ;
329
+
330
+ it ( 'save with uri download abort' , async ( ) => {
331
+ const file = new ParseFile ( 'parse.png' , { uri : 'https://example.com/image.png' } ) ;
332
+ const spy = jest . spyOn (
333
+ defaultController ,
334
+ 'download'
335
+ )
336
+ . mockImplementationOnce ( ( ) => {
337
+ return Promise . resolve ( { } ) ;
338
+ } ) ;
339
+
340
+ const spy2 = jest . spyOn ( defaultController , 'saveBase64' ) ;
341
+ await file . save ( ) ;
342
+ expect ( defaultController . download ) . toHaveBeenCalledTimes ( 1 ) ;
343
+ expect ( defaultController . saveBase64 ) . toHaveBeenCalledTimes ( 0 ) ;
344
+ spy . mockRestore ( ) ;
345
+ spy2 . mockRestore ( ) ;
327
346
} ) ;
328
347
329
348
it ( 'download with base64 http' , async ( ) => {
@@ -352,6 +371,32 @@ describe('FileController', () => {
352
371
spy . mockRestore ( ) ;
353
372
} ) ;
354
373
374
+ it ( 'download with base64 http abort' , async ( ) => {
375
+ defaultController . _setXHR ( null ) ;
376
+ const mockRequest = Object . create ( EventEmitter . prototype ) ;
377
+ const mockResponse = Object . create ( EventEmitter . prototype ) ;
378
+ EventEmitter . call ( mockRequest ) ;
379
+ EventEmitter . call ( mockResponse ) ;
380
+ mockResponse . setEncoding = function ( ) { }
381
+ mockResponse . headers = {
382
+ 'content-type' : 'image/png'
383
+ } ;
384
+ const spy = jest . spyOn ( mockHttp , 'get' )
385
+ . mockImplementationOnce ( ( uri , cb ) => {
386
+ cb ( mockResponse ) ;
387
+ return mockRequest ;
388
+ } ) ;
389
+ const options = {
390
+ requestTask : ( ) => { } ,
391
+ } ;
392
+ defaultController . download ( 'http://example.com/image.png' , options ) . then ( ( data ) => {
393
+ expect ( data ) . toEqual ( { } ) ;
394
+ } ) ;
395
+ mockRequest . emit ( 'aborted' ) ;
396
+ mockResponse . emit ( 'end' ) ;
397
+ spy . mockRestore ( ) ;
398
+ } ) ;
399
+
355
400
it ( 'download with base64 https' , async ( ) => {
356
401
defaultController . _setXHR ( null ) ;
357
402
const mockResponse = Object . create ( EventEmitter . prototype ) ;
@@ -381,6 +426,7 @@ describe('FileController', () => {
381
426
it ( 'download with ajax' , async ( ) => {
382
427
const mockXHR = function ( ) {
383
428
return {
429
+ DONE : 4 ,
384
430
open : jest . fn ( ) ,
385
431
send : jest . fn ( ) . mockImplementation ( function ( ) {
386
432
this . response = [ 61 , 170 , 236 , 120 ] ;
@@ -395,12 +441,45 @@ describe('FileController', () => {
395
441
} ;
396
442
} ;
397
443
defaultController . _setXHR ( mockXHR ) ;
398
-
399
- const data = await defaultController . download ( 'https://example.com/image.png' ) ;
444
+ const options = {
445
+ requestTask : ( ) => { } ,
446
+ } ;
447
+ const data = await defaultController . download ( 'https://example.com/image.png' , options ) ;
400
448
expect ( data . base64 ) . toBe ( 'ParseA==' ) ;
401
449
expect ( data . contentType ) . toBe ( 'image/png' ) ;
402
450
} ) ;
403
451
452
+ it ( 'download with ajax abort' , async ( ) => {
453
+ const mockXHR = function ( ) {
454
+ return {
455
+ open : jest . fn ( ) ,
456
+ send : jest . fn ( ) . mockImplementation ( function ( ) {
457
+ this . response = [ 61 , 170 , 236 , 120 ] ;
458
+ this . readyState = 2 ;
459
+ this . onreadystatechange ( ) ;
460
+ } ) ,
461
+ getResponseHeader : function ( ) {
462
+ return 'image/png' ;
463
+ } ,
464
+ abort : function ( ) {
465
+ this . status = 0 ;
466
+ this . response = undefined ;
467
+ this . readyState = 4 ;
468
+ this . onreadystatechange ( )
469
+ }
470
+ } ;
471
+ } ;
472
+ defaultController . _setXHR ( mockXHR ) ;
473
+ let _requestTask ;
474
+ const options = {
475
+ requestTask : ( task ) => _requestTask = task ,
476
+ } ;
477
+ defaultController . download ( 'https://example.com/image.png' , options ) . then ( ( data ) => {
478
+ expect ( data ) . toEqual ( { } ) ;
479
+ } ) ;
480
+ _requestTask . abort ( ) ;
481
+ } ) ;
482
+
404
483
it ( 'download with ajax error' , async ( ) => {
405
484
const mockXHR = function ( ) {
406
485
return {
@@ -411,9 +490,11 @@ describe('FileController', () => {
411
490
} ;
412
491
} ;
413
492
defaultController . _setXHR ( mockXHR ) ;
414
-
493
+ const options = {
494
+ requestTask : ( ) => { } ,
495
+ } ;
415
496
try {
416
- await defaultController . download ( 'https://example.com/image.png' ) ;
497
+ await defaultController . download ( 'https://example.com/image.png' , options ) ;
417
498
} catch ( e ) {
418
499
expect ( e ) . toBe ( 'error thrown' ) ;
419
500
}
@@ -453,7 +534,8 @@ describe('FileController', () => {
453
534
defaultController ,
454
535
'download'
455
536
)
456
- . mockImplementationOnce ( ( ) => {
537
+ . mockImplementationOnce ( ( uri , options ) => {
538
+ options . requestTask ( null ) ;
457
539
return Promise . resolve ( {
458
540
base64 : 'ParseA==' ,
459
541
contentType : 'image/png' ,
0 commit comments