@@ -373,13 +373,15 @@ describe('FileController', () => {
373
373
} ) ;
374
374
375
375
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 ) ;
377
379
file . _source . format = 'file' ;
378
380
379
381
return file . save ( ) . then ( function ( f ) {
380
382
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' ) ;
383
385
} ) ;
384
386
} ) ;
385
387
@@ -647,13 +649,15 @@ describe('FileController', () => {
647
649
} ) ;
648
650
} ;
649
651
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 ) ;
651
655
file . _source . format = 'file' ;
652
656
653
657
return file . save ( { sessionToken : 'testing_sessionToken' } ) . then ( function ( f ) {
654
658
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' ) ;
657
661
} ) ;
658
662
} ) ;
659
663
@@ -685,38 +689,97 @@ describe('FileController', () => {
685
689
} ) ;
686
690
} ;
687
691
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 ) ;
689
695
file . _source . format = 'file' ;
690
696
691
697
return file . save ( ) . then ( function ( f ) {
692
698
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' ) ;
695
701
} ) ;
696
702
} ) ;
697
703
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
+ } ) ;
703
712
}
704
713
} ) ;
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
+ }
706
770
return [ { success : { objectId : 'child' } } ] ;
707
- } } ) ;
771
+ } ) ;
772
+ CoreManager . setRESTController ( { ajax, request } ) ;
708
773
CoreManager . setLocalDatastore ( mockLocalDatastore ) ;
709
774
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 ) ;
711
778
file . _source . format = 'file' ;
712
779
const object = ParseObject . fromJSON ( { className : 'TestObject' } ) ;
713
780
object . set ( 'file' , file ) ;
714
781
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 ( ) ;
720
783
} ) ;
721
784
722
785
it ( 'should throw error if file deleted without name' , async ( done ) => {
0 commit comments