2017-01-24 14:59:39 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import Upload from '..';
|
2019-03-07 12:38:14 +08:00
|
|
|
import UploadList from '../UploadList';
|
2017-11-29 20:28:19 +08:00
|
|
|
import Form from '../../form';
|
2019-05-06 12:04:39 +08:00
|
|
|
import { spyElementPrototypes } from '../../__tests__/util/domHook';
|
2017-10-15 17:30:50 +08:00
|
|
|
import { errorRequest, successRequest } from './requests';
|
2018-05-31 22:37:09 +08:00
|
|
|
import { setup, teardown } from './mock';
|
2017-01-24 14:59:39 +08:00
|
|
|
|
2017-08-20 15:39:02 +08:00
|
|
|
const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
const fileList = [
|
|
|
|
{
|
|
|
|
uid: '-1',
|
|
|
|
name: 'xxx.png',
|
|
|
|
status: 'done',
|
|
|
|
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
|
thumbUrl: 'https://zos.alipayobjects.com/rmsportal/IQKRngzUuFzJzGzRJXUs.png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
uid: '-2',
|
|
|
|
name: 'yyy.png',
|
|
|
|
status: 'done',
|
|
|
|
url: 'https://zos.alipayobjects.com/rmsportal/IQKRngzUuFzJzGzRJXUs.png',
|
|
|
|
thumbUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
|
},
|
|
|
|
];
|
2017-01-24 14:59:39 +08:00
|
|
|
|
|
|
|
describe('Upload List', () => {
|
2019-05-06 12:04:39 +08:00
|
|
|
// jsdom not support `createObjectURL` yet. Let's handle this.
|
|
|
|
const originCreateObjectURL = window.URL.createObjectURL;
|
|
|
|
window.URL.createObjectURL = jest.fn(() => '');
|
|
|
|
|
|
|
|
// Mock dom
|
|
|
|
let size = { width: 0, height: 0 };
|
|
|
|
function setSize(width, height) {
|
|
|
|
size = { width, height };
|
|
|
|
}
|
|
|
|
const imageSpy = spyElementPrototypes(Image, {
|
|
|
|
src: {
|
|
|
|
set() {
|
|
|
|
if (this.onload) {
|
|
|
|
this.onload();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
width: {
|
|
|
|
get: () => size.width,
|
|
|
|
},
|
|
|
|
height: {
|
|
|
|
get: () => size.height,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
let drawImageCallback = null;
|
|
|
|
function hookDrawImageCall(callback) {
|
|
|
|
drawImageCallback = callback;
|
|
|
|
}
|
|
|
|
const canvasSpy = spyElementPrototypes(HTMLCanvasElement, {
|
|
|
|
getContext: () => ({
|
|
|
|
drawImage: (...args) => {
|
|
|
|
if (drawImageCallback) drawImageCallback(...args);
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
|
|
|
|
toDataURL: () => 'data:image/png;base64,',
|
|
|
|
});
|
|
|
|
|
|
|
|
// HTMLCanvasElement.prototype
|
|
|
|
|
2018-05-31 22:37:09 +08:00
|
|
|
beforeEach(() => setup());
|
2019-05-06 12:04:39 +08:00
|
|
|
afterEach(() => {
|
|
|
|
teardown();
|
|
|
|
drawImageCallback = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
window.URL.createObjectURL = originCreateObjectURL;
|
|
|
|
imageSpy.mockRestore();
|
|
|
|
canvasSpy.mockRestore();
|
|
|
|
});
|
2018-05-31 22:37:09 +08:00
|
|
|
|
2017-01-24 14:59:39 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/4653
|
|
|
|
it('should use file.thumbUrl for <img /> in priority', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Upload defaultFileList={fileList} listType="picture">
|
2018-06-22 21:05:13 +08:00
|
|
|
<button type="button">upload</button>
|
2018-12-07 16:17:45 +08:00
|
|
|
</Upload>,
|
2017-01-24 14:59:39 +08:00
|
|
|
);
|
|
|
|
fileList.forEach((file, i) => {
|
|
|
|
const linkNode = wrapper.find('.ant-upload-list-item-thumbnail').at(i);
|
|
|
|
const imgNode = wrapper.find('.ant-upload-list-item-thumbnail img').at(i);
|
|
|
|
expect(linkNode.prop('href')).toBe(file.url);
|
|
|
|
expect(imgNode.prop('src')).toBe(file.thumbUrl);
|
|
|
|
});
|
|
|
|
});
|
2017-08-20 15:39:02 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/7269
|
|
|
|
it('should remove correct item when uid is 0', async () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const list = [
|
|
|
|
{
|
|
|
|
uid: '0',
|
|
|
|
name: 'xxx.png',
|
|
|
|
status: 'done',
|
|
|
|
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
|
thumbUrl: 'https://zos.alipayobjects.com/rmsportal/IQKRngzUuFzJzGzRJXUs.png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
uid: '1',
|
|
|
|
name: 'xxx.png',
|
|
|
|
status: 'done',
|
|
|
|
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
|
thumbUrl: 'https://zos.alipayobjects.com/rmsportal/IQKRngzUuFzJzGzRJXUs.png',
|
|
|
|
},
|
|
|
|
];
|
2017-08-20 15:39:02 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<Upload defaultFileList={list}>
|
2018-06-22 21:05:13 +08:00
|
|
|
<button type="button">upload</button>
|
2018-12-07 16:17:45 +08:00
|
|
|
</Upload>,
|
2017-08-20 15:39:02 +08:00
|
|
|
);
|
|
|
|
expect(wrapper.find('.ant-upload-list-item').length).toBe(2);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-upload-list-item')
|
|
|
|
.at(0)
|
|
|
|
.find('.anticon-close')
|
|
|
|
.simulate('click');
|
2017-08-20 15:39:02 +08:00
|
|
|
await delay(400);
|
2017-09-20 16:26:18 +08:00
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.ant-upload-list-item').hostNodes().length).toBe(1);
|
2017-08-20 15:39:02 +08:00
|
|
|
});
|
2017-10-10 15:59:13 +08:00
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
it('should be uploading when upload a file', done => {
|
2017-10-12 14:09:17 +08:00
|
|
|
let wrapper;
|
|
|
|
const onChange = ({ file }) => {
|
|
|
|
if (file.status === 'uploading') {
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
}
|
2017-10-15 17:30:50 +08:00
|
|
|
if (file.status === 'done') {
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
wrapper = mount(
|
|
|
|
<Upload
|
|
|
|
action="http://jsonplaceholder.typicode.com/posts/"
|
|
|
|
onChange={onChange}
|
|
|
|
customRequest={successRequest}
|
|
|
|
>
|
2018-06-22 21:05:13 +08:00
|
|
|
<button type="button">upload</button>
|
2018-12-07 16:17:45 +08:00
|
|
|
</Upload>,
|
2017-10-15 17:30:50 +08:00
|
|
|
);
|
|
|
|
wrapper.find('input').simulate('change', {
|
|
|
|
target: {
|
2018-12-07 16:17:45 +08:00
|
|
|
files: [{ name: 'foo.png' }],
|
2017-10-15 17:30:50 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
it('handle error', done => {
|
2017-10-15 17:30:50 +08:00
|
|
|
let wrapper;
|
|
|
|
const onChange = ({ file }) => {
|
|
|
|
if (file.status !== 'uploading') {
|
2017-10-12 14:09:17 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
wrapper = mount(
|
2017-10-15 17:30:50 +08:00
|
|
|
<Upload
|
|
|
|
action="http://jsonplaceholder.typicode.com/posts/"
|
|
|
|
onChange={onChange}
|
|
|
|
customRequest={errorRequest}
|
|
|
|
>
|
2018-06-22 21:05:13 +08:00
|
|
|
<button type="button">upload</button>
|
2018-12-07 16:17:45 +08:00
|
|
|
</Upload>,
|
2017-10-10 15:59:13 +08:00
|
|
|
);
|
|
|
|
wrapper.find('input').simulate('change', {
|
|
|
|
target: {
|
2018-12-07 16:17:45 +08:00
|
|
|
files: [{ name: 'foo.png' }],
|
2017-10-10 15:59:13 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2017-11-29 20:28:19 +08:00
|
|
|
|
2018-02-11 16:11:13 +08:00
|
|
|
it('does concat filelist when beforeUpload returns false', () => {
|
2017-11-29 20:28:19 +08:00
|
|
|
const handleChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Upload
|
|
|
|
listType="picture"
|
|
|
|
defaultFileList={fileList}
|
|
|
|
onChange={handleChange}
|
|
|
|
beforeUpload={() => false}
|
|
|
|
>
|
2018-06-22 21:05:13 +08:00
|
|
|
<button type="button">upload</button>
|
2018-12-07 16:17:45 +08:00
|
|
|
</Upload>,
|
2017-11-29 20:28:19 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
wrapper.find('input').simulate('change', {
|
|
|
|
target: {
|
2018-12-07 16:17:45 +08:00
|
|
|
files: [{ name: 'foo.png' }],
|
2017-11-29 20:28:19 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-02-11 16:11:13 +08:00
|
|
|
expect(wrapper.state().fileList.length).toBe(fileList.length + 1);
|
|
|
|
expect(handleChange.mock.calls[0][0].fileList).toHaveLength(3);
|
2017-11-29 20:28:19 +08:00
|
|
|
});
|
|
|
|
|
2017-12-04 15:59:23 +08:00
|
|
|
it('should support onPreview', () => {
|
|
|
|
const handlePreview = jest.fn();
|
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Upload listType="picture-card" defaultFileList={fileList} onPreview={handlePreview}>
|
2018-06-22 21:05:13 +08:00
|
|
|
<button type="button">upload</button>
|
2018-12-07 16:17:45 +08:00
|
|
|
</Upload>,
|
2017-12-04 15:59:23 +08:00
|
|
|
);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find('.anticon-eye-o')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handlePreview).toHaveBeenCalledWith(fileList[0]);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find('.anticon-eye-o')
|
|
|
|
.at(1)
|
|
|
|
.simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handlePreview).toHaveBeenCalledWith(fileList[1]);
|
2017-12-04 15:59:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support onRemove', async () => {
|
|
|
|
const handleRemove = jest.fn();
|
|
|
|
const handleChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Upload
|
|
|
|
listType="picture-card"
|
|
|
|
defaultFileList={fileList}
|
|
|
|
onRemove={handleRemove}
|
|
|
|
onChange={handleChange}
|
|
|
|
>
|
2018-06-22 21:05:13 +08:00
|
|
|
<button type="button">upload</button>
|
2018-12-07 16:17:45 +08:00
|
|
|
</Upload>,
|
2017-12-04 15:59:23 +08:00
|
|
|
);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find('.anticon-delete')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleRemove).toHaveBeenCalledWith(fileList[0]);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find('.anticon-delete')
|
|
|
|
.at(1)
|
|
|
|
.simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleRemove).toHaveBeenCalledWith(fileList[1]);
|
2017-12-04 15:59:23 +08:00
|
|
|
await delay(0);
|
|
|
|
expect(handleChange.mock.calls.length).toBe(2);
|
|
|
|
});
|
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
describe('should generate thumbUrl from file', () => {
|
|
|
|
[
|
|
|
|
{ width: 100, height: 200, name: 'height large than width' },
|
|
|
|
{ width: 200, height: 100, name: 'width large than height' },
|
|
|
|
].forEach(({ width, height, name }) => {
|
|
|
|
it(name, async () => {
|
|
|
|
setSize(width, height);
|
|
|
|
const onDrawImage = jest.fn();
|
|
|
|
hookDrawImageCall(onDrawImage);
|
|
|
|
|
|
|
|
const handlePreview = jest.fn();
|
|
|
|
const newFileList = [...fileList];
|
|
|
|
const newFile = {
|
|
|
|
...fileList[0],
|
|
|
|
uid: '-3',
|
|
|
|
originFileObj: new File([], 'xxx.png', { type: 'image/png' }),
|
|
|
|
};
|
|
|
|
delete newFile.thumbUrl;
|
|
|
|
newFileList.push(newFile);
|
|
|
|
const wrapper = mount(
|
|
|
|
<Upload listType="picture-card" defaultFileList={newFileList} onPreview={handlePreview}>
|
|
|
|
<button type="button">upload</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
|
|
|
wrapper.setState({});
|
|
|
|
await delay(0);
|
|
|
|
|
|
|
|
expect(wrapper.state().fileList[2].thumbUrl).not.toBe(undefined);
|
|
|
|
expect(onDrawImage).toHaveBeenCalled();
|
|
|
|
|
|
|
|
// Offset check
|
|
|
|
const [, offsetX, offsetY] = onDrawImage.mock.calls[0];
|
|
|
|
if (width > height) {
|
|
|
|
expect(offsetX < 0).toBeTruthy();
|
|
|
|
} else {
|
|
|
|
expect(offsetY < 0).toBeTruthy();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-12-04 15:59:23 +08:00
|
|
|
});
|
2018-03-11 22:24:09 +08:00
|
|
|
|
|
|
|
it('should non-image format file preview', () => {
|
|
|
|
const list = [
|
|
|
|
{
|
2018-04-04 17:17:01 +08:00
|
|
|
name: 'not-image',
|
|
|
|
status: 'done',
|
2018-08-02 17:34:55 +08:00
|
|
|
uid: '-3',
|
2018-03-11 22:24:09 +08:00
|
|
|
url: 'https://cdn.xxx.com/aaa.zip',
|
|
|
|
thumbUrl: 'data:application/zip;base64,UEsDBAoAAAAAADYZYkwAAAAAAAAAAAAAAAAdAAk',
|
|
|
|
originFileObj: new File([], 'aaa.zip'),
|
|
|
|
},
|
2018-04-04 17:17:01 +08:00
|
|
|
{
|
|
|
|
name: 'image',
|
|
|
|
status: 'done',
|
2018-08-02 17:34:55 +08:00
|
|
|
uid: '-4',
|
2018-04-04 17:17:01 +08:00
|
|
|
url: 'https://cdn.xxx.com/aaa',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'not-image',
|
|
|
|
status: 'done',
|
2018-08-02 17:34:55 +08:00
|
|
|
uid: '-5',
|
2018-04-04 17:17:01 +08:00
|
|
|
url: 'https://cdn.xxx.com/aaa.xx',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'not-image',
|
|
|
|
status: 'done',
|
2018-08-02 17:34:55 +08:00
|
|
|
uid: '-6',
|
2018-04-04 17:17:01 +08:00
|
|
|
url: 'https://cdn.xxx.com/aaa.png/xx.xx',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'image',
|
|
|
|
status: 'done',
|
2018-08-02 17:34:55 +08:00
|
|
|
uid: '-7',
|
2018-04-04 17:17:01 +08:00
|
|
|
url: 'https://cdn.xxx.com/xx.xx/aaa.png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'image',
|
|
|
|
status: 'done',
|
2018-08-02 17:34:55 +08:00
|
|
|
uid: '-8',
|
2018-04-04 17:17:01 +08:00
|
|
|
url: 'https://cdn.xxx.com/xx.xx/aaa.png',
|
|
|
|
thumbUrl: 'data:image/png;base64,UEsDBAoAAAAAADYZYkwAAAAAAAAAAAAAAAAdAAk',
|
|
|
|
},
|
2018-04-17 13:49:03 +08:00
|
|
|
{
|
|
|
|
name: 'image',
|
|
|
|
status: 'done',
|
2018-08-02 17:34:55 +08:00
|
|
|
uid: '-9',
|
2018-04-17 13:49:03 +08:00
|
|
|
url: 'https://cdn.xxx.com/xx.xx/aaa.png?query=123',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'image',
|
|
|
|
status: 'done',
|
2018-08-02 17:34:55 +08:00
|
|
|
uid: '-10',
|
2018-04-17 13:49:03 +08:00
|
|
|
url: 'https://cdn.xxx.com/xx.xx/aaa.png#anchor',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'image',
|
|
|
|
status: 'done',
|
2018-08-02 17:34:55 +08:00
|
|
|
uid: '-11',
|
2018-04-17 13:49:03 +08:00
|
|
|
url: 'https://cdn.xxx.com/xx.xx/aaa.png?query=some.query.with.dot',
|
|
|
|
},
|
2018-08-09 20:30:02 +08:00
|
|
|
{
|
|
|
|
name: 'image',
|
|
|
|
status: 'done',
|
|
|
|
uid: '-12',
|
2018-12-07 16:17:45 +08:00
|
|
|
url:
|
|
|
|
'https://publish-pic-cpu.baidu.com/1296beb3-50d9-4276-885f-52645cbb378e.jpeg@w_228%2ch_152',
|
2019-03-18 10:47:17 +08:00
|
|
|
type: 'image/png',
|
2018-08-09 20:30:02 +08:00
|
|
|
},
|
2018-03-11 22:24:09 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Upload listType="picture" defaultFileList={list}>
|
2018-06-22 21:05:13 +08:00
|
|
|
<button type="button">upload</button>
|
2018-12-07 16:17:45 +08:00
|
|
|
</Upload>,
|
2018-03-11 22:24:09 +08:00
|
|
|
);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
2018-10-11 15:11:44 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/7762
|
|
|
|
it('work with form validation', () => {
|
|
|
|
let errors;
|
|
|
|
class TestForm extends React.Component {
|
|
|
|
handleSubmit = () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const {
|
|
|
|
form: { validateFields },
|
|
|
|
} = this.props;
|
|
|
|
validateFields(err => {
|
2018-10-11 15:11:44 +08:00
|
|
|
errors = err;
|
|
|
|
});
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2018-10-11 15:11:44 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 16:17:45 +08:00
|
|
|
const {
|
|
|
|
form: { getFieldDecorator },
|
|
|
|
} = this.props;
|
2018-10-11 15:11:44 +08:00
|
|
|
return (
|
|
|
|
<Form onSubmit={this.handleSubmit}>
|
|
|
|
<Form.Item>
|
|
|
|
{getFieldDecorator('file', {
|
|
|
|
valuePropname: 'fileList',
|
|
|
|
getValueFromEvent: e => e.fileList,
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
required: true,
|
|
|
|
validator: (rule, value, callback) => {
|
|
|
|
if (!value || value.length === 0) {
|
|
|
|
callback('file required');
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Upload beforeUpload={() => false}>
|
2018-10-11 15:11:44 +08:00
|
|
|
<button type="button">upload</button>
|
2018-12-07 16:17:45 +08:00
|
|
|
</Upload>,
|
2018-10-11 15:11:44 +08:00
|
|
|
)}
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const App = Form.create()(TestForm);
|
|
|
|
const wrapper = mount(<App />);
|
|
|
|
wrapper.find(Form).simulate('submit');
|
|
|
|
expect(errors.file.errors).toEqual([{ message: 'file required', field: 'file' }]);
|
|
|
|
|
|
|
|
wrapper.find('input').simulate('change', {
|
|
|
|
target: {
|
2018-12-07 16:17:45 +08:00
|
|
|
files: [{ name: 'foo.png' }],
|
2018-10-11 15:11:44 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
wrapper.find(Form).simulate('submit');
|
|
|
|
expect(errors).toBeNull();
|
|
|
|
});
|
2019-03-07 09:57:20 +08:00
|
|
|
|
|
|
|
it('return when prop onPreview not exists', () => {
|
|
|
|
const wrapper = mount(<UploadList />).instance();
|
|
|
|
expect(wrapper.handlePreview()).toBe(undefined);
|
|
|
|
});
|
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
it('previewFile should work correctly', async () => {
|
2019-03-07 09:57:20 +08:00
|
|
|
const file = new File([''], 'test.txt', { type: 'text/plain' });
|
2019-03-07 12:38:14 +08:00
|
|
|
const items = [{ uid: 'upload-list-item', url: '' }];
|
|
|
|
const wrapper = mount(
|
|
|
|
<UploadList listType="picture-card" items={items} locale={{ previewFile: '' }} />,
|
|
|
|
).instance();
|
2019-05-06 12:04:39 +08:00
|
|
|
return wrapper.props.previewFile(file);
|
2019-03-07 09:57:20 +08:00
|
|
|
});
|
|
|
|
|
2019-03-07 12:38:14 +08:00
|
|
|
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);
|
2019-03-07 09:57:20 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onPreview).toHaveBeenCalled();
|
2019-03-07 09:57:20 +08:00
|
|
|
wrapper.find('.ant-upload-list-item-name').simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onPreview).toHaveBeenCalled();
|
2019-03-07 09:57:20 +08:00
|
|
|
wrapper.setProps({ items: [{ thumbUrl: 'thumbUrl', uid: 'upload-list-item' }] });
|
|
|
|
wrapper.find('.ant-upload-list-item-name').simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onPreview).toHaveBeenCalled();
|
2019-03-07 09:57:20 +08:00
|
|
|
});
|
2019-03-18 10:47:17 +08:00
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
it('upload image file should be converted to the base64', async () => {
|
2019-03-18 10:47:17 +08:00
|
|
|
const mockFile = new File([''], 'foo.png', {
|
|
|
|
type: 'image/png',
|
|
|
|
});
|
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
<UploadList listType="picture-card" items={fileList} locale={{ uploading: 'uploading' }} />,
|
|
|
|
);
|
|
|
|
const instance = wrapper.instance();
|
2019-05-06 12:04:39 +08:00
|
|
|
return instance.props.previewFile(mockFile).then(dataUrl => {
|
2019-03-18 10:47:17 +08:00
|
|
|
expect(dataUrl).toEqual('data:image/png;base64,');
|
2019-05-06 12:04:39 +08:00
|
|
|
});
|
2019-03-18 10:47:17 +08:00
|
|
|
});
|
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
it("upload non image file shouldn't be converted to the base64", async () => {
|
2019-03-18 10:47:17 +08:00
|
|
|
const mockFile = new File([''], 'foo.7z', {
|
|
|
|
type: 'application/x-7z-compressed',
|
|
|
|
});
|
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
<UploadList listType="picture-card" items={fileList} locale={{ uploading: 'uploading' }} />,
|
|
|
|
);
|
|
|
|
const instance = wrapper.instance();
|
2019-05-06 12:04:39 +08:00
|
|
|
return instance.props.previewFile(mockFile).then(dataUrl => {
|
|
|
|
expect(dataUrl).toBe('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('customize previewFile support', async () => {
|
|
|
|
const mockThumbnail = 'mock-image';
|
|
|
|
const previewFile = jest.fn(() => {
|
|
|
|
return Promise.resolve(mockThumbnail);
|
|
|
|
});
|
|
|
|
const file = {
|
|
|
|
...fileList[0],
|
|
|
|
originFileObj: new File([], 'xxx.png'),
|
|
|
|
};
|
|
|
|
delete file.thumbUrl;
|
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
<Upload listType="picture" defaultFileList={[file]} previewFile={previewFile}>
|
|
|
|
<button type="button" />
|
|
|
|
</Upload>,
|
|
|
|
);
|
|
|
|
wrapper.setState({});
|
|
|
|
await delay(0);
|
|
|
|
|
|
|
|
expect(previewFile).toHaveBeenCalledWith(file.originFileObj);
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
expect(wrapper.find('.ant-upload-list-item-thumbnail img').prop('src')).toBe(mockThumbnail);
|
2019-03-18 10:47:17 +08:00
|
|
|
});
|
2017-01-24 14:59:39 +08:00
|
|
|
});
|