@@ -11,6 +11,7 @@ jest.autoMockOff();
1111jest . mock ( 'http' ) ;
1212jest . mock ( 'https' ) ;
1313
14+ const ParseError = require ( '../ParseError' ) . default ;
1415const ParseFile = require ( '../ParseFile' ) . default ;
1516const CoreManager = require ( '../CoreManager' ) ;
1617const EventEmitter = require ( '../EventEmitter' ) ;
@@ -613,4 +614,55 @@ describe('FileController', () => {
613614 expect ( f . url ( ) ) . toBe ( 'https://files.parsetfss.com/a//api.parse.com/1/files/parse.txt' ) ;
614615 } ) ;
615616 } ) ;
617+
618+ it ( 'should throw error if file deleted without name' , async ( done ) => {
619+ const file = new ParseFile ( '' , [ 1 , 2 , 3 ] ) ;
620+ try {
621+ await file . destroy ( ) ;
622+ } catch ( e ) {
623+ expect ( e . message ) . toBe ( 'Cannot delete an unsaved ParseFile.' ) ;
624+ done ( ) ;
625+ }
626+ } ) ;
627+
628+ it ( 'should delete file' , async ( ) => {
629+ const file = new ParseFile ( 'filename' , [ 1 , 2 , 3 ] ) ;
630+ const ajax = jest . fn ( ) . mockResolvedValueOnce ( { foo : 'bar' } ) ;
631+ CoreManager . setRESTController ( { ajax, request : ( ) => { } } ) ;
632+ const result = await file . destroy ( ) ;
633+ expect ( result ) . toEqual ( file ) ;
634+ expect ( ajax ) . toHaveBeenCalledWith ( 'DELETE' , 'https://api.parse.com/1/files/filename' , '' , {
635+ "X-Parse-Application-ID" : null ,
636+ "X-Parse-Master-Key" : null ,
637+ } ) ;
638+ } ) ;
639+
640+ it ( 'should handle delete file error' , async ( ) => {
641+ const file = new ParseFile ( 'filename' , [ 1 , 2 , 3 ] ) ;
642+ const ajax = jest . fn ( ) . mockResolvedValueOnce ( Promise . reject ( new ParseError ( 403 , 'Cannot delete file.' ) ) ) ;
643+ const handleError = jest . fn ( ) ;
644+ CoreManager . setRESTController ( { ajax, request : ( ) => { } , handleError } ) ;
645+ const result = await file . destroy ( ) ;
646+ expect ( result ) . toEqual ( file ) ;
647+ expect ( ajax ) . toHaveBeenCalledWith ( 'DELETE' , 'https://api.parse.com/1/files/filename' , '' , {
648+ "X-Parse-Application-ID" : null ,
649+ "X-Parse-Master-Key" : null ,
650+ } ) ;
651+ expect ( handleError ) . toHaveBeenCalled ( ) ;
652+ } ) ;
653+
654+ it ( 'should handle delete file error invalid server response' , async ( ) => {
655+ const file = new ParseFile ( 'filename' , [ 1 , 2 , 3 ] ) ;
656+ const response = null ;
657+ const ajax = jest . fn ( ) . mockResolvedValueOnce ( Promise . reject ( response ) ) ;
658+ const handleError = jest . fn ( ) ;
659+ CoreManager . setRESTController ( { ajax, request : ( ) => { } , handleError } ) ;
660+ const result = await file . destroy ( ) ;
661+ expect ( result ) . toEqual ( file ) ;
662+ expect ( ajax ) . toHaveBeenCalledWith ( 'DELETE' , 'https://api.parse.com/1/files/filename' , '' , {
663+ "X-Parse-Application-ID" : null ,
664+ "X-Parse-Master-Key" : null ,
665+ } ) ;
666+ expect ( handleError ) . not . toHaveBeenCalled ( ) ;
667+ } ) ;
616668} ) ;
0 commit comments