@@ -9,10 +9,7 @@ const b64Digit = require('../ParseFile').b64Digit;
99
1010const ParseObject = require ( '../ParseObject' ) . default ;
1111const 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' ) ;
1613
1714const mockLocalDatastore = {
1815 _updateLocalIdForObject : jest . fn ( ( _localId , /** @type {ParseObject }*/ object ) => {
@@ -491,189 +488,52 @@ describe('FileController', () => {
491488 spy2 . mockRestore ( ) ;
492489 } ) ;
493490
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-
568491 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' } ) ;
586494 const options = {
587495 requestTask : ( ) => { } ,
588496 } ;
589497 const data = await defaultController . download ( 'https://example.com/image.png' , options ) ;
590- expect ( data . base64 ) . toBe ( 'ParseA==' ) ;
498+ expect ( data . base64 ) . toBeDefined ( ) ;
591499 expect ( data . contentType ) . toBe ( 'image/png' ) ;
592500 } ) ;
593501
594502 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 } ) ;
612504 const options = {
613505 requestTask : ( ) => { } ,
614506 } ;
615507 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+ } ) ;
617512 } ) ;
618513
619514 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' } ) ;
640516 let _requestTask ;
641517 const options = {
642518 requestTask : task => ( _requestTask = task ) ,
643519 } ;
644520 defaultController . download ( 'https://example.com/image.png' , options ) . then ( data => {
645521 expect ( data ) . toEqual ( { } ) ;
646522 } ) ;
523+ expect ( _requestTask ) . toBeDefined ( ) ;
524+ expect ( _requestTask . abort ) . toBeDefined ( ) ;
647525 _requestTask . abort ( ) ;
648526 } ) ;
649527
650528 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' ) ) ;
660530 const options = {
661531 requestTask : ( ) => { } ,
662532 } ;
663533 try {
664534 await defaultController . download ( 'https://example.com/image.png' , options ) ;
665535 } 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' ) ;
677537 }
678538 } ) ;
679539
0 commit comments