Skip to content

Commit

Permalink
Mock requests
Browse files Browse the repository at this point in the history
  • Loading branch information
yesmeck committed Oct 15, 2017
1 parent 0a6475c commit e6b1ba6
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,79 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Upload List handle error 1`] = `
<span
class=""
>
<div
class="ant-upload ant-upload-select ant-upload-select-text"
>
<span
class="ant-upload"
role="button"
tabindex="0"
>
<input
accept=""
style="display: none;"
type="file"
/>
<button>
upload
</button>
</span>
</div>
<div
class="ant-upload-list ant-upload-list-text"
>
<div
class="ant-upload-list-item ant-upload-list-item-error"
>
<div
class="ant-upload-list-item-info"
>
<span>
<i
class="anticon anticon-paper-clip"
/>
<span
class="ant-upload-list-item-name"
title="foo.png"
>
foo.png
</span>
</span>
</div>
<i
class="anticon anticon-cross"
title="删除文件"
/>
<div
class="ant-upload-list-item-progress fade-leave"
>
<div
class="ant-progress ant-progress-line ant-progress-status-normal"
>
<div>
<div
class="ant-progress-outer"
>
<div
class="ant-progress-inner"
>
<div
class="ant-progress-bg"
style="width: 0%; height: 2px;"
/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</span>
`;

exports[`Upload List should be uploading when upload a file 1`] = `
<span
class=""
Expand Down Expand Up @@ -100,7 +174,7 @@ exports[`Upload List should be uploading when upload a file 2`] = `
class="ant-upload-list ant-upload-list-text"
>
<div
class="ant-upload-list-item ant-upload-list-item-error"
class="ant-upload-list-item ant-upload-list-item-done"
>
<div
class="ant-upload-list-item-info"
Expand Down
11 changes: 11 additions & 0 deletions components/upload/__tests__/requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const successRequest = ({ onSuccess, file }) => {
setTimeout(() => {
onSuccess(null, file);
});
};

export const errorRequest = ({ onError }) => {
setTimeout(() => {
onError();
});
};
41 changes: 33 additions & 8 deletions components/upload/__tests__/uploadlist.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { mount } from 'enzyme';
import Upload from '..';
import { errorRequest, successRequest } from './requests';

const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));

Expand Down Expand Up @@ -62,23 +63,47 @@ describe('Upload List', () => {

it('should be uploading when upload a file', (done) => {
let wrapper;
let finished;
const onChange = ({ file }) => {
if (finished) {
done();
return;
}
if (file.status === 'uploading') {
expect(wrapper.render()).toMatchSnapshot();
}
if (file.status === 'error') {
if (file.status === 'done') {
expect(wrapper.render()).toMatchSnapshot();
done();
}
};
wrapper = mount(
<Upload
action="http://jsonplaceholder.typicode.com/posts/"
onChange={onChange}
customRequest={successRequest}
>
<button>upload</button>
</Upload>
);
wrapper.find('input').simulate('change', {
target: {
files: [
{ filename: 'foo.png' },
],
},
});
});

it('handle error', (done) => {
let wrapper;
const onChange = ({ file }) => {
if (file.status !== 'uploading') {
expect(wrapper.render()).toMatchSnapshot();
finished = true;
done();
}
};
wrapper = mount(
<Upload action="http://jsonplaceholder.typicode.com/posts/" onChange={onChange}>
<Upload
action="http://jsonplaceholder.typicode.com/posts/"
onChange={onChange}
customRequest={errorRequest}
>
<button>upload</button>
</Upload>
);
Expand Down

0 comments on commit e6b1ba6

Please sign in to comment.