ant-design/components/upload/__tests__/upload.test.js

351 lines
8.9 KiB
JavaScript
Raw Normal View History

2017-02-22 16:06:08 +08:00
/* eslint-disable react/no-string-refs, react/prefer-es6-class */
import React from 'react';
import { mount } from 'enzyme';
import Upload from '..';
2018-12-07 16:17:45 +08:00
import { T, fileToObject, genPercentAdd, getFileItem, removeFileItem } from '../utils';
2018-05-31 22:37:09 +08:00
import { setup, teardown } from './mock';
2017-02-22 16:06:08 +08:00
describe('Upload', () => {
2018-05-31 22:37:09 +08:00
beforeEach(() => setup());
afterEach(() => teardown());
2017-02-22 16:06:08 +08:00
// https://github.com/react-component/upload/issues/36
it('should get refs inside Upload in componentDidMount', () => {
let ref;
2017-04-30 18:47:15 +08:00
class App extends React.Component {
2017-02-22 16:06:08 +08:00
componentDidMount() {
ref = this.refs.input;
2017-04-27 21:12:23 +08:00
}
2017-02-22 16:06:08 +08:00
render() {
return (
<Upload supportServerRender={false}>
<input ref="input" />
</Upload>
);
2017-04-27 21:12:23 +08:00
}
}
2017-02-22 16:06:08 +08:00
mount(<App />);
expect(ref).toBeDefined();
});
2018-12-07 16:17:45 +08:00
it('return promise in beforeUpload', done => {
const data = jest.fn();
const props = {
action: 'http://upload.com',
2018-12-07 16:17:45 +08:00
beforeUpload: () => new Promise(resolve => setTimeout(() => resolve('success'), 100)),
data,
onChange: ({ file }) => {
if (file.status !== 'uploading') {
expect(data).toBeCalled();
done();
}
},
};
const wrapper = mount(
<Upload {...props}>
<button type="button">upload</button>
2018-12-07 16:17:45 +08:00
</Upload>,
);
wrapper.find('input').simulate('change', {
target: {
files: [{ file: 'foo.png' }],
},
});
});
it('upload promise return file in beforeUpload', done => {
const data = jest.fn();
const props = {
action: 'http://upload.com',
beforeUpload: file =>
new Promise(resolve =>
setTimeout(() => {
const result = file;
result.name = 'test.png';
resolve(result);
}, 100),
),
data,
onChange: ({ file }) => {
if (file.status !== 'uploading') {
expect(data).toBeCalled();
expect(file.name).toEqual('test.png');
done();
}
},
};
const wrapper = mount(
<Upload {...props}>
<button type="button">upload</button>
</Upload>,
);
wrapper.find('input').simulate('change', {
target: {
2018-12-07 16:17:45 +08:00
files: [{ file: 'foo.png' }],
},
});
});
2018-12-07 16:17:45 +08:00
it('should not stop upload when return value of beforeUpload is false', done => {
const fileList = [
{
uid: 'bar',
name: 'bar.png',
},
];
const mockFile = new File(['foo'], 'foo.png', {
type: 'image/png',
});
const data = jest.fn();
const props = {
action: 'http://upload.com',
fileList,
beforeUpload: () => false,
data,
onChange: ({ file, fileList: updatedFileList }) => {
expect(file instanceof File).toBe(true);
expect(updatedFileList.map(f => f.name)).toEqual(['bar.png', 'foo.png']);
expect(data).not.toBeCalled();
done();
},
};
const wrapper = mount(
<Upload {...props}>
<button type="button">upload</button>
2018-12-07 16:17:45 +08:00
</Upload>,
);
wrapper.find('input').simulate('change', {
target: {
2018-12-07 16:17:45 +08:00
files: [mockFile],
},
});
});
2018-12-07 16:17:45 +08:00
it('should increase percent automaticly when call autoUpdateProgress in IE', done => {
let uploadInstance;
let lastPercent = -1;
const props = {
2018-05-31 22:37:09 +08:00
action: 'http://upload.com',
onChange: ({ file }) => {
if (file.percent === 0 && file.status === 'uploading') {
// manually call it
uploadInstance.autoUpdateProgress(0, file);
}
if (file.status === 'uploading') {
expect(file.percent).toBeGreaterThan(lastPercent);
lastPercent = file.percent;
}
if (file.status === 'done' || file.status === 'error') {
done();
}
},
};
const wrapper = mount(
<Upload {...props}>
<button type="button">upload</button>
2018-12-07 16:17:45 +08:00
</Upload>,
);
wrapper.find('input').simulate('change', {
target: {
2018-12-07 16:17:45 +08:00
files: [{ file: 'foo.png' }],
},
});
uploadInstance = wrapper.instance();
});
2018-12-07 16:17:45 +08:00
it('should not stop upload when return value of beforeUpload is not false', done => {
const data = jest.fn();
const props = {
action: 'http://upload.com',
beforeUpload() {},
data,
onChange: () => {
expect(data).toBeCalled();
done();
},
};
const wrapper = mount(
<Upload {...props}>
<button type="button">upload</button>
2018-12-07 16:17:45 +08:00
</Upload>,
);
wrapper.find('input').simulate('change', {
target: {
2018-12-07 16:17:45 +08:00
files: [{ file: 'foo.png' }],
},
});
});
2018-05-02 13:56:54 +08:00
it('should be controlled by fileList', () => {
2018-12-07 16:17:45 +08:00
const fileList = [
{
uid: '-1',
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
},
];
const wrapper = mount(<Upload />);
expect(wrapper.instance().state.fileList).toEqual([]);
wrapper.setProps({ fileList });
expect(wrapper.instance().state.fileList).toEqual(fileList);
});
2018-05-02 13:56:54 +08:00
describe('util', () => {
2018-12-01 18:30:05 +08:00
// https://github.com/react-component/upload/issues/36
it('should T() return true', () => {
const res = T();
expect(res).toBe(true);
});
2018-05-02 13:56:54 +08:00
it('should be able to copy file instance', () => {
const file = new File([], 'aaa.zip');
const copiedFile = fileToObject(file);
2018-12-07 16:17:45 +08:00
['uid', 'lastModified', 'lastModifiedDate', 'name', 'size', 'type'].forEach(key => {
2018-05-02 13:56:54 +08:00
expect(key in copiedFile).toBe(true);
});
});
2018-12-01 18:30:05 +08:00
it('should be able to progress from 0.1 ', () => {
// 0.1 -> 0.98
const getPercent = genPercentAdd();
let curPercent = 0;
curPercent = getPercent(curPercent);
expect(curPercent).toBe(0.1);
});
it('should be able to progress to 0.98 ', () => {
// 0.1 -> 0.98
const getPercent = genPercentAdd();
let curPercent = 0;
for (let i = 0; i < 500; i += 1) {
curPercent = getPercent(curPercent);
}
expect(parseFloat(curPercent.toFixed(2))).toBe(0.98);
});
it('should be able to get fileItem', () => {
const file = { uid: '-1', name: 'item.jpg' };
2018-12-07 16:17:45 +08:00
const fileList = [
{
uid: '-1',
name: 'item.jpg',
},
];
2018-12-01 18:30:05 +08:00
const targetItem = getFileItem(file, fileList);
expect(targetItem).toBe(fileList[0]);
});
it('should be able to remove fileItem', () => {
const file = { uid: '-1', name: 'item.jpg' };
2018-12-07 16:17:45 +08:00
const fileList = [
{
uid: '-1',
name: 'item.jpg',
},
{
uid: '-2',
name: 'item2.jpg',
},
];
2018-12-01 18:30:05 +08:00
const targetItem = removeFileItem(file, fileList);
expect(targetItem).toEqual(fileList.slice(1));
});
it('should not be able to remove fileItem', () => {
const file = { uid: '-3', name: 'item.jpg' };
2018-12-07 16:17:45 +08:00
const fileList = [
{
uid: '-1',
name: 'item.jpg',
},
{
uid: '-2',
name: 'item2.jpg',
},
];
2018-12-01 18:30:05 +08:00
const targetItem = removeFileItem(file, fileList);
expect(targetItem).toBe(null);
});
2018-05-02 13:56:54 +08:00
});
it('should support linkProps as object', () => {
2018-12-07 16:17:45 +08:00
const fileList = [
{
uid: '-1',
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
linkProps: {
download: 'image',
rel: 'noopener',
},
},
2018-12-07 16:17:45 +08:00
];
const wrapper = mount(<Upload fileList={fileList} />);
const linkNode = wrapper.find('a.ant-upload-list-item-name');
expect(linkNode.props().download).toBe('image');
expect(linkNode.props().rel).toBe('noopener');
});
it('should support linkProps as json stringify', () => {
const linkPropsString = JSON.stringify({
download: 'image',
rel: 'noopener',
});
2018-12-07 16:17:45 +08:00
const fileList = [
{
uid: '-1',
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
linkProps: linkPropsString,
},
];
const wrapper = mount(<Upload fileList={fileList} />);
const linkNode = wrapper.find('a.ant-upload-list-item-name');
expect(linkNode.props().download).toBe('image');
expect(linkNode.props().rel).toBe('noopener');
});
2019-01-12 18:08:10 +08:00
2019-01-13 13:37:20 +08:00
it('should not stop remove when return value of onRemove is false', done => {
2019-01-12 18:08:10 +08:00
const mockRemove = jest.fn(() => false);
const props = {
onRemove: mockRemove,
fileList: [
{
uid: '-1',
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
},
],
};
const wrapper = mount(<Upload {...props} />);
wrapper.find('div.ant-upload-list-item i.anticon-close').simulate('click');
2019-01-13 13:37:20 +08:00
setImmediate(() => {
wrapper.update();
2019-01-12 18:08:10 +08:00
2019-01-13 13:37:20 +08:00
expect(mockRemove).toBeCalled();
expect(props.fileList).toHaveLength(1);
expect(props.fileList[0].status).toBe('done');
done();
});
2019-01-12 18:08:10 +08:00
});
2017-02-22 16:06:08 +08:00
});