Fix beforeUpload will stop uploading when return (#7870)

value is undefined

it should be same as rc-upload
This commit is contained in:
偏右 2017-10-12 14:48:35 +08:00 committed by GitHub
parent b77cc6392e
commit 43362de756
2 changed files with 56 additions and 2 deletions

View File

@ -223,13 +223,13 @@ export default class Upload extends React.Component<UploadProps, any> {
return true;
}
const result = this.props.beforeUpload(file, fileList);
if (!result) {
if (result === false) {
this.onChange({
file,
fileList,
});
return false;
} else if ((result as PromiseLike<any>).then) {
} else if (result && (result as PromiseLike<any>).then) {
return result;
}
return true;

View File

@ -53,4 +53,58 @@ describe('Upload', () => {
},
});
});
it('should not stop upload when return value of beforeUpload is not false', (done) => {
const data = jest.fn();
const props = {
action: 'http://upload.com',
beforeUpload: () => false,
data,
onChange: () => {
expect(data).not.toBeCalled();
done();
},
};
const wrapper = mount(
<Upload {...props}>
<button>upload</button>
</Upload>
);
wrapper.find('input').simulate('change', {
target: {
files: [
{ filename: 'foo.png' },
],
},
});
});
it('should not stop upload when return value of beforeUpload is not false', (done) => {
const data = jest.fn();
const props = {
action: 'http://upload.com',
beforeUpload() {},
data,
onChange: () => {
expect(data).toBeCalled();
done();
},
};
const wrapper = mount(
<Upload {...props}>
<button>upload</button>
</Upload>
);
wrapper.find('input').simulate('change', {
target: {
files: [
{ filename: 'foo.png' },
],
},
});
});
});