Browse Source

Merge pull request #15241 from zy410419243/issue-14775

pref: lift up coverage rate
pull/15279/head
偏右 6 years ago
committed by GitHub
parent
commit
d4140713ab
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      components/modal/__tests__/Modal.test.js
  2. 28
      components/time-picker/__tests__/index.test.js
  3. 7
      components/time-picker/index.tsx
  4. 26
      components/upload/UploadList.tsx
  5. 54
      components/upload/__tests__/uploadlist.test.js

14
components/modal/__tests__/Modal.test.js

@ -43,4 +43,18 @@ describe('Modal', () => {
const wrapper = mount(<ModalTester footer={null} />);
expect(wrapper.render()).toMatchSnapshot();
});
it('onCancel should be called', () => {
const onCancel = jest.fn();
const wrapper = mount(<Modal onCancel={onCancel} />).instance();
wrapper.handleCancel();
expect(onCancel).toBeCalled();
});
it('onOk should be called', () => {
const onOk = jest.fn();
const wrapper = mount(<Modal onOk={onOk} />).instance();
wrapper.handleOk();
expect(onOk).toBeCalled();
});
});

28
components/time-picker/__tests__/index.test.js

@ -40,4 +40,32 @@ describe('TimePicker', () => {
);
expect(wrapper.render()).toMatchSnapshot();
});
it('handleChange should work correctly', done => {
const date = moment('2000-01-01 00:00:00');
const onChange = (value, formattedValue) => {
expect(value).toBe(date);
expect(formattedValue).toBe(date.format('HH:mm:ss'));
done();
};
const wrapper = mount(<TimePicker onChange={onChange} />).instance();
wrapper.handleChange(date);
});
it('handleOpenClose should work correctly', done => {
const onOpenChange = open => {
expect(open).toBe(true);
done();
};
const wrapper = mount(<TimePicker onOpenChange={onOpenChange} />).instance();
wrapper.handleOpenClose({ open: true });
});
it('clearIcon should render correctly', () => {
const clearIcon = <div className="test-clear-icon">test</div>;
const wrapper = mount(<TimePicker clearIcon={clearIcon} />);
expect(wrapper.find('Picker').prop('clearIcon')).toEqual(
<div className="test-clear-icon ant-time-picker-clear">test</div>,
);
});
});

7
components/time-picker/index.tsx

@ -154,13 +154,10 @@ class TimePicker extends React.Component<TimePickerProps, any> {
renderInputIcon(prefixCls: string) {
const { suffixIcon } = this.props;
const clockIcon = (suffixIcon &&
(React.isValidElement<{ className?: string }>(suffixIcon) ? (
(React.isValidElement<{ className?: string }>(suffixIcon) &&
React.cloneElement(suffixIcon, {
className: classNames(suffixIcon.props.className, `${prefixCls}-clock-icon`),
})
) : (
<span className={`${prefixCls}-clock-icon`}>{suffixIcon}</span>
))) || <Icon type="clock-circle" className={`${prefixCls}-clock-icon`} />;
}))) || <Icon type="clock-circle" className={`${prefixCls}-clock-icon`} />;
return <span className={`${prefixCls}-icon`}>{clockIcon}</span>;
}

26
components/upload/UploadList.tsx

@ -8,16 +8,6 @@ import Progress from '../progress';
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) => {
if (file.type && !imageTypes.includes(file.type)) {
callback('');
}
const reader = new FileReader();
reader.onloadend = () => callback(reader.result);
reader.readAsDataURL(file);
};
const extname = (url: string) => {
if (!url) {
return '';
@ -72,6 +62,16 @@ export default class UploadList extends React.Component<UploadListProps, any> {
return onPreview(file);
};
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
previewFile = (file: File | Blob, callback: Function) => {
if (file.type && !imageTypes.includes(file.type)) {
callback('');
}
const reader = new FileReader();
reader.onloadend = () => callback(reader.result);
reader.readAsDataURL(file);
};
componentDidUpdate() {
if (this.props.listType !== 'picture' && this.props.listType !== 'picture-card') {
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 */
this.previewFile(file.originFileObj, (previewDataUrl: string) => {
file.thumbUrl = previewDataUrl;
/*eslint-enable */
this.forceUpdate();
});
});

54
components/upload/__tests__/uploadlist.test.js

@ -1,6 +1,7 @@
import React from 'react';
import { mount } from 'enzyme';
import Upload from '..';
import UploadList from '../UploadList';
import Form from '../../form';
import { errorRequest, successRequest } from './requests';
import { setup, teardown } from './mock';
@ -349,4 +350,57 @@ 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' });
const items = [{ uid: 'upload-list-item', url: '' }];
const wrapper = mount(
<UploadList listType="picture-card" items={items} locale={{ previewFile: '' }} />,
).instance();
wrapper.previewFile(file, callback);
expect(callback).toBeCalled();
});
it('extname should work correctly when url not exists', () => {
const items = [{ uid: 'upload-list-item', url: '' }];
const wrapper = mount(
<UploadList listType="picture-card" items={items} locale={{ previewFile: '' }} />,
);
expect(wrapper.find('.ant-upload-list-item-thumbnail').length).toBe(2);
});
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();
});
});

Loading…
Cancel
Save