Skip to content

Commit 6692237

Browse files
committed
fix: action should delay fetch after beforeUpload done
1 parent cac326e commit 6692237

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/AjaxUploader.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff 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);

tests/uploader.spec.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { mount } from 'enzyme';
55
import sinon from 'sinon';
66
import Uploader from '../index';
77

8+
const sleep = (timeout = 500) => new Promise(resolve => setTimeout(resolve, timeout));
9+
810
function 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
});

0 commit comments

Comments
 (0)