fix: show thumbnail for non-image files by upload onChange (#25432)

This commit is contained in:
Albert Zhang 2020-07-06 21:47:01 +08:00 committed by GitHub
parent b06e2567b6
commit 6f2aa5afb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 1 deletions

View File

@ -785,6 +785,65 @@ describe('Upload List', () => {
});
});
describe('thumbUrl support for non-image', () => {
const nonImageFile = new File([''], 'foo.7z', { type: 'application/x-7z-compressed' });
it('should render <img /> when upload non-image file and configure thumbUrl in onChange', done => {
let wrapper;
const onChange = async ({ fileList: files }) => {
const newfileList = files.map(item => ({
...item,
thumbUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
}));
wrapper.setProps({ fileList: newfileList });
await sleep();
const imgNode = wrapper.find('.ant-upload-list-item-thumbnail img');
expect(imgNode.length).toBe(1);
done();
};
wrapper = mount(
<Upload
action="http://jsonplaceholder.typicode.com/posts/"
listType="picture-card"
fileList={[]}
onChange={onChange}
customRequest={successRequest}
>
<button type="button">upload</button>
</Upload>,
);
const imgNode = wrapper.find('.ant-upload-list-item-thumbnail img');
expect(imgNode.length).toBe(0);
wrapper.find('input').simulate('change', { target: { files: [nonImageFile] } });
});
it('should not render <img /> when upload non-image file without thumbUrl in onChange', done => {
let wrapper;
const onChange = async ({ fileList: files }) => {
wrapper.setProps({ fileList: files });
await sleep();
const imgNode = wrapper.find('.ant-upload-list-item-thumbnail img');
expect(imgNode.length).toBe(0);
done();
};
wrapper = mount(
<Upload
action="http://jsonplaceholder.typicode.com/posts/"
listType="picture-card"
fileList={[]}
onChange={onChange}
customRequest={successRequest}
>
<button type="button">upload</button>
</Upload>,
);
const imgNode = wrapper.find('.ant-upload-list-item-thumbnail img');
expect(imgNode.length).toBe(0);
wrapper.find('input').simulate('change', { target: { files: [nonImageFile] } });
});
});
it('should support transformFile', done => {
const handleTransformFile = jest.fn();
const onChange = ({ file }) => {

View File

@ -45,7 +45,7 @@ const extname = (url: string = '') => {
const isImageFileType = (type: string): boolean => type.indexOf('image/') === 0;
export const isImageUrl = (file: UploadFile): boolean => {
if (file.type) {
if (file.type && !file.thumbUrl) {
return isImageFileType(file.type);
}
const url: string = (file.thumbUrl || file.url) as string;