Skip to content

Commit 56513ac

Browse files
committed
test: Add test case
1 parent 8982f6b commit 56513ac

File tree

2 files changed

+125
-13
lines changed

2 files changed

+125
-13
lines changed

package.json

+2-13
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"now-build": "npm run docs:build"
3838
},
3939
"devDependencies": {
40-
"@types/jest": "^26.0.0",
40+
"@types/enzyme": "^3.10.8",
41+
"@types/jest": "^26.0.19",
4142
"@types/react": "^16.9.2",
4243
"@types/react-dom": "^16.9.0",
4344
"@umijs/fabric": "^2.0.0",
@@ -67,18 +68,6 @@
6768
"classnames": "^2.2.5",
6869
"rc-util": "^5.2.0"
6970
},
70-
"jest": {
71-
"collectCoverageFrom": [
72-
"src/*"
73-
],
74-
"coveragePathIgnorePatterns": [
75-
"src/IframeUploader.jsx"
76-
],
77-
"transform": {
78-
"\\.tsx?$": "./node_modules/rc-tools/scripts/jestPreprocessor.js",
79-
"\\.jsx?$": "./node_modules/rc-tools/scripts/jestPreprocessor.js"
80-
}
81-
},
8271
"peerDependencies": {
8372
"react": ">=16.9.0",
8473
"react-dom": ">=16.9.0"

tests/batch.spec.tsx

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* eslint no-console:0 */
2+
import React from 'react';
3+
import { format } from 'util';
4+
import { mount, ReactWrapper } from 'enzyme';
5+
import sinon from 'sinon';
6+
import Upload from '../src';
7+
import { UploadProps, RcFile } from '../src/interface';
8+
9+
describe('Upload.Batch', () => {
10+
function getFile(name: string): RcFile {
11+
return {
12+
name,
13+
toString: () => name,
14+
} as RcFile;
15+
}
16+
17+
function genProps(props?: {
18+
onStart: UploadProps['onStart'];
19+
onProgress: UploadProps['onProgress'];
20+
onSuccess: UploadProps['onSuccess'];
21+
onError: UploadProps['onError'];
22+
}) {
23+
return {
24+
action: '/test',
25+
data: { a: 1, b: 2 },
26+
multiple: true,
27+
accept: '.png',
28+
onStart(file) {
29+
props?.onStart?.(file);
30+
},
31+
onSuccess(res, file, xhr) {
32+
props?.onSuccess?.(res, file, xhr);
33+
},
34+
onProgress(step, file) {
35+
props?.onProgress?.(step, file);
36+
},
37+
onError(err, ret, file) {
38+
props?.onError?.(err, ret, file);
39+
},
40+
};
41+
}
42+
43+
describe('onBatchUpload', () => {
44+
const firstFile = getFile('first.png');
45+
const secondFile = getFile('second.png');
46+
47+
function triggerUpload(wrapper: ReactWrapper) {
48+
const files: RcFile[] = [firstFile, secondFile];
49+
wrapper.find('input').first().simulate('change', { target: { files } });
50+
}
51+
52+
it('should trigger', done => {
53+
const onBatchUpload = jest.fn();
54+
55+
const wrapper = mount(<Upload onBatchUpload={onBatchUpload} {...genProps()} />);
56+
triggerUpload(wrapper);
57+
58+
setTimeout(() => {
59+
expect(onBatchUpload).toHaveBeenCalledWith([
60+
expect.objectContaining(firstFile),
61+
expect.objectContaining(secondFile),
62+
]);
63+
done();
64+
}, 10);
65+
});
66+
67+
it('beforeUpload return false', done => {
68+
const onBatchUpload = jest.fn();
69+
70+
const wrapper = mount(
71+
<Upload onBatchUpload={onBatchUpload} beforeUpload={() => false} {...genProps()} />,
72+
);
73+
triggerUpload(wrapper);
74+
75+
setTimeout(() => {
76+
expect(onBatchUpload).toHaveBeenCalledWith([
77+
expect.objectContaining(firstFile),
78+
expect.objectContaining(secondFile),
79+
]);
80+
done();
81+
}, 10);
82+
});
83+
84+
it('beforeUpload return promise file', done => {
85+
const onBatchUpload = jest.fn();
86+
87+
const wrapper = mount(
88+
<Upload
89+
onBatchUpload={onBatchUpload}
90+
beforeUpload={file => Promise.resolve(file)}
91+
{...genProps()}
92+
/>,
93+
);
94+
triggerUpload(wrapper);
95+
96+
setTimeout(() => {
97+
expect(onBatchUpload).toHaveBeenCalledWith([
98+
expect.objectContaining(firstFile),
99+
expect.objectContaining(secondFile),
100+
]);
101+
done();
102+
}, 10);
103+
});
104+
105+
it('beforeUpload return promise rejection', done => {
106+
const onBatchUpload = jest.fn();
107+
108+
const wrapper = mount(
109+
<Upload
110+
onBatchUpload={onBatchUpload}
111+
beforeUpload={() => Promise.reject()}
112+
{...genProps()}
113+
/>,
114+
);
115+
triggerUpload(wrapper);
116+
117+
setTimeout(() => {
118+
expect(onBatchUpload).toHaveBeenCalledWith([]);
119+
done();
120+
}, 10);
121+
});
122+
});
123+
});

0 commit comments

Comments
 (0)