2017-01-24 14:59:39 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import Upload from '..';
|
2017-10-15 17:30:50 +08:00
|
|
|
import { errorRequest, successRequest } from './requests';
|
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));
|
|
|
|
|
2017-01-24 14:59:39 +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',
|
|
|
|
}];
|
|
|
|
|
|
|
|
describe('Upload List', () => {
|
|
|
|
// 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>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);
|
|
|
|
});
|
|
|
|
});
|
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 () => {
|
|
|
|
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>upload</button>
|
|
|
|
</Upload>
|
|
|
|
);
|
|
|
|
expect(wrapper.find('.ant-upload-list-item').length).toBe(2);
|
|
|
|
wrapper.find('.ant-upload-list-item').at(0).find('.anticon-cross').simulate('click');
|
|
|
|
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
|
|
|
|
2017-10-12 14:09:17 +08:00
|
|
|
it('should be uploading when upload a file', (done) => {
|
|
|
|
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}
|
|
|
|
>
|
|
|
|
<button>upload</button>
|
|
|
|
</Upload>
|
|
|
|
);
|
|
|
|
wrapper.find('input').simulate('change', {
|
|
|
|
target: {
|
|
|
|
files: [
|
|
|
|
{ filename: 'foo.png' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handle error', (done) => {
|
|
|
|
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}
|
|
|
|
>
|
2017-10-10 15:59:13 +08:00
|
|
|
<button>upload</button>
|
|
|
|
</Upload>
|
|
|
|
);
|
|
|
|
wrapper.find('input').simulate('change', {
|
|
|
|
target: {
|
|
|
|
files: [
|
|
|
|
{ filename: 'foo.png' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2017-01-24 14:59:39 +08:00
|
|
|
});
|