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

111 lines
2.4 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 '..';
describe('Upload', () => {
// 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();
});
it('return promise in beforeUpload', (done) => {
const data = jest.fn();
const props = {
action: 'http://upload.com',
beforeUpload: () => new Promise(resolve =>
2017-10-22 23:36:27 +08:00
setTimeout(() => resolve('success'), 1000)
),
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' },
],
},
});
});
2017-02-22 16:06:08 +08:00
});