File tree Expand file tree Collapse file tree 2 files changed +62
-11
lines changed Expand file tree Collapse file tree 2 files changed +62
-11
lines changed Original file line number Diff line number Diff line change @@ -134,22 +134,26 @@ class AjaxUploader extends Component {
134134 } = props ;
135135
136136 new Promise ( resolve => {
137- let { data , action } = props ;
137+ let { action } = props ;
138138 if ( typeof action === 'function' ) {
139139 action = action ( file ) ;
140140 }
141- if ( typeof data === 'function' ) {
142- data = data ( file ) ;
143- }
144- resolve ( Promise . all ( [ action , data ] ) ) ;
145- } ) . then ( ( [ action , data ] ) => {
141+ return resolve ( action ) ;
142+ } ) . then ( action => {
146143 const { uid } = file ;
147144 const request = props . customRequest || defaultRequest ;
148- const transform = Promise . resolve ( transformFile ( file ) ) . catch ( e => {
149- console . error ( e ) ; // eslint-disable-line no-console
150- } ) ;
145+ const transform = Promise . resolve ( transformFile ( file ) )
146+ . then ( ( transformedFile ) => {
147+ let { data } = props ;
148+ if ( typeof data === 'function' ) {
149+ data = data ( transformedFile ) ;
150+ }
151+ return Promise . all ( [ transformedFile , data ] ) ;
152+ } ) . catch ( e => {
153+ console . error ( e ) ; // eslint-disable-line no-console
154+ } ) ;
151155
152- transform . then ( ( transformedFile ) => {
156+ transform . then ( ( [ transformedFile , data ] ) => {
153157 const requestOption = {
154158 action,
155159 filename : props . name ,
Original file line number Diff line number Diff line change @@ -374,7 +374,7 @@ describe('uploader', () => {
374374 expect ( requests [ 0 ] . url ) . to . be ( '/upload.do' ) ;
375375 expect ( requests [ 0 ] . requestBody . get ( 'field1' ) ) . to . be ( 'a' ) ;
376376 done ( ) ;
377- } , 1000 ) ;
377+ } , 2000 ) ;
378378 } , 100 ) ;
379379 } ) ;
380380 } ) ;
@@ -469,6 +469,53 @@ describe('uploader', () => {
469469 ReactDOM . unmountComponentAtNode ( node ) ;
470470 } ) ;
471471
472+ it ( 'transform file function should be called before data function' , done => {
473+ const props = {
474+ action : '/test' ,
475+ data ( file ) {
476+ return new Promise ( ( resolve ) => {
477+ setTimeout ( ( ) => {
478+ resolve ( {
479+ url : file . url
480+ } )
481+ } , 500 )
482+ } )
483+ } ,
484+ transformFile ( file ) {
485+ return new Promise ( ( resolve ) => {
486+ setTimeout ( ( ) => {
487+ file . url = 'this is file url' ;
488+ resolve ( file ) ;
489+ } , 500 ) ;
490+ } ) ;
491+ } ,
492+ } ;
493+ ReactDOM . render ( < Uploader { ...props } /> , node , function init ( ) {
494+ uploader = this ;
495+ const input = TestUtils . findRenderedDOMComponentWithTag ( uploader , 'input' ) ;
496+
497+ const files = [
498+ {
499+ name : 'success.png' ,
500+ toString ( ) {
501+ return this . name ;
502+ } ,
503+ } ,
504+ ] ;
505+
506+ files . item = i => files [ i ] ;
507+
508+ Simulate . change ( input , { target : { files } } ) ;
509+
510+ setTimeout ( ( ) => {
511+ setTimeout ( ( ) => {
512+ expect ( requests [ 0 ] . requestBody . get ( 'url' ) ) . to . be ( 'this is file url' ) ;
513+ done ( ) ;
514+ } , 1000 ) ;
515+ } , 100 ) ;
516+ } ) ;
517+ } ) ;
518+
472519 it ( 'noes not affect receive origin file when transform file is null' , done => {
473520 const handlers = { } ;
474521 const props = {
You can’t perform that action at this time.
0 commit comments