Skip to content

Commit f3ccf21

Browse files
committed
test: add test case
1 parent 6b0ae0d commit f3ccf21

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

tests/uploader.spec.tsx

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { fireEvent, render } from '@testing-library/react';
21
import { resetWarned } from '@rc-component/util/lib/warning';
2+
import { fireEvent, render } from '@testing-library/react';
33
import React from 'react';
44
import sinon from 'sinon';
55
import { format } from 'util';
@@ -1040,6 +1040,71 @@ describe('uploader', () => {
10401040
);
10411041
});
10421042

1043+
describe('AcceptConfig', () => {
1044+
let uploader: ReturnType<typeof render>;
1045+
const handlers: UploadProps = {};
1046+
1047+
const props: UploadProps = {
1048+
action: '/test',
1049+
data: { a: 1, b: 2 },
1050+
directory: true, // Enable format filtering
1051+
onStart(file) {
1052+
if (handlers.onStart) {
1053+
handlers.onStart(file);
1054+
}
1055+
},
1056+
};
1057+
1058+
function testAcceptConfig(desc: string, accept: any, files: object[], expectCallTimes: number) {
1059+
it(desc, done => {
1060+
uploader = render(<Upload {...props} accept={accept} />);
1061+
const input = uploader.container.querySelector('input')!;
1062+
fireEvent.change(input, { target: { files } });
1063+
const mockStart = jest.fn();
1064+
handlers.onStart = mockStart;
1065+
1066+
setTimeout(() => {
1067+
expect(mockStart.mock.calls.length).toBe(expectCallTimes);
1068+
done();
1069+
}, 100);
1070+
});
1071+
}
1072+
1073+
testAcceptConfig(
1074+
'should work with format only',
1075+
{ format: '.png' },
1076+
[{ name: 'test.png' }, { name: 'test.jpg' }],
1077+
1,
1078+
);
1079+
1080+
testAcceptConfig(
1081+
'should work with filter: native',
1082+
{ format: '.png', filter: 'native' },
1083+
[{ name: 'test.png' }, { name: 'test.jpg' }],
1084+
2, // native filter bypasses format check
1085+
);
1086+
1087+
testAcceptConfig(
1088+
'should work with custom filter function',
1089+
{
1090+
format: '.png',
1091+
filter: (file: any) => file.name.includes('custom'),
1092+
},
1093+
[{ name: 'custom.jpg' }, { name: 'test.png' }],
1094+
1, // only custom.jpg passes custom filter
1095+
);
1096+
1097+
testAcceptConfig(
1098+
'should work with MIME type format',
1099+
{ format: 'image/*' },
1100+
[
1101+
{ name: 'test.png', type: 'image/png' },
1102+
{ name: 'doc.txt', type: 'text/plain' },
1103+
],
1104+
1, // only image file passes
1105+
);
1106+
});
1107+
10431108
describe('transform file before request', () => {
10441109
let uploader: ReturnType<typeof render>;
10451110
beforeEach(() => {

0 commit comments

Comments
 (0)