test: update cases for UploadList

This commit is contained in:
zy410419243 2019-03-07 09:57:20 +08:00
parent 0f356fb151
commit e6c1fd3385
2 changed files with 48 additions and 6 deletions

View File

@ -9,7 +9,7 @@ import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
const imageTypes: string[] = ['image', 'webp', 'png', 'svg', 'gif', 'jpg', 'jpeg', 'bmp', 'dpg'];
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
const previewFile = (file: File | Blob, callback: Function) => {
export const previewFile = (file: File | Blob, callback: Function) => {
if (file.type && !imageTypes.includes(file.type)) {
callback('');
}
@ -18,7 +18,7 @@ const previewFile = (file: File | Blob, callback: Function) => {
reader.readAsDataURL(file);
};
const extname = (url: string) => {
export const extname = (url: string) => {
if (!url) {
return '';
}
@ -87,13 +87,9 @@ export default class UploadList extends React.Component<UploadListProps, any> {
) {
return;
}
/*eslint-disable */
file.thumbUrl = '';
/*eslint-enable */
previewFile(file.originFileObj, (previewDataUrl: string) => {
/*eslint-disable */
file.thumbUrl = previewDataUrl;
/*eslint-enable */
this.forceUpdate();
});
});

View File

@ -1,6 +1,7 @@
import React from 'react';
import { mount } from 'enzyme';
import Upload from '..';
import UploadList, { previewFile, extname } from '../UploadList';
import Form from '../../form';
import { errorRequest, successRequest } from './requests';
import { setup, teardown } from './mock';
@ -349,4 +350,49 @@ describe('Upload List', () => {
wrapper.find(Form).simulate('submit');
expect(errors).toBeNull();
});
it('return when prop onPreview not exists', () => {
const wrapper = mount(<UploadList />).instance();
expect(wrapper.handlePreview()).toBe(undefined);
});
it('previewFile should work correctly', () => {
const callback = jest.fn();
const file = new File([''], 'test.txt', { type: 'text/plain' });
previewFile(file, callback);
expect(callback).toBeCalled();
});
it('extname should work correctly', () => {
expect(extname()).toBe('');
});
it('when picture-card is loading, icon should render correctly', () => {
const items = [{ status: 'uploading', uid: 'upload-list-item' }];
const wrapper = mount(
<UploadList listType="picture-card" items={items} locale={{ uploading: 'uploading' }} />,
);
expect(wrapper.find('.ant-upload-list-item-uploading-text').length).toBe(1);
expect(wrapper.find('.ant-upload-list-item-uploading-text').text()).toBe('uploading');
});
it('onPreview should be called, when url exists', () => {
const onPreview = jest.fn();
const items = [{ thumbUrl: 'thumbUrl', url: 'url', uid: 'upload-list-item' }];
const wrapper = mount(
<UploadList
listType="picture-card"
items={items}
locale={{ uploading: 'uploading' }}
onPreview={onPreview}
/>,
);
wrapper.find('.ant-upload-list-item-thumbnail').simulate('click');
expect(onPreview).toBeCalled();
wrapper.find('.ant-upload-list-item-name').simulate('click');
expect(onPreview).toBeCalled();
wrapper.setProps({ items: [{ thumbUrl: 'thumbUrl', uid: 'upload-list-item' }] });
wrapper.find('.ant-upload-list-item-name').simulate('click');
expect(onPreview).toBeCalled();
});
});