@@ -9,10 +9,7 @@ const b64Digit = require('../ParseFile').b64Digit;
9
9
10
10
const ParseObject = require ( '../ParseObject' ) . default ;
11
11
const CoreManager = require ( '../CoreManager' ) . default ;
12
- const EventEmitter = require ( '../EventEmitter' ) . default ;
13
-
14
- const mockHttp = require ( 'http' ) ;
15
- const mockHttps = require ( 'https' ) ;
12
+ const mockFetch = require ( './test_helpers/mockFetch' ) ;
16
13
17
14
const mockLocalDatastore = {
18
15
_updateLocalIdForObject : jest . fn ( ( _localId , /** @type {ParseObject }*/ object ) => {
@@ -491,189 +488,52 @@ describe('FileController', () => {
491
488
spy2 . mockRestore ( ) ;
492
489
} ) ;
493
490
494
- it ( 'download with base64 http' , async ( ) => {
495
- defaultController . _setXHR ( null ) ;
496
- const mockResponse = Object . create ( EventEmitter . prototype ) ;
497
- EventEmitter . call ( mockResponse ) ;
498
- mockResponse . setEncoding = function ( ) { } ;
499
- mockResponse . headers = {
500
- 'content-type' : 'image/png' ,
501
- } ;
502
- const spy = jest . spyOn ( mockHttp , 'get' ) . mockImplementationOnce ( ( uri , cb ) => {
503
- cb ( mockResponse ) ;
504
- mockResponse . emit ( 'data' , 'base64String' ) ;
505
- mockResponse . emit ( 'end' ) ;
506
- return {
507
- on : function ( ) { } ,
508
- } ;
509
- } ) ;
510
-
511
- const data = await defaultController . download ( 'http://example.com/image.png' ) ;
512
- expect ( data . base64 ) . toBe ( 'base64String' ) ;
513
- expect ( data . contentType ) . toBe ( 'image/png' ) ;
514
- expect ( mockHttp . get ) . toHaveBeenCalledTimes ( 1 ) ;
515
- expect ( mockHttps . get ) . toHaveBeenCalledTimes ( 0 ) ;
516
- spy . mockRestore ( ) ;
517
- } ) ;
518
-
519
- it ( 'download with base64 http abort' , async ( ) => {
520
- defaultController . _setXHR ( null ) ;
521
- const mockRequest = Object . create ( EventEmitter . prototype ) ;
522
- const mockResponse = Object . create ( EventEmitter . prototype ) ;
523
- EventEmitter . call ( mockRequest ) ;
524
- EventEmitter . call ( mockResponse ) ;
525
- mockResponse . setEncoding = function ( ) { } ;
526
- mockResponse . headers = {
527
- 'content-type' : 'image/png' ,
528
- } ;
529
- const spy = jest . spyOn ( mockHttp , 'get' ) . mockImplementationOnce ( ( uri , cb ) => {
530
- cb ( mockResponse ) ;
531
- return mockRequest ;
532
- } ) ;
533
- const options = {
534
- requestTask : ( ) => { } ,
535
- } ;
536
- defaultController . download ( 'http://example.com/image.png' , options ) . then ( data => {
537
- expect ( data ) . toEqual ( { } ) ;
538
- } ) ;
539
- mockRequest . emit ( 'abort' ) ;
540
- spy . mockRestore ( ) ;
541
- } ) ;
542
-
543
- it ( 'download with base64 https' , async ( ) => {
544
- defaultController . _setXHR ( null ) ;
545
- const mockResponse = Object . create ( EventEmitter . prototype ) ;
546
- EventEmitter . call ( mockResponse ) ;
547
- mockResponse . setEncoding = function ( ) { } ;
548
- mockResponse . headers = {
549
- 'content-type' : 'image/png' ,
550
- } ;
551
- const spy = jest . spyOn ( mockHttps , 'get' ) . mockImplementationOnce ( ( uri , cb ) => {
552
- cb ( mockResponse ) ;
553
- mockResponse . emit ( 'data' , 'base64String' ) ;
554
- mockResponse . emit ( 'end' ) ;
555
- return {
556
- on : function ( ) { } ,
557
- } ;
558
- } ) ;
559
-
560
- const data = await defaultController . download ( 'https://example.com/image.png' ) ;
561
- expect ( data . base64 ) . toBe ( 'base64String' ) ;
562
- expect ( data . contentType ) . toBe ( 'image/png' ) ;
563
- expect ( mockHttp . get ) . toHaveBeenCalledTimes ( 0 ) ;
564
- expect ( mockHttps . get ) . toHaveBeenCalledTimes ( 1 ) ;
565
- spy . mockRestore ( ) ;
566
- } ) ;
567
-
568
491
it ( 'download with ajax' , async ( ) => {
569
- const mockXHR = function ( ) {
570
- return {
571
- DONE : 4 ,
572
- open : jest . fn ( ) ,
573
- send : jest . fn ( ) . mockImplementation ( function ( ) {
574
- this . response = [ 61 , 170 , 236 , 120 ] ;
575
- this . readyState = 2 ;
576
- this . onreadystatechange ( ) ;
577
- this . readyState = 4 ;
578
- this . onreadystatechange ( ) ;
579
- } ) ,
580
- getResponseHeader : function ( ) {
581
- return 'image/png' ;
582
- } ,
583
- } ;
584
- } ;
585
- defaultController . _setXHR ( mockXHR ) ;
492
+ const response = 'hello' ;
493
+ mockFetch ( [ { status : 200 , response } ] , { 'Content-Length' : 64 , 'Content-Type' : 'image/png' } ) ;
586
494
const options = {
587
495
requestTask : ( ) => { } ,
588
496
} ;
589
497
const data = await defaultController . download ( 'https://example.com/image.png' , options ) ;
590
- expect ( data . base64 ) . toBe ( 'ParseA==' ) ;
498
+ expect ( data . base64 ) . toBeDefined ( ) ;
591
499
expect ( data . contentType ) . toBe ( 'image/png' ) ;
592
500
} ) ;
593
501
594
502
it ( 'download with ajax no response' , async ( ) => {
595
- const mockXHR = function ( ) {
596
- return {
597
- DONE : 4 ,
598
- open : jest . fn ( ) ,
599
- send : jest . fn ( ) . mockImplementation ( function ( ) {
600
- this . response = undefined ;
601
- this . readyState = 2 ;
602
- this . onreadystatechange ( ) ;
603
- this . readyState = 4 ;
604
- this . onreadystatechange ( ) ;
605
- } ) ,
606
- getResponseHeader : function ( ) {
607
- return 'image/png' ;
608
- } ,
609
- } ;
610
- } ;
611
- defaultController . _setXHR ( mockXHR ) ;
503
+ mockFetch ( [ { status : 200 , response : { } } ] , { 'Content-Length' : 0 } ) ;
612
504
const options = {
613
505
requestTask : ( ) => { } ,
614
506
} ;
615
507
const data = await defaultController . download ( 'https://example.com/image.png' , options ) ;
616
- expect ( data ) . toEqual ( { } ) ;
508
+ expect ( data ) . toEqual ( {
509
+ base64 : '' ,
510
+ contentType : undefined ,
511
+ } ) ;
617
512
} ) ;
618
513
619
514
it ( 'download with ajax abort' , async ( ) => {
620
- const mockXHR = function ( ) {
621
- return {
622
- open : jest . fn ( ) ,
623
- send : jest . fn ( ) . mockImplementation ( function ( ) {
624
- this . response = [ 61 , 170 , 236 , 120 ] ;
625
- this . readyState = 2 ;
626
- this . onreadystatechange ( ) ;
627
- } ) ,
628
- getResponseHeader : function ( ) {
629
- return 'image/png' ;
630
- } ,
631
- abort : function ( ) {
632
- this . status = 0 ;
633
- this . response = undefined ;
634
- this . readyState = 4 ;
635
- this . onreadystatechange ( ) ;
636
- } ,
637
- } ;
638
- } ;
639
- defaultController . _setXHR ( mockXHR ) ;
515
+ mockFetch ( [ ] , { } , { name : 'AbortError' } ) ;
640
516
let _requestTask ;
641
517
const options = {
642
518
requestTask : task => ( _requestTask = task ) ,
643
519
} ;
644
520
defaultController . download ( 'https://example.com/image.png' , options ) . then ( data => {
645
521
expect ( data ) . toEqual ( { } ) ;
646
522
} ) ;
523
+ expect ( _requestTask ) . toBeDefined ( ) ;
524
+ expect ( _requestTask . abort ) . toBeDefined ( ) ;
647
525
_requestTask . abort ( ) ;
648
526
} ) ;
649
527
650
528
it ( 'download with ajax error' , async ( ) => {
651
- const mockXHR = function ( ) {
652
- return {
653
- open : jest . fn ( ) ,
654
- send : jest . fn ( ) . mockImplementation ( function ( ) {
655
- this . onerror ( 'error thrown' ) ;
656
- } ) ,
657
- } ;
658
- } ;
659
- defaultController . _setXHR ( mockXHR ) ;
529
+ mockFetch ( [ ] , { } , new Error ( 'error thrown' ) ) ;
660
530
const options = {
661
531
requestTask : ( ) => { } ,
662
532
} ;
663
533
try {
664
534
await defaultController . download ( 'https://example.com/image.png' , options ) ;
665
535
} catch ( e ) {
666
- expect ( e ) . toBe ( 'error thrown' ) ;
667
- }
668
- } ) ;
669
-
670
- it ( 'download with xmlhttprequest unsupported' , async ( ) => {
671
- defaultController . _setXHR ( null ) ;
672
- process . env . PARSE_BUILD = 'browser' ;
673
- try {
674
- await defaultController . download ( 'https://example.com/image.png' ) ;
675
- } catch ( e ) {
676
- expect ( e ) . toBe ( 'Cannot make a request: No definition of XMLHttpRequest was found.' ) ;
536
+ expect ( e . message ) . toBe ( 'error thrown' ) ;
677
537
}
678
538
} ) ;
679
539
0 commit comments