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

337 lines
8.5 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00:00
import { mount } from '@vue/test-utils';
import Upload from '..';
import { T, fileToObject, genPercentAdd, getFileItem, removeFileItem } from '../utils';
import PropsTypes from '../../_util/vue-types';
import { UploadListProps } from '../interface';
2018-06-02 07:12:53 +00:00
2019-01-12 03:33:27 +00:00
UploadListProps.items = PropsTypes.any;
2018-05-31 08:36:59 +00:00
describe('Upload', () => {
it('should get refs inside Upload in componentDidMount', () => {
2019-01-12 03:33:27 +00:00
let ref = null;
2018-05-31 08:36:59 +00:00
const APP = {
2019-01-12 03:33:27 +00:00
mounted() {
ref = this.$refs.input;
2018-05-31 08:36:59 +00:00
},
2019-01-12 03:33:27 +00:00
render() {
2018-05-31 08:36:59 +00:00
return (
2019-01-12 03:33:27 +00:00
<Upload supportServerRender={false} action="">
<input ref="input" />
2018-05-31 08:36:59 +00:00
</Upload>
2019-01-12 03:33:27 +00:00
);
2018-05-31 08:36:59 +00:00
},
2019-01-12 03:33:27 +00:00
};
mount(APP);
expect(ref).toBeDefined();
});
2018-05-31 08:36:59 +00:00
2019-01-12 03:33:27 +00:00
it('return promise in beforeUpload', done => {
const data = jest.fn();
2018-05-31 08:36:59 +00:00
const props = {
propsData: {
2018-06-16 14:11:34 +00:00
action: 'http://upload.com',
2019-01-12 03:33:27 +00:00
beforeUpload: () => new Promise(resolve => setTimeout(() => resolve('success'), 100)),
2018-05-31 08:36:59 +00:00
data,
},
listeners: {
change: ({ file }) => {
if (file.status !== 'uploading') {
2019-01-12 03:33:27 +00:00
expect(data).toBeCalled();
done();
2018-05-31 08:36:59 +00:00
}
},
},
slots: {
default: '<button>upload</button>',
},
sync: false,
2019-01-12 03:33:27 +00:00
};
const wrapper = mount(Upload, props);
2018-05-31 08:36:59 +00:00
setTimeout(() => {
wrapper.find({ name: 'ajaxUploader' }).vm.onChange({
target: {
2019-01-02 14:00:06 +00:00
files: [{ file: 'foo.png' }],
2018-05-31 08:36:59 +00:00
},
2019-01-12 03:33:27 +00:00
});
}, 0);
});
2018-05-31 08:36:59 +00:00
2019-01-07 12:59:57 +00:00
it('upload promise return file in beforeUpload', done => {
2019-01-12 03:33:27 +00:00
const data = jest.fn();
2019-01-07 12:59:57 +00:00
const props = {
propsData: {
action: 'http://upload.com',
beforeUpload: file =>
new Promise(resolve =>
setTimeout(() => {
2019-01-12 03:33:27 +00:00
const result = file;
result.name = 'test.png';
resolve(result);
2019-01-07 12:59:57 +00:00
}, 100),
),
data,
},
listeners: {
change: ({ file }) => {
if (file.status !== 'uploading') {
2019-01-12 03:33:27 +00:00
expect(data).toBeCalled();
expect(file.name).toEqual('test.png');
done();
2019-01-07 12:59:57 +00:00
}
},
},
slots: {
default: '<button>upload</button>',
},
sync: false,
2019-01-12 03:33:27 +00:00
};
2019-01-07 12:59:57 +00:00
2019-01-12 03:33:27 +00:00
const wrapper = mount(Upload, props);
2019-01-07 12:59:57 +00:00
setTimeout(() => {
wrapper.find({ name: 'ajaxUploader' }).vm.onChange({
target: {
files: [{ file: 'foo.png' }],
},
2019-01-12 03:33:27 +00:00
});
}, 0);
});
2019-01-07 12:59:57 +00:00
2019-01-12 03:33:27 +00:00
it('should not stop upload when return value of beforeUpload is false', done => {
const data = jest.fn();
2018-05-31 08:36:59 +00:00
const props = {
propsData: {
2018-06-16 14:11:34 +00:00
action: 'http://upload.com',
2018-05-31 08:36:59 +00:00
beforeUpload: () => false,
data,
},
listeners: {
change: ({ file }) => {
2019-01-12 03:33:27 +00:00
expect(file instanceof File).toBe(true);
expect(data).not.toBeCalled();
done();
2018-05-31 08:36:59 +00:00
},
},
slots: {
default: '<button>upload</button>',
},
sync: false,
2019-01-12 03:33:27 +00:00
};
const wrapper = mount(Upload, props);
2018-05-31 08:36:59 +00:00
setTimeout(() => {
const mockFile = new File(['foo'], 'foo.png', {
type: 'image/png',
2019-01-12 03:33:27 +00:00
});
2018-05-31 08:36:59 +00:00
wrapper.find({ name: 'ajaxUploader' }).vm.onChange({
target: {
files: [mockFile],
},
2019-01-12 03:33:27 +00:00
});
}, 0);
});
2018-05-31 08:36:59 +00:00
2019-01-12 03:33:27 +00:00
it('should increase percent automaticly when call autoUpdateProgress in IE', done => {
let uploadInstance;
let lastPercent = -1;
2018-05-31 08:36:59 +00:00
const props = {
propsData: {
2018-06-16 14:11:34 +00:00
action: 'http://upload.com',
2018-05-31 08:36:59 +00:00
},
listeners: {
change: ({ file }) => {
if (file.percent === 0 && file.status === 'uploading') {
// manually call it
2019-01-12 03:33:27 +00:00
uploadInstance.autoUpdateProgress(0, file);
2018-05-31 08:36:59 +00:00
}
if (file.status === 'uploading') {
2019-01-12 03:33:27 +00:00
expect(file.percent).toBeGreaterThan(lastPercent);
lastPercent = file.percent;
2018-05-31 08:36:59 +00:00
}
if (file.status === 'done' || file.status === 'error') {
2019-01-12 03:33:27 +00:00
done();
2018-05-31 08:36:59 +00:00
}
},
},
slots: {
default: '<button>upload</button>',
},
sync: false,
2019-01-12 03:33:27 +00:00
};
const wrapper = mount(Upload, props);
2018-05-31 08:36:59 +00:00
setTimeout(() => {
const mockFile = new File(['foo'], 'foo.png', {
type: 'image/png',
2019-01-12 03:33:27 +00:00
});
2018-05-31 08:36:59 +00:00
wrapper.find({ name: 'ajaxUploader' }).vm.onChange({
target: {
files: [mockFile],
},
2019-01-12 03:33:27 +00:00
});
uploadInstance = wrapper.vm;
}, 0);
});
it('should not stop upload when return value of beforeUpload is not false', done => {
const data = jest.fn();
2018-05-31 08:36:59 +00:00
const props = {
propsData: {
2018-06-16 14:11:34 +00:00
action: 'http://upload.com',
2019-01-12 03:33:27 +00:00
beforeUpload() {},
2018-05-31 08:36:59 +00:00
data,
},
listeners: {
change: () => {
2019-01-12 03:33:27 +00:00
expect(data).toBeCalled();
done();
2018-05-31 08:36:59 +00:00
},
},
slots: {
default: '<button>upload</button>',
},
sync: false,
2019-01-12 03:33:27 +00:00
};
2018-05-31 08:36:59 +00:00
2019-01-12 03:33:27 +00:00
const wrapper = mount(Upload, props);
2018-05-31 08:36:59 +00:00
setTimeout(() => {
const mockFile = new File(['foo'], 'foo.png', {
type: 'image/png',
2019-01-12 03:33:27 +00:00
});
2018-05-31 08:36:59 +00:00
wrapper.find({ name: 'ajaxUploader' }).vm.onChange({
target: {
files: [mockFile],
},
2019-01-12 03:33:27 +00:00
});
}, 0);
});
2018-05-31 08:36:59 +00:00
describe('util', () => {
2019-01-07 12:59:57 +00:00
// https://github.com/react-component/upload/issues/36
it('should T() return true', () => {
2019-01-12 03:33:27 +00:00
const res = T();
expect(res).toBe(true);
});
2018-05-31 08:36:59 +00:00
it('should be able to copy file instance', () => {
2019-01-12 03:33:27 +00:00
const file = new File([], 'aaa.zip');
2018-05-31 08:36:59 +00:00
const copiedFile = fileToObject(file);
2019-01-12 03:33:27 +00:00
['uid', 'lastModified', 'lastModifiedDate', 'name', 'size', 'type'].forEach(key => {
expect(key in copiedFile).toBe(true);
});
});
2019-01-07 12:59:57 +00:00
it('should be able to progress from 0.1 ', () => {
// 0.1 -> 0.98
2019-01-12 03:33:27 +00:00
const getPercent = genPercentAdd();
let curPercent = 0;
curPercent = getPercent(curPercent);
expect(curPercent).toBe(0.1);
});
2019-01-07 12:59:57 +00:00
it('should be able to progress to 0.98 ', () => {
// 0.1 -> 0.98
2019-01-12 03:33:27 +00:00
const getPercent = genPercentAdd();
let curPercent = 0;
2019-01-07 12:59:57 +00:00
for (let i = 0; i < 500; i += 1) {
2019-01-12 03:33:27 +00:00
curPercent = getPercent(curPercent);
2019-01-07 12:59:57 +00:00
}
2019-01-12 03:33:27 +00:00
expect(parseFloat(curPercent.toFixed(2))).toBe(0.98);
});
2019-01-07 12:59:57 +00:00
it('should be able to get fileItem', () => {
2019-01-12 03:33:27 +00:00
const file = { uid: '-1', name: 'item.jpg' };
2019-01-07 12:59:57 +00:00
const fileList = [
{
uid: '-1',
name: 'item.jpg',
},
2019-01-12 03:33:27 +00:00
];
const targetItem = getFileItem(file, fileList);
expect(targetItem).toBe(fileList[0]);
});
2019-01-07 12:59:57 +00:00
it('should be able to remove fileItem', () => {
2019-01-12 03:33:27 +00:00
const file = { uid: '-1', name: 'item.jpg' };
2019-01-07 12:59:57 +00:00
const fileList = [
{
uid: '-1',
name: 'item.jpg',
},
{
uid: '-2',
name: 'item2.jpg',
},
2019-01-12 03:33:27 +00:00
];
const targetItem = removeFileItem(file, fileList);
expect(targetItem).toEqual(fileList.slice(1));
});
2019-01-07 12:59:57 +00:00
it('should not be able to remove fileItem', () => {
2019-01-12 03:33:27 +00:00
const file = { uid: '-3', name: 'item.jpg' };
2019-01-07 12:59:57 +00:00
const fileList = [
{
uid: '-1',
name: 'item.jpg',
},
{
uid: '-2',
name: 'item2.jpg',
},
2019-01-12 03:33:27 +00:00
];
const targetItem = removeFileItem(file, fileList);
expect(targetItem).toBe(null);
});
});
2018-12-05 10:31:58 +00:00
it('should support linkProps as object', () => {
2019-01-12 03:33:27 +00:00
const fileList = [
{
uid: '-1',
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
linkProps: {
download: 'image',
rel: 'noopener',
},
2018-12-05 10:31:58 +00:00
},
2019-01-12 03:33:27 +00:00
];
2018-12-05 10:31:58 +00:00
const props = {
propsData: {
fileList,
},
sync: false,
2019-01-12 03:33:27 +00:00
};
const wrapper = mount(Upload, props);
2018-12-05 10:31:58 +00:00
setTimeout(() => {
2019-01-12 03:33:27 +00:00
const linkNode = wrapper.find('a.ant-upload-list-item-name');
expect(linkNode.props().download).toBe('image');
expect(linkNode.props().rel).toBe('noopener');
}, 0);
});
2018-12-05 10:31:58 +00:00
it('should support linkProps as json stringify', () => {
const linkPropsString = JSON.stringify({
download: 'image',
rel: 'noopener',
2019-01-12 03:33:27 +00:00
});
const fileList = [
{
uid: '-1',
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
linkProps: linkPropsString,
},
];
2018-12-05 10:31:58 +00:00
const props = {
propsData: {
fileList,
},
sync: false,
2019-01-12 03:33:27 +00:00
};
const wrapper = mount(Upload, props);
2018-12-05 10:31:58 +00:00
setTimeout(() => {
2019-01-12 03:33:27 +00:00
const linkNode = wrapper.find('a.ant-upload-list-item-name');
expect(linkNode.props().download).toBe('image');
expect(linkNode.props().rel).toBe('noopener');
}, 0);
});
});