@@ -373,13 +373,15 @@ describe('FileController', () => {
373373 } ) ;
374374
375375 it ( 'saves files via ajax' , ( ) => {
376- const file = new ParseFile ( 'parse.txt' , [ 61 , 170 , 236 , 120 ] ) ;
376+ // eslint-disable-next-line no-undef
377+ const blob = new Blob ( [ 61 , 170 , 236 , 120 ] ) ;
378+ const file = new ParseFile ( 'parse.txt' , blob ) ;
377379 file . _source . format = 'file' ;
378380
379381 return file . save ( ) . then ( function ( f ) {
380382 expect ( f ) . toBe ( file ) ;
381- expect ( f . name ( ) ) . toBe ( '/api.parse.com/1/files/ parse.txt' ) ;
382- expect ( f . url ( ) ) . toBe ( 'https://files.parsetfss.com/a//api.parse.com/1/files/ parse.txt' ) ;
383+ expect ( f . name ( ) ) . toBe ( 'parse.txt' ) ;
384+ expect ( f . url ( ) ) . toBe ( 'https://files.parsetfss.com/a/parse.txt' ) ;
383385 } ) ;
384386 } ) ;
385387
@@ -647,13 +649,15 @@ describe('FileController', () => {
647649 } ) ;
648650 } ;
649651 CoreManager . setRESTController ( { request, ajax } ) ;
650- const file = new ParseFile ( 'parse.txt' , [ 61 , 170 , 236 , 120 ] ) ;
652+ // eslint-disable-next-line no-undef
653+ const blob = new Blob ( [ 61 , 170 , 236 , 120 ] ) ;
654+ const file = new ParseFile ( 'parse.txt' , blob ) ;
651655 file . _source . format = 'file' ;
652656
653657 return file . save ( { sessionToken : 'testing_sessionToken' } ) . then ( function ( f ) {
654658 expect ( f ) . toBe ( file ) ;
655- expect ( f . name ( ) ) . toBe ( '/api.parse.com/1/files/ parse.txt' ) ;
656- expect ( f . url ( ) ) . toBe ( 'https://files.parsetfss.com/a//api.parse.com/1/files/ parse.txt' ) ;
659+ expect ( f . name ( ) ) . toBe ( 'parse.txt' ) ;
660+ expect ( f . url ( ) ) . toBe ( 'https://files.parsetfss.com/a/parse.txt' ) ;
657661 } ) ;
658662 } ) ;
659663
@@ -685,38 +689,97 @@ describe('FileController', () => {
685689 } ) ;
686690 } ;
687691 CoreManager . setRESTController ( { request, ajax } ) ;
688- const file = new ParseFile ( 'parse.txt' , [ 61 , 170 , 236 , 120 ] ) ;
692+ // eslint-disable-next-line no-undef
693+ const blob = new Blob ( [ 61 , 170 , 236 , 120 ] ) ;
694+ const file = new ParseFile ( 'parse.txt' , blob ) ;
689695 file . _source . format = 'file' ;
690696
691697 return file . save ( ) . then ( function ( f ) {
692698 expect ( f ) . toBe ( file ) ;
693- expect ( f . name ( ) ) . toBe ( '/api.parse.com/1/files/ parse.txt' ) ;
694- expect ( f . url ( ) ) . toBe ( 'https://files.parsetfss.com/a//api.parse.com/1/files/ parse.txt' ) ;
699+ expect ( f . name ( ) ) . toBe ( 'parse.txt' ) ;
700+ expect ( f . url ( ) ) . toBe ( 'https://files.parsetfss.com/a/parse.txt' ) ;
695701 } ) ;
696702 } ) ;
697703
698- it ( 'saves files via object saveAll options' , async ( ) => {
699- const ajax = jest . fn ( ) . mockResolvedValueOnce ( {
700- response : {
701- name : 'parse.txt' ,
702- url : 'http://files.parsetfss.com/a/parse.txt'
704+ it ( 'should save file using saveFile with metadata and tags' , async ( ) => {
705+ CoreManager . set ( 'UserController' , {
706+ currentUserAsync ( ) {
707+ return Promise . resolve ( {
708+ getSessionToken ( ) {
709+ return 'currentUserToken' ;
710+ }
711+ } ) ;
703712 }
704713 } ) ;
705- CoreManager . setRESTController ( { ajax, request : ( ) => {
714+ const request = jest . fn ( ( method , path ) => {
715+ const name = path . substr ( path . indexOf ( '/' ) + 1 ) ;
716+ return Promise . resolve ( {
717+ name : name ,
718+ url : 'https://files.parsetfss.com/a/' + name
719+ } ) ;
720+ } ) ;
721+ const ajax = function ( method , path , data , headers , options ) {
722+ expect ( options . sessionToken ) . toBe ( 'currentUserToken' )
723+ const name = path . substr ( path . indexOf ( '/' ) + 1 ) ;
724+ return Promise . resolve ( {
725+ response : {
726+ name : name ,
727+ url : 'https://files.parsetfss.com/a/' + name
728+ }
729+ } ) ;
730+ } ;
731+ CoreManager . setRESTController ( { request, ajax } ) ;
732+ // eslint-disable-next-line no-undef
733+ const blob = new Blob ( [ 61 , 170 , 236 , 120 ] ) ;
734+ const file = new ParseFile ( 'parse.txt' , blob ) ;
735+ file . _source . format = 'file' ;
736+ file . addMetadata ( 'foo' , 'bar' ) ;
737+ file . addTag ( 'bar' , 'foo' ) ;
738+ const f = await file . save ( ) ;
739+ expect ( f ) . toBe ( file ) ;
740+ expect ( f . name ( ) ) . toBe ( 'parse.txt' ) ;
741+ expect ( f . url ( ) ) . toBe ( 'https://files.parsetfss.com/a/parse.txt' ) ;
742+ expect ( request ) . toHaveBeenCalledWith (
743+ 'POST' ,
744+ 'files/parse.txt' ,
745+ {
746+ base64 : 'NjExNzAyMzYxMjA=' ,
747+ fileData : {
748+ metadata : {
749+ foo : 'bar' ,
750+ } ,
751+ tags : {
752+ bar : 'foo' ,
753+ } ,
754+ } ,
755+ } ,
756+ { requestTask : expect . any ( Function ) } ,
757+ ) ;
758+ } ) ;
759+
760+ it ( 'saves files via object saveAll options' , async ( ) => {
761+ const ajax = async ( ) => { } ;
762+ const request = jest . fn ( async ( method , path , data , options ) => {
763+ if ( path . indexOf ( 'files/' ) === 0 ) {
764+ expect ( options . sessionToken ) . toBe ( 'testToken' ) ;
765+ return {
766+ name : 'parse.txt' ,
767+ url : 'http://files.parsetfss.com/a/parse.txt'
768+ } ;
769+ }
706770 return [ { success : { objectId : 'child' } } ] ;
707- } } ) ;
771+ } ) ;
772+ CoreManager . setRESTController ( { ajax, request } ) ;
708773 CoreManager . setLocalDatastore ( mockLocalDatastore ) ;
709774
710- const file = new ParseFile ( 'parse.txt' , [ 61 , 170 , 236 , 120 ] ) ;
775+ // eslint-disable-next-line no-undef
776+ const blob = new Blob ( [ 61 , 170 , 236 , 120 ] ) ;
777+ const file = new ParseFile ( 'parse.txt' , blob ) ;
711778 file . _source . format = 'file' ;
712779 const object = ParseObject . fromJSON ( { className : 'TestObject' } ) ;
713780 object . set ( 'file' , file ) ;
714781 await ParseObject . saveAll ( [ object ] , { sessionToken : 'testToken' } ) ;
715-
716- const request = ajax . mock . calls [ 0 ] ;
717- expect ( request [ 1 ] ) . toBe ( 'https://api.parse.com/1/files/parse.txt' )
718- expect ( request [ 3 ] [ 'X-Parse-Session-Token' ] ) . toBe ( 'testToken' ) ;
719- expect ( request [ 4 ] . sessionToken ) . toBe ( 'testToken' ) ;
782+ expect ( request ) . toHaveBeenCalled ( ) ;
720783 } ) ;
721784
722785 it ( 'should throw error if file deleted without name' , async ( done ) => {
0 commit comments