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

302 lines
7.4 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00:00
import { mount } from '@vue/test-utils';
import Upload from '..';
import { getFileItem, removeFileItem } from '../utils';
2019-01-12 03:33:27 +00:00
import PropsTypes from '../../_util/vue-types';
2021-12-17 07:32:32 +00:00
import { uploadListProps } from '../interface';
2019-12-10 05:16:16 +00:00
import { setup, teardown } from './mock';
2018-06-02 07:12:53 +00:00
2021-12-17 07:32:32 +00:00
uploadListProps.items = PropsTypes.any;
2018-05-31 08:36:59 +00:00
describe('Upload', () => {
2019-12-10 05:16:16 +00:00
beforeEach(() => setup());
afterEach(() => teardown());
2018-05-31 08:36:59 +00:00
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
2020-08-04 15:00:11 +00:00
xit('return promise in beforeUpload', done => {
2019-01-12 03:33:27 +00:00
const data = jest.fn();
2018-05-31 08:36:59 +00:00
const props = {
2020-07-25 07:46:49 +00:00
props: {
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: {
2020-08-04 15:00:11 +00:00
default: () => <button>upload</button>,
2018-05-31 08:36:59 +00:00
},
sync: false,
2019-01-12 03:33:27 +00:00
};
const wrapper = mount(Upload, props);
2018-05-31 08:36:59 +00:00
setTimeout(() => {
2020-08-04 15:00:11 +00:00
wrapper.findComponent('ajaxUploader').vm.onChange({
2018-05-31 08:36:59 +00:00
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
2020-08-04 15:00:11 +00:00
xit('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 = {
2020-08-04 15:00:11 +00:00
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();
}
2019-01-07 12:59:57 +00:00
},
slots: {
2020-08-04 15:00:11 +00:00
default: () => <button>upload</button>,
2019-01-07 12:59:57 +00:00
},
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
2020-08-04 15:00:11 +00:00
xit('should not stop upload when return value of beforeUpload is false', done => {
2019-01-12 03:33:27 +00:00
const data = jest.fn();
2018-05-31 08:36:59 +00:00
const props = {
2020-08-04 15:00:11 +00:00
action: 'http://upload.com',
beforeUpload: () => false,
data,
onChange: ({ file }) => {
expect(file instanceof File).toBe(true);
expect(data).not.toBeCalled();
done();
2018-05-31 08:36:59 +00:00
},
slots: {
2020-08-04 15:00:11 +00:00
default: () => <button>upload</button>,
2018-05-31 08:36:59 +00:00
},
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
2020-08-04 15:00:11 +00:00
xit('should increase percent automaticly when call autoUpdateProgress in IE', done => {
2019-01-12 03:33:27 +00:00
let uploadInstance;
let lastPercent = -1;
2018-05-31 08:36:59 +00:00
const props = {
2020-07-25 07:46:49 +00:00
props: {
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);
});
2020-08-04 15:00:11 +00:00
xit('should not stop upload when return value of beforeUpload is not false', done => {
2019-01-12 03:33:27 +00:00
const data = jest.fn();
2018-05-31 08:36:59 +00:00
const props = {
2020-07-25 07:46:49 +00:00
props: {
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
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 = {
2020-07-25 07:46:49 +00:00
props: {
2018-12-05 10:31:58 +00:00
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 = {
2020-07-25 07:46:49 +00:00
props: {
2018-12-05 10:31:58 +00:00
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);
});
});