@@ -351,6 +351,33 @@ describe('ParseFile', () => {
351351 {
352352 metadata : { foo : 'bar' } ,
353353 tags : { bar : 'foo' } ,
354+ directory : undefined ,
355+ requestTask : expect . any ( Function ) ,
356+ }
357+ ) ;
358+ } ) ;
359+
360+ it ( 'should save file with directory option' , async ( ) => {
361+ const fileController = {
362+ saveFile : jest . fn ( ) . mockResolvedValue ( { } ) ,
363+ saveBase64 : ( ) => { } ,
364+ download : ( ) => { } ,
365+ } ;
366+ CoreManager . setFileController ( fileController ) ;
367+ const file = new ParseFile ( 'donald_duck.txt' , new File ( [ 'Parse' ] , 'donald_duck.txt' ) ) ;
368+ file . setDirectory ( 'user-uploads/avatars' ) ;
369+ await file . save ( ) ;
370+ expect ( fileController . saveFile ) . toHaveBeenCalledWith (
371+ 'donald_duck.txt' ,
372+ {
373+ file : expect . any ( File ) ,
374+ format : 'file' ,
375+ type : '' ,
376+ } ,
377+ {
378+ metadata : { } ,
379+ tags : { } ,
380+ directory : 'user-uploads/avatars' ,
354381 requestTask : expect . any ( Function ) ,
355382 }
356383 ) ;
@@ -403,6 +430,29 @@ describe('ParseFile', () => {
403430 expect ( file . tags ( ) ) . toEqual ( { } ) ;
404431 } ) ;
405432
433+ it ( 'should set directory' , ( ) => {
434+ const file = new ParseFile ( 'parse.txt' , [ 61 , 170 , 236 , 120 ] ) ;
435+ file . setDirectory ( 'user-uploads/avatars' ) ;
436+ expect ( file . directory ( ) ) . toBe ( 'user-uploads/avatars' ) ;
437+ } ) ;
438+
439+ it ( 'should not set directory if value is not a string' , ( ) => {
440+ const file = new ParseFile ( 'parse.txt' , [ 61 , 170 , 236 , 120 ] ) ;
441+ file . setDirectory ( 123 ) ;
442+ expect ( file . directory ( ) ) . toBeUndefined ( ) ;
443+ } ) ;
444+
445+ it ( 'should not set directory if value is an empty string' , ( ) => {
446+ const file = new ParseFile ( 'parse.txt' , [ 61 , 170 , 236 , 120 ] ) ;
447+ file . setDirectory ( '' ) ;
448+ expect ( file . directory ( ) ) . toBeUndefined ( ) ;
449+ } ) ;
450+
451+ it ( 'should return undefined directory by default' , ( ) => {
452+ const file = new ParseFile ( 'parse.txt' , [ 61 , 170 , 236 , 120 ] ) ;
453+ expect ( file . directory ( ) ) . toBeUndefined ( ) ;
454+ } ) ;
455+
406456 it ( 'can create files with a Buffer' , ( ) => {
407457 const buffer = Buffer . from ( [ 61 , 170 , 236 , 120 ] ) ;
408458 const file = new ParseFile ( 'parse.txt' , buffer , 'application/octet-stream' ) ;
@@ -761,6 +811,68 @@ describe('FileController', () => {
761811 ) ;
762812 } ) ;
763813
814+ it ( 'should include directory in fileData payload when saving' , async ( ) => {
815+ const request = jest . fn ( ( method , path ) => {
816+ const name = path . substr ( path . indexOf ( '/' ) + 1 ) ;
817+ return Promise . resolve ( {
818+ name : name ,
819+ url : 'https://files.example.com/a/' + name ,
820+ } ) ;
821+ } ) ;
822+ const ajax = function ( ) {
823+ return Promise . resolve ( { response : { } } ) ;
824+ } ;
825+ CoreManager . setRESTController ( { request, ajax } ) ;
826+
827+ const file = new ParseFile ( 'parse.txt' , { base64 : 'ParseA==' } ) ;
828+ file . setDirectory ( 'user-uploads/avatars' ) ;
829+ await file . save ( ) ;
830+ expect ( request ) . toHaveBeenCalledWith (
831+ 'POST' ,
832+ 'files/parse.txt' ,
833+ {
834+ base64 : 'ParseA==' ,
835+ _ContentType : 'text/plain' ,
836+ fileData : {
837+ metadata : { } ,
838+ tags : { } ,
839+ directory : 'user-uploads/avatars' ,
840+ } ,
841+ } ,
842+ { requestTask : expect . any ( Function ) }
843+ ) ;
844+ } ) ;
845+
846+ it ( 'should not include directory in fileData payload when not set' , async ( ) => {
847+ const request = jest . fn ( ( method , path ) => {
848+ const name = path . substr ( path . indexOf ( '/' ) + 1 ) ;
849+ return Promise . resolve ( {
850+ name : name ,
851+ url : 'https://files.example.com/a/' + name ,
852+ } ) ;
853+ } ) ;
854+ const ajax = function ( ) {
855+ return Promise . resolve ( { response : { } } ) ;
856+ } ;
857+ CoreManager . setRESTController ( { request, ajax } ) ;
858+
859+ const file = new ParseFile ( 'parse.txt' , { base64 : 'ParseA==' } ) ;
860+ await file . save ( ) ;
861+ expect ( request ) . toHaveBeenCalledWith (
862+ 'POST' ,
863+ 'files/parse.txt' ,
864+ {
865+ base64 : 'ParseA==' ,
866+ _ContentType : 'text/plain' ,
867+ fileData : {
868+ metadata : { } ,
869+ tags : { } ,
870+ } ,
871+ } ,
872+ { requestTask : expect . any ( Function ) }
873+ ) ;
874+ } ) ;
875+
764876 it ( 'saves files via object saveAll options' , async ( ) => {
765877 const ajax = async ( ) => { } ;
766878 const request = jest . fn ( async ( method , path , data , options ) => {
@@ -1139,7 +1251,7 @@ describe('FileController', () => {
11391251 expect ( true ) . toBe ( false ) ;
11401252 } catch ( e ) {
11411253 expect ( e . message ) . toBe (
1142- 'Cannot save a stream-based file with metadata or tags . Use a Buffer instead.'
1254+ 'Cannot save a stream-based file with metadata, tags, or directory . Use a Buffer instead.'
11431255 ) ;
11441256 }
11451257 } ) ;
@@ -1160,6 +1272,29 @@ describe('FileController', () => {
11601272 expect ( request ) . not . toHaveBeenCalled ( ) ;
11611273 } ) ;
11621274
1275+ it ( 'buffer with directory falls back to saveBase64' , async ( ) => {
1276+ const request = jest . fn ( ) . mockResolvedValue ( {
1277+ name : 'parse.txt' ,
1278+ url : 'https://files.example.com/a/parse.txt' ,
1279+ } ) ;
1280+ const ajax = jest . fn ( ) ;
1281+ CoreManager . setRESTController ( { request, ajax } ) ;
1282+
1283+ const file = new ParseFile ( 'parse.txt' , Buffer . from ( [ 61 , 170 , 236 , 120 ] ) , 'text/plain' ) ;
1284+ file . setDirectory ( 'user-uploads/avatars' ) ;
1285+ await file . save ( ) ;
1286+
1287+ expect ( ajax ) . not . toHaveBeenCalled ( ) ;
1288+ expect ( request ) . toHaveBeenCalledWith (
1289+ 'POST' ,
1290+ 'files/parse.txt' ,
1291+ expect . objectContaining ( {
1292+ fileData : expect . objectContaining ( { directory : 'user-uploads/avatars' } ) ,
1293+ } ) ,
1294+ expect . any ( Object )
1295+ ) ;
1296+ } ) ;
1297+
11631298 it ( 'falls back to saveBase64 when controller lacks saveBinary' , async ( ) => {
11641299 CoreManager . setFileController ( {
11651300 saveFile : jest . fn ( ) ,
0 commit comments