@@ -41,6 +41,10 @@ describe('ParseFile', () => {
41
41
} ) ;
42
42
} ) ;
43
43
44
+ afterEach ( ( ) => {
45
+ process . env . PARSE_BUILD = 'node' ;
46
+ } ) ;
47
+
44
48
it ( 'can create files with base64 encoding' , ( ) => {
45
49
const file = new ParseFile ( 'parse.txt' , { base64 : 'ParseA==' } ) ;
46
50
expect ( file . _source . base64 ) . toBe ( 'ParseA==' ) ;
@@ -303,6 +307,7 @@ describe('FileController', () => {
303
307
} ) ;
304
308
305
309
it ( 'download with base64 http' , async ( ) => {
310
+ defaultController . _setXHR ( null ) ;
306
311
const mockResponse = Object . create ( EventEmitter . prototype ) ;
307
312
EventEmitter . call ( mockResponse ) ;
308
313
mockResponse . setEncoding = function ( ) { }
@@ -328,6 +333,7 @@ describe('FileController', () => {
328
333
} ) ;
329
334
330
335
it ( 'download with base64 https' , async ( ) => {
336
+ defaultController . _setXHR ( null ) ;
331
337
const mockResponse = Object . create ( EventEmitter . prototype ) ;
332
338
EventEmitter . call ( mockResponse ) ;
333
339
mockResponse . setEncoding = function ( ) { }
@@ -351,4 +357,55 @@ describe('FileController', () => {
351
357
expect ( mockHttps . get ) . toHaveBeenCalledTimes ( 1 ) ;
352
358
spy . mockRestore ( ) ;
353
359
} ) ;
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
+ } ) ;
354
411
} ) ;
0 commit comments