ant-design/components/upload/__tests__/upload.test.js
偏右 43362de756 Fix beforeUpload will stop uploading when return (#7870)
value is undefined

it should be same as rc-upload
2017-10-12 14:48:35 +08:00

111 lines
2.4 KiB
JavaScript

/* eslint-disable react/no-string-refs, react/prefer-es6-class */
import React from 'react';
import { mount } from 'enzyme';
import Upload from '..';
describe('Upload', () => {
// https://github.com/react-component/upload/issues/36
it('should get refs inside Upload in componentDidMount', () => {
let ref;
class App extends React.Component {
componentDidMount() {
ref = this.refs.input;
}
render() {
return (
<Upload supportServerRender={false}>
<input ref="input" />
</Upload>
);
}
}
mount(<App />);
expect(ref).toBeDefined();
});
it('return promise in beforeUpload', (done) => {
const data = jest.fn();
const props = {
action: 'http://upload.com',
beforeUpload: () => new Promise(resolve =>
setTimeout(() => resolve('success'))
),
data,
onChange: ({ file }) => {
if (file.status !== 'uploading') {
expect(data).toBeCalled();
done();
}
},
};
const wrapper = mount(
<Upload {...props}>
<button>upload</button>
</Upload>
);
wrapper.find('input').simulate('change', {
target: {
files: [
{ filename: 'foo.png' },
],
},
});
});
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: () => false,
data,
onChange: () => {
expect(data).not.toBeCalled();
done();
},
};
const wrapper = mount(
<Upload {...props}>
<button>upload</button>
</Upload>
);
wrapper.find('input').simulate('change', {
target: {
files: [
{ filename: 'foo.png' },
],
},
});
});
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>upload</button>
</Upload>
);
wrapper.find('input').simulate('change', {
target: {
files: [
{ filename: 'foo.png' },
],
},
});
});
});