You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
564 lines
16 KiB
564 lines
16 KiB
import React from 'react';
|
|
import { mount } from 'enzyme';
|
|
import Upload from '..';
|
|
import UploadList from '../UploadList';
|
|
import Form from '../../form';
|
|
import { spyElementPrototypes } from '../../__tests__/util/domHook';
|
|
import { errorRequest, successRequest } from './requests';
|
|
import { setup, teardown } from './mock';
|
|
import { sleep } from '../../../tests/utils';
|
|
|
|
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',
|
|
},
|
|
];
|
|
|
|
describe('Upload List', () => {
|
|
// 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
|
|
|
|
beforeEach(() => setup());
|
|
afterEach(() => {
|
|
teardown();
|
|
drawImageCallback = null;
|
|
});
|
|
|
|
afterAll(() => {
|
|
window.URL.createObjectURL = originCreateObjectURL;
|
|
imageSpy.mockRestore();
|
|
canvasSpy.mockRestore();
|
|
});
|
|
|
|
// 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">
|
|
<button type="button">upload</button>
|
|
</Upload>,
|
|
);
|
|
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);
|
|
});
|
|
});
|
|
|
|
// https://github.com/ant-design/ant-design/issues/7269
|
|
it('should remove correct item when uid is 0', async () => {
|
|
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',
|
|
},
|
|
];
|
|
const wrapper = mount(
|
|
<Upload defaultFileList={list}>
|
|
<button type="button">upload</button>
|
|
</Upload>,
|
|
);
|
|
expect(wrapper.find('.ant-upload-list-item').length).toBe(2);
|
|
wrapper
|
|
.find('.ant-upload-list-item')
|
|
.at(0)
|
|
.find('.anticon-close')
|
|
.simulate('click');
|
|
await sleep(400);
|
|
wrapper.update();
|
|
expect(wrapper.find('.ant-upload-list-item').hostNodes().length).toBe(1);
|
|
});
|
|
|
|
it('should be uploading when upload a file', done => {
|
|
let wrapper;
|
|
const onChange = ({ file }) => {
|
|
if (file.status === 'uploading') {
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
}
|
|
if (file.status === 'done') {
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
done();
|
|
}
|
|
};
|
|
wrapper = mount(
|
|
<Upload
|
|
action="http://jsonplaceholder.typicode.com/posts/"
|
|
onChange={onChange}
|
|
customRequest={successRequest}
|
|
>
|
|
<button type="button">upload</button>
|
|
</Upload>,
|
|
);
|
|
wrapper.find('input').simulate('change', {
|
|
target: {
|
|
files: [{ name: 'foo.png' }],
|
|
},
|
|
});
|
|
});
|
|
|
|
it('handle error', done => {
|
|
let wrapper;
|
|
const onChange = ({ file }) => {
|
|
if (file.status !== 'uploading') {
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
done();
|
|
}
|
|
};
|
|
wrapper = mount(
|
|
<Upload
|
|
action="http://jsonplaceholder.typicode.com/posts/"
|
|
onChange={onChange}
|
|
customRequest={errorRequest}
|
|
>
|
|
<button type="button">upload</button>
|
|
</Upload>,
|
|
);
|
|
wrapper.find('input').simulate('change', {
|
|
target: {
|
|
files: [{ name: 'foo.png' }],
|
|
},
|
|
});
|
|
});
|
|
|
|
it('does concat filelist when beforeUpload returns false', () => {
|
|
const handleChange = jest.fn();
|
|
const wrapper = mount(
|
|
<Upload
|
|
listType="picture"
|
|
defaultFileList={fileList}
|
|
onChange={handleChange}
|
|
beforeUpload={() => false}
|
|
>
|
|
<button type="button">upload</button>
|
|
</Upload>,
|
|
);
|
|
|
|
wrapper.find('input').simulate('change', {
|
|
target: {
|
|
files: [{ name: 'foo.png' }],
|
|
},
|
|
});
|
|
|
|
expect(wrapper.state().fileList.length).toBe(fileList.length + 1);
|
|
expect(handleChange.mock.calls[0][0].fileList).toHaveLength(3);
|
|
});
|
|
|
|
it('should support onPreview', () => {
|
|
const handlePreview = jest.fn();
|
|
const wrapper = mount(
|
|
<Upload listType="picture-card" defaultFileList={fileList} onPreview={handlePreview}>
|
|
<button type="button">upload</button>
|
|
</Upload>,
|
|
);
|
|
wrapper
|
|
.find('.anticon-eye-o')
|
|
.at(0)
|
|
.simulate('click');
|
|
expect(handlePreview).toHaveBeenCalledWith(fileList[0]);
|
|
wrapper
|
|
.find('.anticon-eye-o')
|
|
.at(1)
|
|
.simulate('click');
|
|
expect(handlePreview).toHaveBeenCalledWith(fileList[1]);
|
|
});
|
|
|
|
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}
|
|
>
|
|
<button type="button">upload</button>
|
|
</Upload>,
|
|
);
|
|
wrapper
|
|
.find('.anticon-delete')
|
|
.at(0)
|
|
.simulate('click');
|
|
expect(handleRemove).toHaveBeenCalledWith(fileList[0]);
|
|
wrapper
|
|
.find('.anticon-delete')
|
|
.at(1)
|
|
.simulate('click');
|
|
expect(handleRemove).toHaveBeenCalledWith(fileList[1]);
|
|
await sleep();
|
|
expect(handleChange.mock.calls.length).toBe(2);
|
|
});
|
|
|
|
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 sleep();
|
|
|
|
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();
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
it('should non-image format file preview', () => {
|
|
const list = [
|
|
{
|
|
name: 'not-image',
|
|
status: 'done',
|
|
uid: '-3',
|
|
url: 'https://cdn.xxx.com/aaa.zip',
|
|
thumbUrl: 'data:application/zip;base64,UEsDBAoAAAAAADYZYkwAAAAAAAAAAAAAAAAdAAk',
|
|
originFileObj: new File([], 'aaa.zip'),
|
|
},
|
|
{
|
|
name: 'image',
|
|
status: 'done',
|
|
uid: '-4',
|
|
url: 'https://cdn.xxx.com/aaa',
|
|
},
|
|
{
|
|
name: 'not-image',
|
|
status: 'done',
|
|
uid: '-5',
|
|
url: 'https://cdn.xxx.com/aaa.xx',
|
|
},
|
|
{
|
|
name: 'not-image',
|
|
status: 'done',
|
|
uid: '-6',
|
|
url: 'https://cdn.xxx.com/aaa.png/xx.xx',
|
|
},
|
|
{
|
|
name: 'image',
|
|
status: 'done',
|
|
uid: '-7',
|
|
url: 'https://cdn.xxx.com/xx.xx/aaa.png',
|
|
},
|
|
{
|
|
name: 'image',
|
|
status: 'done',
|
|
uid: '-8',
|
|
url: 'https://cdn.xxx.com/xx.xx/aaa.png',
|
|
thumbUrl: 'data:image/png;base64,UEsDBAoAAAAAADYZYkwAAAAAAAAAAAAAAAAdAAk',
|
|
},
|
|
{
|
|
name: 'image',
|
|
status: 'done',
|
|
uid: '-9',
|
|
url: 'https://cdn.xxx.com/xx.xx/aaa.png?query=123',
|
|
},
|
|