@@ -41,6 +41,10 @@ describe('ParseFile', () => {
4141 } ) ;
4242 } ) ;
4343
44+ afterEach ( ( ) => {
45+ process . env . PARSE_BUILD = 'node' ;
46+ } ) ;
47+
4448 it ( 'can create files with base64 encoding' , ( ) => {
4549 const file = new ParseFile ( 'parse.txt' , { base64 : 'ParseA==' } ) ;
4650 expect ( file . _source . base64 ) . toBe ( 'ParseA==' ) ;
@@ -303,6 +307,7 @@ describe('FileController', () => {
303307 } ) ;
304308
305309 it ( 'download with base64 http' , async ( ) => {
310+ defaultController . _setXHR ( null ) ;
306311 const mockResponse = Object . create ( EventEmitter . prototype ) ;
307312 EventEmitter . call ( mockResponse ) ;
308313 mockResponse . setEncoding = function ( ) { }
@@ -328,6 +333,7 @@ describe('FileController', () => {
328333 } ) ;
329334
330335 it ( 'download with base64 https' , async ( ) => {
336+ defaultController . _setXHR ( null ) ;
331337 const mockResponse = Object . create ( EventEmitter . prototype ) ;
332338 EventEmitter . call ( mockResponse ) ;
333339 mockResponse . setEncoding = function ( ) { }
@@ -351,4 +357,55 @@ describe('FileController', () => {
351357 expect ( mockHttps . get ) . toHaveBeenCalledTimes ( 1 ) ;
352358 spy . mockRestore ( ) ;
353359 } ) ;
360+
361+ it ( 'download with ajax' , async ( ) => {
362+ const mockXHR = function ( ) {
363+ return {
364+ open : jest . fn ( ) ,
365+ send : jest . fn ( ) . mockImplementation ( function ( ) {
366+ this . response = [ 61 , 170 , 236 , 120 ] ;
367+ this . readyState = 2 ;
368+ this . onreadystatechange ( ) ;
369+ this . readyState = 4 ;
370+ this . onreadystatechange ( ) ;
371+ } ) ,
372+ getResponseHeader : function ( ) {
373+ return 'image/png' ;
374+ }
375+ } ;
376+ } ;
377+ defaultController . _setXHR ( mockXHR ) ;
378+
379+ const data = await defaultController . download ( 'https://example.com/image.png' ) ;
380+ expect ( data . base64 ) . toBe ( 'ParseA==' ) ;
381+ expect ( data . contentType ) . toBe ( 'image/png' ) ;
382+ } ) ;
383+
384+ it ( 'download with ajax error' , async ( ) => {
385+ const mockXHR = function ( ) {
386+ return {
387+ open : jest . fn ( ) ,
388+ send : jest . fn ( ) . mockImplementation ( function ( ) {
389+ this . onerror ( 'error thrown' ) ;
390+ } )
391+ } ;
392+ } ;
393+ defaultController . _setXHR ( mockXHR ) ;
394+
395+ try {
396+ await defaultController . download ( 'https://example.com/image.png' ) ;
397+ } catch ( e ) {
398+ expect ( e ) . toBe ( 'error thrown' ) ;
399+ }
400+ } ) ;
401+
402+ it ( 'download with xmlhttprequest unsupported' , async ( ) => {
403+ defaultController . _setXHR ( null ) ;
404+ process . env . PARSE_BUILD = 'browser' ;
405+ try {
406+ await defaultController . download ( 'https://example.com/image.png' ) ;
407+ } catch ( e ) {
408+ expect ( e ) . toBe ( 'Cannot make a request: No definition of XMLHttpRequest was found.' ) ;
409+ }
410+ } ) ;
354411} ) ;
0 commit comments