2017-01-24 14:59:39 +08:00
|
|
|
import React from 'react';
|
2020-08-29 21:28:44 +08:00
|
|
|
import { act } from 'react-dom/test-utils';
|
2017-01-24 14:59:39 +08:00
|
|
|
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';
|
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';
|
2019-07-31 18:06:31 +08:00
|
|
|
import { sleep } from '../../../tests/utils';
|
2017-08-20 15:39:02 +08:00
|
|
|
|
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', () => {
|
2020-09-07 16:41:28 +08:00
|
|
|
// Mock for rc-util raf
|
|
|
|
window.requestAnimationFrame = callback => {
|
|
|
|
window.setTimeout(callback, 16);
|
|
|
|
};
|
|
|
|
window.cancelAnimationFrame = id => {
|
|
|
|
window.clearTimeout(id);
|
|
|
|
};
|
|
|
|
|
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 };
|
|
|
|
}
|
2020-04-22 11:59:56 +08:00
|
|
|
const mockWidthGet = jest.spyOn(Image.prototype, 'width', 'get');
|
|
|
|
const mockHeightGet = jest.spyOn(Image.prototype, 'height', 'get');
|
|
|
|
const mockSrcSet = jest.spyOn(Image.prototype, 'src', 'set');
|
2019-05-06 12:04:39 +08:00
|
|
|
|
|
|
|
let drawImageCallback = null;
|
|
|
|
function hookDrawImageCall(callback) {
|
|
|
|
drawImageCallback = callback;
|
|
|
|
}
|
2020-04-22 11:59:56 +08:00
|
|
|
const mockGetCanvasContext = jest.spyOn(HTMLCanvasElement.prototype, 'getContext');
|
|
|
|
const mockToDataURL = jest.spyOn(HTMLCanvasElement.prototype, 'toDataURL');
|
2019-05-06 12:04:39 +08:00
|
|
|
|
|
|
|
// HTMLCanvasElement.prototype
|
|
|
|
|
2018-05-31 22:37:09 +08:00
|
|
|
beforeEach(() => setup());
|
2019-05-06 12:04:39 +08:00
|
|
|
afterEach(() => {
|
|
|
|
teardown();
|
|
|
|
drawImageCallback = null;
|
|
|
|
});
|
|
|
|
|
2020-04-01 17:38:21 +08:00
|
|
|
let open;
|
|
|
|
beforeAll(() => {
|
|
|
|
open = jest.spyOn(window, 'open').mockImplementation(() => {});
|
2020-04-22 11:59:56 +08:00
|
|
|
mockWidthGet.mockImplementation(() => size.width);
|
|
|
|
mockHeightGet.mockImplementation(() => size.height);
|
|
|
|
mockSrcSet.mockImplementation(function fn() {
|
|
|
|
if (this.onload) {
|
|
|
|
this.onload();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mockGetCanvasContext.mockReturnValue({
|
|
|
|
drawImage: (...args) => {
|
|
|
|
if (drawImageCallback) drawImageCallback(...args);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
mockToDataURL.mockReturnValue('data:image/png;base64,');
|
2020-04-01 17:38:21 +08:00
|
|
|
});
|
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
afterAll(() => {
|
|
|
|
window.URL.createObjectURL = originCreateObjectURL;
|
2020-04-22 11:59:56 +08:00
|
|
|
mockWidthGet.mockRestore();
|
|
|
|
mockHeightGet.mockRestore();
|
|
|
|
mockSrcSet.mockRestore();
|
|
|
|
mockGetCanvasContext.mockRestore();
|
|
|
|
mockToDataURL.mockRestore();
|
2020-04-01 17:38:21 +08:00
|
|
|
open.mockRestore();
|
2019-05-06 12:04:39 +08:00
|
|
|
});
|
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);
|
|
|
|
});
|
2021-02-26 15:18:37 +08:00
|
|
|
wrapper.unmount();
|
2017-01-24 14:59:39 +08:00
|
|
|
});
|
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);
|
2020-04-01 17:38:21 +08:00
|
|
|
wrapper.find('.ant-upload-list-item').at(0).find('.anticon-delete').simulate('click');
|
2020-11-21 19:00:11 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(1000);
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
const domNode = wrapper.find('.ant-upload-list-text-container').at(0).hostNodes().instance();
|
|
|
|
const transitionEndEvent = new Event('transitionend');
|
|
|
|
domNode.dispatchEvent(transitionEndEvent);
|
|
|
|
wrapper.update();
|
|
|
|
});
|
|
|
|
|
|
|
|
// console.log(wrapper.html());
|
|
|
|
|
|
|
|
expect(wrapper.find('.ant-upload-list-text-container').hostNodes().length).toBe(1);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
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;
|
2020-03-18 00:02:23 +08:00
|
|
|
let latestFileList = null;
|
|
|
|
const onChange = ({ file, fileList: eventFileList }) => {
|
|
|
|
expect(eventFileList === latestFileList).toBeFalsy();
|
2017-10-12 14:09:17 +08:00
|
|
|
if (file.status === 'uploading') {
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
}
|
2017-10-15 17:30:50 +08:00
|
|
|
if (file.status === 'done') {
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2021-02-26 15:18:37 +08:00
|
|
|
wrapper.unmount();
|
2017-10-15 17:30:50 +08:00
|
|
|
done();
|
|
|
|
}
|
2020-03-18 00:02:23 +08:00
|
|
|
|
|
|
|
latestFileList = eventFileList;
|
2017-10-15 17:30:50 +08:00
|
|
|
};
|
|
|
|
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
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-26 22:38:11 +08:00
|
|
|
it('handle error', async () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const 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
|
|
|
},
|
|
|
|
});
|
2021-02-26 22:38:11 +08:00
|
|
|
|
|
|
|
// Wait twice since `errorRequest` also use timeout for mock
|
2021-03-29 15:49:28 +08:00
|
|
|
await act(async () => {
|
|
|
|
await sleep();
|
|
|
|
await sleep();
|
|
|
|
});
|
2021-02-26 22:38:11 +08:00
|
|
|
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
file: expect.objectContaining({
|
|
|
|
status: 'error',
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2021-03-29 15:49:28 +08:00
|
|
|
await act(async () => {
|
|
|
|
await sleep(1000);
|
|
|
|
wrapper.update();
|
|
|
|
});
|
|
|
|
|
2021-02-26 22:38:11 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
|
|
|
|
// Error message
|
|
|
|
jest.useFakeTimers();
|
|
|
|
wrapper.find('.ant-upload-list-item').simulate('mouseEnter');
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
|
|
|
|
expect(wrapper.find('Trigger').state().popupVisible).toBeTruthy();
|
|
|
|
|
|
|
|
wrapper.unmount();
|
2017-10-10 15:59:13 +08:00
|
|
|
});
|
2017-11-29 20:28:19 +08:00
|
|
|
|
2021-02-26 15:18:37 +08:00
|
|
|
it('does concat fileList when beforeUpload returns false', async () => {
|
2017-11-29 20:28:19 +08:00
|
|
|
const handleChange = jest.fn();
|
2020-08-18 15:44:31 +08:00
|
|
|
const ref = React.createRef();
|
2017-11-29 20:28:19 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<Upload
|
2020-08-18 15:44:31 +08:00
|
|
|
ref={ref}
|
2017-11-29 20:28:19 +08:00
|
|
|
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
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-02-26 15:18:37 +08:00
|
|
|
await sleep();
|
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
expect(ref.current.fileList.length).toBe(fileList.length + 1);
|
2018-02-11 16:11:13 +08:00
|
|
|
expect(handleChange.mock.calls[0][0].fileList).toHaveLength(3);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2017-11-29 20:28:19 +08:00
|
|
|
});
|
|
|
|
|
2019-09-11 21:46:56 +08:00
|
|
|
it('In the case of listType=picture, the error status does not show the download.', () => {
|
2021-02-26 15:18:37 +08:00
|
|
|
global.testName =
|
|
|
|
'In the case of listType=picture, the error status does not show the download.';
|
2019-09-11 21:46:56 +08:00
|
|
|
const file = { status: 'error', uid: 'file' };
|
|
|
|
const wrapper = mount(
|
2020-02-20 22:47:14 +08:00
|
|
|
<Upload listType="picture" fileList={[file]} showUploadList={{ showDownloadIcon: true }}>
|
2019-09-11 21:46:56 +08:00
|
|
|
<button type="button">upload</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
2020-11-21 19:00:11 +08:00
|
|
|
|
|
|
|
// Has error item className
|
|
|
|
wrapper.find('.ant-upload-list-item-error').simulate('mouseenter');
|
|
|
|
|
2019-09-11 21:46:56 +08:00
|
|
|
expect(wrapper.find('div.ant-upload-list-item i.anticon-download').length).toBe(0);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2019-09-11 21:46:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('In the case of listType=picture-card, the error status does not show the download.', () => {
|
2021-02-26 15:18:37 +08:00
|
|
|
global.testName =
|
|
|
|
'In the case of listType=picture-card, the error status does not show the download.';
|
2019-09-11 21:46:56 +08:00
|
|
|
const file = { status: 'error', uid: 'file' };
|
|
|
|
const wrapper = mount(
|
2020-02-20 22:47:14 +08:00
|
|
|
<Upload listType="picture-card" fileList={[file]} showUploadList={{ showDownloadIcon: true }}>
|
2019-09-11 21:46:56 +08:00
|
|
|
<button type="button">upload</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
|
|
|
expect(wrapper.find('div.ant-upload-list-item i.anticon-download').length).toBe(0);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2019-09-11 21:46:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('In the case of listType=text, the error status does not show the download.', () => {
|
|
|
|
const file = { status: 'error', uid: 'file' };
|
|
|
|
const wrapper = mount(
|
2020-02-20 22:47:14 +08:00
|
|
|
<Upload listType="text" fileList={[file]} showUploadList={{ showDownloadIcon: true }}>
|
2019-09-11 21:46:56 +08:00
|
|
|
<button type="button">upload</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
|
|
|
expect(wrapper.find('div.ant-upload-list-item i.anticon-download').length).toBe(0);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2019-09-11 21:46:56 +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
|
|
|
);
|
2020-04-01 17:38:21 +08:00
|
|
|
wrapper.find('.anticon-eye').at(0).simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handlePreview).toHaveBeenCalledWith(fileList[0]);
|
2020-04-01 17:38:21 +08:00
|
|
|
wrapper.find('.anticon-eye').at(1).simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handlePreview).toHaveBeenCalledWith(fileList[1]);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
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
|
|
|
);
|
2020-04-01 17:38:21 +08:00
|
|
|
wrapper.find('.anticon-delete').at(0).simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleRemove).toHaveBeenCalledWith(fileList[0]);
|
2020-04-01 17:38:21 +08:00
|
|
|
wrapper.find('.anticon-delete').at(1).simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleRemove).toHaveBeenCalledWith(fileList[1]);
|
2019-07-31 18:06:31 +08:00
|
|
|
await sleep();
|
2017-12-04 15:59:23 +08:00
|
|
|
expect(handleChange.mock.calls.length).toBe(2);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2017-12-04 15:59:23 +08:00
|
|
|
});
|
|
|
|
|
2019-09-11 21:46:56 +08:00
|
|
|
it('should support onDownload', async () => {
|
|
|
|
const handleDownload = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Upload
|
|
|
|
listType="picture-card"
|
|
|
|
defaultFileList={[
|
|
|
|
{
|
|
|
|
uid: '0',
|
|
|
|
name: 'xxx.png',
|
|
|
|
status: 'done',
|
|
|
|
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
onDownload={handleDownload}
|
2020-02-20 22:47:14 +08:00
|
|
|
showUploadList={{
|
|
|
|
showDownloadIcon: true,
|
|
|
|
}}
|
2019-09-11 21:46:56 +08:00
|
|
|
>
|
|
|
|
<button type="button">upload</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
2020-04-01 17:38:21 +08:00
|
|
|
wrapper.find('.anticon-download').at(0).simulate('click');
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2019-09-11 21:46:56 +08:00
|
|
|
});
|
|
|
|
|
2020-02-20 22:47:14 +08:00
|
|
|
it('should support no onDownload', async () => {
|
2019-09-11 21:46:56 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<Upload
|
|
|
|
listType="picture-card"
|
|
|
|
defaultFileList={[
|
|
|
|
{
|
|
|
|
uid: '0',
|
|
|
|
name: 'xxx.png',
|
|
|
|
status: 'done',
|
|
|
|
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
|
},
|
|
|
|
]}
|
2020-02-20 22:47:14 +08:00
|
|
|
showUploadList={{
|
|
|
|
showDownloadIcon: true,
|
|
|
|
}}
|
2019-09-11 21:46:56 +08:00
|
|
|
>
|
|
|
|
<button type="button">upload</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
2020-04-01 17:38:21 +08:00
|
|
|
wrapper.find('.anticon-download').at(0).simulate('click');
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2019-09-11 21:46:56 +08:00
|
|
|
});
|
|
|
|
|
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);
|
2020-08-18 15:44:31 +08:00
|
|
|
const ref = React.createRef();
|
2021-02-26 15:18:37 +08:00
|
|
|
const wrapper = mount(
|
2020-08-18 15:44:31 +08:00
|
|
|
<Upload
|
|
|
|
ref={ref}
|
|
|
|
listType="picture-card"
|
|
|
|
defaultFileList={newFileList}
|
|
|
|
onPreview={handlePreview}
|
|
|
|
>
|
2019-05-06 12:04:39 +08:00
|
|
|
<button type="button">upload</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
2021-02-26 15:18:37 +08:00
|
|
|
wrapper.update();
|
2019-07-31 18:06:31 +08:00
|
|
|
await sleep();
|
2019-05-06 12:04:39 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
expect(ref.current.fileList[2].thumbUrl).not.toBe(undefined);
|
2019-05-06 12:04:39 +08:00
|
|
|
expect(onDrawImage).toHaveBeenCalled();
|
|
|
|
|
|
|
|
// Offset check
|
|
|
|
const [, offsetX, offsetY] = onDrawImage.mock.calls[0];
|
|
|
|
if (width > height) {
|
2020-10-23 11:18:57 +08:00
|
|
|
expect(offsetX === 0).toBeTruthy();
|
2019-05-06 12:04:39 +08:00
|
|
|
} else {
|
2020-10-23 11:18:57 +08:00
|
|
|
expect(offsetY === 0).toBeTruthy();
|
2019-05-06 12:04:39 +08:00
|
|
|
}
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2019-05-06 12:04:39 +08:00
|
|
|
});
|
|
|
|
});
|
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();
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2018-03-11 22:24:09 +08:00
|
|
|
});
|
2018-10-11 15:11:44 +08:00
|
|
|
|
2021-02-26 22:38:11 +08:00
|
|
|
it('not crash when uploading not provides percent', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
<Upload
|
|
|
|
listType="picture"
|
|
|
|
defaultFileList={[
|
|
|
|
{
|
|
|
|
name: 'bamboo.png',
|
|
|
|
status: 'uploading',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
wrapper.unmount();
|
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2019-09-29 12:33:24 +08:00
|
|
|
it('should support showRemoveIcon and showPreviewIcon', () => {
|
|
|
|
const list = [
|
|
|
|
{
|
|
|
|
name: 'image',
|
|
|
|
status: 'uploading',
|
|
|
|
uid: '-4',
|
|
|
|
url: 'https://cdn.xxx.com/aaa',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'image',
|
|
|
|
status: 'done',
|
|
|
|
uid: '-5',
|
|
|
|
url: 'https://cdn.xxx.com/aaa',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
<Upload
|
|
|
|
listType="picture"
|
|
|
|
defaultFileList={list}
|
|
|
|
showUploadList={{
|
|
|
|
showRemoveIcon: false,
|
|
|
|
showPreviewIcon: false,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<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();
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2018-03-11 22:24:09 +08:00
|
|
|
});
|
2021-01-16 18:06:26 +08:00
|
|
|
|
2020-02-17 14:55:16 +08:00
|
|
|
it('should support custom onClick in custom icon', async () => {
|
|
|
|
const handleRemove = jest.fn();
|
|
|
|
const handleChange = jest.fn();
|
|
|
|
const myClick = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Upload
|
|
|
|
listType="picture-card"
|
|
|
|
defaultFileList={fileList}
|
|
|
|
onRemove={handleRemove}
|
|
|
|
onChange={handleChange}
|
|
|
|
showUploadList={{
|
|
|
|
showRemoveIcon: true,
|
2020-02-19 13:20:43 +08:00
|
|
|
removeIcon: (
|
|
|
|
<i className="custom-delete" onClick={myClick}>
|
|
|
|
RM
|
|
|
|
</i>
|
|
|
|
),
|
2020-02-17 14:55:16 +08:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<button type="button">upload</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
2020-04-01 17:38:21 +08:00
|
|
|
wrapper.find('.custom-delete').at(0).simulate('click');
|
2020-02-17 14:55:16 +08:00
|
|
|
expect(handleRemove).toHaveBeenCalledWith(fileList[0]);
|
|
|
|
expect(myClick).toHaveBeenCalled();
|
2020-04-01 17:38:21 +08:00
|
|
|
wrapper.find('.custom-delete').at(1).simulate('click');
|
2020-02-17 14:55:16 +08:00
|
|
|
expect(handleRemove).toHaveBeenCalledWith(fileList[1]);
|
|
|
|
expect(myClick).toHaveBeenCalled();
|
|
|
|
await sleep();
|
|
|
|
expect(handleChange.mock.calls.length).toBe(2);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2020-02-17 14:55:16 +08:00
|
|
|
});
|
2020-10-10 13:32:27 +08:00
|
|
|
|
2020-02-17 14:55:16 +08:00
|
|
|
it('should support removeIcon and downloadIcon', () => {
|
|
|
|
const list = [
|
|
|
|
{
|
|
|
|
name: 'image',
|
|
|
|
status: 'uploading',
|
|
|
|
uid: '-4',
|
|
|
|
url: 'https://cdn.xxx.com/aaa',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'image',
|
|
|
|
status: 'done',
|
|
|
|
uid: '-5',
|
|
|
|
url: 'https://cdn.xxx.com/aaa',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
<Upload
|
|
|
|
listType="picture"
|
|
|
|
defaultFileList={list}
|
|
|
|
showUploadList={{
|
|
|
|
showRemoveIcon: true,
|
|
|
|
showDownloadIcon: true,
|
2020-09-13 08:31:18 +08:00
|
|
|
removeIcon: () => <i>RM</i>,
|
2020-02-17 14:55:16 +08:00
|
|
|
downloadIcon: <i>DL</i>,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<button type="button">upload</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2020-10-10 13:32:27 +08:00
|
|
|
|
|
|
|
const wrapper2 = mount(
|
|
|
|
<Upload
|
|
|
|
listType="picture"
|
|
|
|
defaultFileList={list}
|
|
|
|
showUploadList={{
|
|
|
|
showRemoveIcon: true,
|
|
|
|
showDownloadIcon: true,
|
|
|
|
removeIcon: <i>RM</i>,
|
|
|
|
downloadIcon: () => <i>DL</i>,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<button type="button">upload</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
|
|
|
expect(wrapper2.render()).toMatchSnapshot();
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
|
|
|
wrapper2.unmount();
|
2020-02-17 14:55:16 +08:00
|
|
|
});
|
2018-10-11 15:11:44 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/7762
|
2019-07-03 20:14:39 +08:00
|
|
|
it('work with form validation', async () => {
|
|
|
|
let formRef;
|
|
|
|
|
|
|
|
const TestForm = () => {
|
|
|
|
const [form] = Form.useForm();
|
|
|
|
formRef = form;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form form={form}>
|
|
|
|
<Form.Item
|
|
|
|
name="file"
|
|
|
|
valuePropName="fileList"
|
|
|
|
getValueFromEvent={e => e.fileList}
|
|
|
|
rules={[
|
|
|
|
{
|
|
|
|
required: true,
|
|
|
|
validator: (rule, value, callback) => {
|
|
|
|
if (!value || value.length === 0) {
|
|
|
|
callback('file required');
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<Upload beforeUpload={() => false}>
|
|
|
|
<button type="button">upload</button>
|
|
|
|
</Upload>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = mount(<TestForm />);
|
2018-10-11 15:11:44 +08:00
|
|
|
|
|
|
|
wrapper.find(Form).simulate('submit');
|
2019-08-29 23:41:46 +08:00
|
|
|
await sleep();
|
2019-07-03 20:14:39 +08:00
|
|
|
expect(formRef.getFieldError(['file'])).toEqual(['file required']);
|
2018-10-11 15:11:44 +08:00
|
|
|
|
|
|
|
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
|
|
|
},
|
|
|
|
});
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2018-10-11 15:11:44 +08:00
|
|
|
wrapper.find(Form).simulate('submit');
|
2019-08-29 23:41:46 +08:00
|
|
|
await sleep();
|
2019-07-03 20:14:39 +08:00
|
|
|
expect(formRef.getFieldError(['file'])).toEqual([]);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2018-10-11 15:11:44 +08:00
|
|
|
});
|
2019-03-07 09:57:20 +08:00
|
|
|
|
|
|
|
it('return when prop onPreview not exists', () => {
|
2020-08-18 15:44:31 +08:00
|
|
|
const ref = React.createRef();
|
2021-02-26 15:18:37 +08:00
|
|
|
const wrapper = mount(<UploadList ref={ref} />);
|
2020-08-18 15:44:31 +08:00
|
|
|
expect(ref.current.handlePreview()).toBe(undefined);
|
2021-02-26 15:18:37 +08:00
|
|
|
wrapper.unmount();
|
2019-03-07 09:57:20 +08:00
|
|
|
});
|
|
|
|
|
2019-09-11 21:46:56 +08:00
|
|
|
it('return when prop onDownload not exists', () => {
|
|
|
|
const file = new File([''], 'test.txt', { type: 'text/plain' });
|
|
|
|
const items = [{ uid: 'upload-list-item', url: '' }];
|
2020-08-18 15:44:31 +08:00
|
|
|
const ref = React.createRef();
|
2021-02-26 15:18:37 +08:00
|
|
|
const wrapper = mount(
|
2020-02-20 22:47:14 +08:00
|
|
|
<UploadList
|
2020-08-18 15:44:31 +08:00
|
|
|
ref={ref}
|
2020-02-20 22:47:14 +08:00
|
|
|
items={items}
|
|
|
|
locale={{ downloadFile: '' }}
|
|
|
|
showUploadList={{ showDownloadIcon: true }}
|
2020-02-24 13:10:29 +08:00
|
|
|
/>,
|
2020-08-18 15:44:31 +08:00
|
|
|
);
|
|
|
|
expect(ref.current.handleDownload(file)).toBe(undefined);
|
2021-02-26 15:18:37 +08:00
|
|
|
wrapper.unmount();
|
2019-09-11 21:46:56 +08:00
|
|
|
});
|
|
|
|
|
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: '' }} />,
|
2020-08-18 15:44:31 +08:00
|
|
|
);
|
2021-02-26 15:18:37 +08:00
|
|
|
expect(wrapper.props().previewFile(file)).toBeTruthy();
|
|
|
|
|
|
|
|
wrapper.unmount();
|
2019-03-07 09:57:20 +08:00
|
|
|
});
|
|
|
|
|
2019-09-11 21:46:56 +08:00
|
|
|
it('downloadFile should work correctly', async () => {
|
|
|
|
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}
|
|
|
|
onDownload={() => {}}
|
|
|
|
locale={{ downloadFile: '' }}
|
2020-02-20 22:47:14 +08:00
|
|
|
showUploadList={{ showDownloadIcon: true }}
|
2019-09-11 21:46:56 +08:00
|
|
|
/>,
|
2020-08-18 15:44:31 +08:00
|
|
|
);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
// Not throw
|
|
|
|
wrapper.props().onDownload(file);
|
|
|
|
|
|
|
|
wrapper.unmount();
|
2019-09-11 21:46:56 +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: '' }} />,
|
|
|
|
);
|
2020-01-03 17:48:10 +08:00
|
|
|
expect(wrapper.find('.ant-upload-list-item-thumbnail').length).toBe(1);
|
2021-02-26 15:18:37 +08:00
|
|
|
wrapper.unmount();
|
2019-03-07 09:57:20 +08:00
|
|
|
});
|
|
|
|
|
2020-02-24 13:10:29 +08:00
|
|
|
it('extname should work correctly when url exists', done => {
|
2019-09-11 21:46:56 +08:00
|
|
|
const items = [{ status: 'done', uid: 'upload-list-item', url: '/example' }];
|
|
|
|
const wrapper = mount(
|
|
|
|
<UploadList
|
|
|
|
listType="picture"
|
|
|
|
onDownload={file => {
|
|
|
|
expect(file.url).toBe('/example');
|
2021-02-26 15:18:37 +08:00
|
|
|
wrapper.unmount();
|
2020-02-20 22:47:14 +08:00
|
|
|
done();
|
2019-09-11 21:46:56 +08:00
|
|
|
}}
|
|
|
|
items={items}
|
|
|
|
locale={{ downloadFile: '' }}
|
2020-02-20 22:47:14 +08:00
|
|
|
showDownloadIcon
|
2019-09-11 21:46:56 +08:00
|
|
|
/>,
|
|
|
|
);
|
2019-10-16 10:44:56 +08:00
|
|
|
wrapper.find('div.ant-upload-list-item .anticon-download').simulate('click');
|
2019-09-11 21:46:56 +08:00
|
|
|
});
|
|
|
|
|
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' }} />,
|
|
|
|
);
|
2020-01-03 17:48:10 +08:00
|
|
|
expect(wrapper.find('.ant-upload-list-item-thumbnail').length).toBe(1);
|
|
|
|
expect(wrapper.find('.ant-upload-list-item-thumbnail').text()).toBe('uploading');
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2019-03-07 09:57:20 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
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' }} />,
|
|
|
|
);
|
2021-02-26 15:18:37 +08:00
|
|
|
await wrapper
|
2020-08-18 15:44:31 +08:00
|
|
|
.props()
|
|
|
|
.previewFile(mockFile)
|
|
|
|
.then(dataUrl => {
|
|
|
|
expect(dataUrl).toEqual('data:image/png;base64,');
|
|
|
|
});
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
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' }} />,
|
|
|
|
);
|
2021-02-26 15:18:37 +08:00
|
|
|
await wrapper
|
2020-08-18 15:44:31 +08:00
|
|
|
.props()
|
|
|
|
.previewFile(mockFile)
|
|
|
|
.then(dataUrl => {
|
|
|
|
expect(dataUrl).toBe('');
|
|
|
|
});
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2019-05-06 12:04:39 +08:00
|
|
|
});
|
|
|
|
|
2019-07-01 17:52:37 +08:00
|
|
|
describe('customize previewFile support', () => {
|
|
|
|
function test(name, renderInstance) {
|
|
|
|
it(name, async () => {
|
|
|
|
const mockThumbnail = 'mock-image';
|
2020-12-09 17:12:32 +08:00
|
|
|
const previewFile = jest.fn(() => Promise.resolve(mockThumbnail));
|
2019-07-01 17:52:37 +08:00
|
|
|
const file = {
|
|
|
|
...fileList[0],
|
|
|
|
originFileObj: renderInstance(),
|
|
|
|
};
|
|
|
|
delete file.thumbUrl;
|
2020-08-18 15:44:31 +08:00
|
|
|
const ref = React.createRef();
|
2019-07-01 17:52:37 +08:00
|
|
|
const wrapper = mount(
|
2020-08-18 15:44:31 +08:00
|
|
|
<Upload ref={ref} listType="picture" defaultFileList={[file]} previewFile={previewFile}>
|
2019-08-11 16:38:04 +08:00
|
|
|
<button type="button">button</button>
|
2019-07-01 17:52:37 +08:00
|
|
|
</Upload>,
|
|
|
|
);
|
2021-02-26 15:18:37 +08:00
|
|
|
wrapper.update();
|
2019-07-01 17:52:37 +08:00
|
|
|
expect(previewFile).toHaveBeenCalledWith(file.originFileObj);
|
2020-08-18 15:44:31 +08:00
|
|
|
await sleep(100);
|
2019-07-01 17:52:37 +08:00
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
expect(wrapper.find('.ant-upload-list-item-thumbnail img').prop('src')).toBe(mockThumbnail);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2019-07-01 17:52:37 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
test('File', () => new File([], 'xxx.png'));
|
|
|
|
test('Blob', () => new Blob());
|
2019-03-18 10:47:17 +08:00
|
|
|
});
|
2019-08-04 12:18:46 +08:00
|
|
|
|
2020-04-14 19:32:16 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/22958
|
|
|
|
describe('customize isImageUrl support', () => {
|
|
|
|
const list = [
|
|
|
|
...fileList,
|
|
|
|
{
|
|
|
|
uid: '0',
|
|
|
|
name: 'xxx.png',
|
|
|
|
status: 'done',
|
|
|
|
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
|
thumbUrl:
|
|
|
|
'http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg@!panda_style?spm=a2c4g.11186623.2.17.4dc56b29BHokyg&file=example.jpg@!panda_style',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
it('should not render <img /> when file.thumbUrl use "!" as separator', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Upload listType="picture-card" fileList={list}>
|
|
|
|
<button type="button">button</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
|
|
|
const imgNode = wrapper.find('.ant-upload-list-item-thumbnail img');
|
|
|
|
expect(imgNode.length).toBe(2);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2020-04-14 19:32:16 +08:00
|
|
|
});
|
|
|
|
it('should render <img /> when custom imageUrl return true', () => {
|
2020-12-09 17:12:32 +08:00
|
|
|
const isImageUrl = jest.fn(() => true);
|
2020-04-14 19:32:16 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<Upload listType="picture-card" fileList={list} isImageUrl={isImageUrl}>
|
|
|
|
<button type="button">button</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
|
|
|
const imgNode = wrapper.find('.ant-upload-list-item-thumbnail img');
|
|
|
|
expect(isImageUrl).toHaveBeenCalled();
|
|
|
|
expect(imgNode.length).toBe(3);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2020-04-14 19:32:16 +08:00
|
|
|
});
|
|
|
|
it('should not render <img /> when custom imageUrl return false', () => {
|
2020-12-09 17:12:32 +08:00
|
|
|
const isImageUrl = jest.fn(() => false);
|
2020-04-14 19:32:16 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<Upload listType="picture-card" fileList={list} isImageUrl={isImageUrl}>
|
|
|
|
<button type="button">button</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
|
|
|
const imgNode = wrapper.find('.ant-upload-list-item-thumbnail img');
|
|
|
|
expect(isImageUrl).toHaveBeenCalled();
|
|
|
|
expect(imgNode.length).toBe(0);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2020-04-14 19:32:16 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-06 21:47:01 +08:00
|
|
|
describe('thumbUrl support for non-image', () => {
|
2020-09-08 14:47:31 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2020-07-06 21:47:01 +08:00
|
|
|
const nonImageFile = new File([''], 'foo.7z', { type: 'application/x-7z-compressed' });
|
2020-09-08 14:47:31 +08:00
|
|
|
|
|
|
|
/** Wait for a long promise since `rc-util` internal has at least 3 promise wait */
|
|
|
|
async function waitPromise() {
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
for (let i = 0; i < 10; i += 1) {
|
|
|
|
await Promise.resolve();
|
|
|
|
}
|
|
|
|
/* eslint-enable */
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should render <img /> when upload non-image file and configure thumbUrl in onChange', async () => {
|
2020-07-06 21:47:01 +08:00
|
|
|
let wrapper;
|
2020-09-08 14:47:31 +08:00
|
|
|
const onChange = jest.fn(({ fileList: files }) => {
|
2020-08-29 21:28:44 +08:00
|
|
|
const newFileList = files.map(item => ({
|
2020-07-06 21:47:01 +08:00
|
|
|
...item,
|
|
|
|
thumbUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
|
}));
|
|
|
|
|
2020-08-29 21:28:44 +08:00
|
|
|
wrapper.setProps({ fileList: newFileList });
|
2020-09-08 14:47:31 +08:00
|
|
|
});
|
2020-08-29 21:28:44 +08:00
|
|
|
|
2020-07-06 21:47:01 +08:00
|
|
|
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');
|
2020-09-08 14:47:31 +08:00
|
|
|
expect(imgNode.length).toBeFalsy();
|
|
|
|
|
|
|
|
// Simulate change is a timeout change
|
2020-07-06 21:47:01 +08:00
|
|
|
wrapper.find('input').simulate('change', { target: { files: [nonImageFile] } });
|
2020-09-08 14:47:31 +08:00
|
|
|
|
|
|
|
// Wait for `rc-upload` process file
|
|
|
|
await waitPromise();
|
|
|
|
|
|
|
|
// Wait for mock request finish request
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
// Basic called times
|
|
|
|
expect(onChange).toHaveBeenCalled();
|
|
|
|
|
|
|
|
// Check for images
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.update();
|
|
|
|
});
|
|
|
|
const afterImgNode = wrapper.find('.ant-upload-list-item-thumbnail img');
|
|
|
|
expect(afterImgNode.length).toBeTruthy();
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2020-07-06 21:47:01 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not render <img /> when upload non-image file without thumbUrl in onChange', done => {
|
2021-02-26 15:18:37 +08:00
|
|
|
global.testName =
|
|
|
|
'should not render <img /> when upload non-image file without thumbUrl in onChange';
|
2020-07-06 21:47:01 +08:00
|
|
|
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);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
2020-07-06 21:47:01 +08:00
|
|
|
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] } });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-12-15 15:56:35 +08:00
|
|
|
it('[deprecated] should support transformFile', done => {
|
2021-02-26 15:18:37 +08:00
|
|
|
let wrapper;
|
2021-03-04 16:59:06 +08:00
|
|
|
let lastFile;
|
2021-02-26 15:18:37 +08:00
|
|
|
|
2019-08-04 12:18:46 +08:00
|
|
|
const handleTransformFile = jest.fn();
|
|
|
|
const onChange = ({ file }) => {
|
|
|
|
if (file.status === 'done') {
|
2021-03-04 16:59:06 +08:00
|
|
|
expect(file).not.toBe(lastFile);
|
2019-08-04 12:18:46 +08:00
|
|
|
expect(handleTransformFile).toHaveBeenCalled();
|
2021-02-26 15:18:37 +08:00
|
|
|
wrapper.unmount();
|
2019-08-04 12:18:46 +08:00
|
|
|
done();
|
|
|
|
}
|
2021-03-04 16:59:06 +08:00
|
|
|
|
|
|
|
lastFile = file;
|
2019-08-04 12:18:46 +08:00
|
|
|
};
|
2021-02-26 15:18:37 +08:00
|
|
|
wrapper = mount(
|
2019-08-04 12:18:46 +08:00
|
|
|
<Upload
|
|
|
|
action="http://jsonplaceholder.typicode.com/posts/"
|
|
|
|
transformFile={handleTransformFile}
|
|
|
|
onChange={onChange}
|
|
|
|
customRequest={successRequest}
|
|
|
|
>
|
|
|
|
<button type="button">upload</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
|
|
|
wrapper.find('input').simulate('change', {
|
|
|
|
target: {
|
|
|
|
files: [{ name: 'foo.png' }],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2020-08-24 16:33:24 +08:00
|
|
|
|
|
|
|
it('should render button inside UploadList when listStyle is picture-card', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Upload
|
|
|
|
action="http://jsonplaceholder.typicode.com/posts/"
|
|
|
|
listType="picture-card"
|
|
|
|
fileList={[
|
|
|
|
{
|
|
|
|
uid: '0',
|
|
|
|
name: 'xxx.png',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
showUploadList
|
|
|
|
>
|
|
|
|
<button className="trigger" type="button">
|
|
|
|
upload
|
|
|
|
</button>
|
|
|
|
</Upload>,
|
|
|
|
);
|
|
|
|
expect(wrapper.exists('.ant-upload-list button.trigger')).toBe(true);
|
|
|
|
wrapper.setProps({ showUploadList: false });
|
|
|
|
expect(wrapper.exists('.ant-upload-list button.trigger')).toBe(false);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
2020-08-24 16:33:24 +08:00
|
|
|
});
|
2020-09-07 16:41:28 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/26536
|
|
|
|
it('multiple file upload should keep the internal fileList async', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
const uploadRef = React.createRef();
|
|
|
|
|
|
|
|
const MyUpload = () => {
|
|
|
|
const [testFileList, setTestFileList] = React.useState([]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Upload
|
|
|
|
ref={uploadRef}
|
|
|
|
fileList={testFileList}
|
|
|
|
action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
|
|
|
|
multiple
|
|
|
|
onChange={info => {
|
|
|
|
setTestFileList([...info.fileList]);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<button type="button">Upload</button>
|
|
|
|
</Upload>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-02-26 15:18:37 +08:00
|
|
|
const wrapper = mount(<MyUpload />);
|
2020-09-07 16:41:28 +08:00
|
|
|
|
|
|
|
// Mock async update in a frame
|
2021-03-13 23:46:32 +08:00
|
|
|
const fileNames = ['light', 'bamboo', 'little'];
|
2020-09-07 16:41:28 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
act(() => {
|
|
|
|
uploadRef.current.onBatchStart(
|
|
|
|
fileNames.map(fileName => {
|
|
|
|
const file = new File([], fileName);
|
|
|
|
file.uid = fileName;
|
|
|
|
|
|
|
|
return {
|
|
|
|
file,
|
|
|
|
parsedFile: file,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2020-09-07 16:41:28 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
expect(uploadRef.current.fileList).toHaveLength(fileNames.length);
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
expect(uploadRef.current.fileList).toHaveLength(fileNames.length);
|
2020-09-07 16:41:28 +08:00
|
|
|
|
2021-02-26 15:18:37 +08:00
|
|
|
wrapper.unmount();
|
|
|
|
|
2020-09-07 16:41:28 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2020-09-17 16:14:42 +08:00
|
|
|
|
|
|
|
it('itemRender', () => {
|
2021-05-06 14:12:26 +08:00
|
|
|
const onDownload = jest.fn();
|
|
|
|
const onRemove = jest.fn();
|
|
|
|
const onPreview = jest.fn();
|
|
|
|
const itemRender = (originNode, file, currFileList, actions) => {
|
2020-09-17 16:14:42 +08:00
|
|
|
const { name, status, uid, url } = file;
|
|
|
|
const index = currFileList.indexOf(file);
|
|
|
|
return (
|
2021-05-06 14:12:26 +08:00
|
|
|
<div className="custom-item-render">
|
|
|
|
<span>
|
|
|
|
{`uid:${uid} name: ${name} status: ${status} url: ${url} ${index + 1}/${
|
|
|
|
currFileList.length
|
|
|
|
}`}
|
|
|
|
</span>
|
|
|
|
<span onClick={actions.remove} className="custom-item-render-action-remove">
|
|
|
|
remove
|
|
|
|
</span>
|
|
|
|
<span onClick={actions.download} className="custom-item-render-action-download">
|
|
|
|
download
|
|
|
|
</span>
|
|
|
|
<span onClick={actions.preview} className="custom-item-render-action-preview">
|
|
|
|
preview
|
|
|
|
</span>
|
|
|
|
</div>
|
2020-09-17 16:14:42 +08:00
|
|
|
);
|
|
|
|
};
|
2021-05-06 14:12:26 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<UploadList
|
|
|
|
onDownload={onDownload}
|
|
|
|
onPreview={onPreview}
|
|
|
|
onRemove={onRemove}
|
|
|
|
locale={{}}
|
|
|
|
items={fileList}
|
|
|
|
itemRender={itemRender}
|
|
|
|
/>,
|
|
|
|
);
|
2020-09-17 16:14:42 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2021-02-26 15:18:37 +08:00
|
|
|
|
2021-05-06 14:12:26 +08:00
|
|
|
wrapper.find('.custom-item-render-action-remove').first().simulate('click');
|
|
|
|
expect(onRemove.mock.calls[0][0]).toEqual(fileList[0]);
|
|
|
|
|
|
|
|
wrapper.find('.custom-item-render-action-download').first().simulate('click');
|
|
|
|
expect(onDownload.mock.calls[0][0]).toEqual(fileList[0]);
|
|
|
|
|
|
|
|
wrapper.find('.custom-item-render-action-preview').first().simulate('click');
|
|
|
|
expect(onPreview.mock.calls[0][0]).toEqual(fileList[0]);
|
|
|
|
|
2021-02-26 15:18:37 +08:00
|
|
|
wrapper.unmount();
|
2020-09-17 16:14:42 +08:00
|
|
|
});
|
2021-02-26 22:38:11 +08:00
|
|
|
|
|
|
|
it('LIST_IGNORE should not add in list', async () => {
|
|
|
|
const beforeUpload = jest.fn(() => Upload.LIST_IGNORE);
|
|
|
|
const wrapper = mount(<Upload beforeUpload={beforeUpload} />);
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
wrapper.find('input').simulate('change', {
|
|
|
|
target: {
|
|
|
|
files: [{ file: 'foo.png' }],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await sleep();
|
|
|
|
|
|
|
|
expect(beforeUpload).toHaveBeenCalled();
|
|
|
|
expect(wrapper.find('UploadList').props().items).toHaveLength(0);
|
|
|
|
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
2021-03-10 17:32:32 +08:00
|
|
|
|
|
|
|
it('Not crash when fileList is null', () => {
|
|
|
|
const defaultWrapper = mount(<Upload defaultFileList={null} />);
|
|
|
|
defaultWrapper.unmount();
|
|
|
|
|
|
|
|
const wrapper = mount(<Upload fileList={null} />);
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
2017-01-24 14:59:39 +08:00
|
|
|
});
|