File tree Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Original file line number Diff line number Diff line change @@ -123,7 +123,7 @@ class AjaxUploader extends Component<UploadProps> {
123123 * Process file before upload. When all the file is ready, we start upload.
124124 */
125125 processFile = async ( file : RcFile , fileList : RcFile [ ] ) : Promise < ParsedFileInfo > => {
126- const { beforeUpload, action , data } = this . props ;
126+ const { beforeUpload } = this . props ;
127127
128128 let transformedFile : BeforeUploadFileType | void = file ;
129129 if ( beforeUpload ) {
@@ -143,13 +143,17 @@ class AjaxUploader extends Component<UploadProps> {
143143 }
144144 }
145145
146+ // Get latest action
147+ const { action } = this . props ;
146148 let mergedAction : string ;
147149 if ( typeof action === 'function' ) {
148150 mergedAction = await action ( file ) ;
149151 } else {
150152 mergedAction = action ;
151153 }
152154
155+ // Get latest data
156+ const { data } = this . props ;
153157 let mergedData : object ;
154158 if ( typeof data === 'function' ) {
155159 mergedData = await data ( file ) ;
Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ import { mount } from 'enzyme';
55import sinon from 'sinon' ;
66import Uploader from '../index' ;
77
8+ const sleep = ( timeout = 500 ) => new Promise ( resolve => setTimeout ( resolve , timeout ) ) ;
9+
810function Item ( name ) {
911 this . name = name ;
1012 this . toString = ( ) => this . name ;
@@ -466,8 +468,6 @@ describe('uploader', () => {
466468 } ) ,
467469 ) ;
468470
469- const sleep = ( timeout = 500 ) => new Promise ( resolve => setTimeout ( resolve , timeout ) ) ;
470-
471471 async function testWrapper ( props ) {
472472 const onBatchStart = jest . fn ( ) ;
473473 const wrapper = mount ( < Uploader onBatchStart = { onBatchStart } { ...props } /> ) ;
@@ -555,4 +555,36 @@ describe('uploader', () => {
555555 expect ( onBatchStart ) . toHaveBeenCalledWith ( batchEventFiles ) ;
556556 } ) ;
557557 } ) ;
558+
559+ it ( 'dynamic change action in beforeUpload should work' , async ( ) => {
560+ const Test = ( ) => {
561+ const [ action , setAction ] = React . useState ( 'light' ) ;
562+
563+ async function beforeUpload ( ) {
564+ setAction ( 'bamboo' ) ;
565+ await sleep ( 100 ) ;
566+ return true ;
567+ }
568+
569+ return < Uploader beforeUpload = { beforeUpload } action = { action } /> ;
570+ } ;
571+
572+ const wrapper = mount ( < Test /> ) ;
573+ wrapper . find ( 'input' ) . simulate ( 'change' , {
574+ target : {
575+ files : [
576+ {
577+ name : 'little.png' ,
578+ toString ( ) {
579+ return this . name ;
580+ } ,
581+ } ,
582+ ] ,
583+ } ,
584+ } ) ;
585+
586+ await sleep ( 200 ) ;
587+
588+ expect ( requests [ 0 ] . url ) . toEqual ( 'bamboo' ) ;
589+ } ) ;
558590} ) ;
You can’t perform that action at this time.
0 commit comments