Skip to content

Commit a983bfa

Browse files
authored
chore: Pass invalidate type (#320)
* test: test driven * fix: Ignore invalite type
1 parent 8747865 commit a983bfa

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

src/attr-accept.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warning from 'rc-util/lib/warning';
12
import type { RcFile } from './interface';
23

34
export default (file: RcFile, acceptedFiles: string | string[]) => {
@@ -15,6 +16,8 @@ export default (file: RcFile, acceptedFiles: string | string[]) => {
1516
if (/^\*(\/\*)?$/.test(type)) {
1617
return true;
1718
}
19+
20+
// like .jpg, .png
1821
if (validType.charAt(0) === '.') {
1922
const lowerFileName = fileName.toLowerCase();
2023
const lowerType = validType.toLowerCase();
@@ -26,11 +29,24 @@ export default (file: RcFile, acceptedFiles: string | string[]) => {
2629

2730
return affixList.some(affix => lowerFileName.endsWith(affix));
2831
}
32+
33+
// This is something like a image/* mime type
2934
if (/\/\*$/.test(validType)) {
30-
// This is something like a image/* mime type
3135
return baseMimeType === validType.replace(/\/.*$/, '');
3236
}
33-
return mimeType === validType;
37+
38+
// Full match
39+
if (mimeType === validType) {
40+
return true;
41+
}
42+
43+
// Invalidate type should skip
44+
if (/^\w+$/.test(validType)) {
45+
warning(false, `Upload takes an invalidate 'accept' type '${validType}'.Skip for check.`);
46+
return true;
47+
}
48+
49+
return false;
3450
});
3551
}
3652
return true;

tests/uploader.spec.js

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint no-console:0 */
22
import React from 'react';
33
import { format } from 'util';
4+
import { resetWarned } from 'rc-util/lib/warning';
45
import { mount } from 'enzyme';
56
import sinon from 'sinon';
67
import Uploader from '../index';
@@ -409,6 +410,35 @@ describe('uploader', () => {
409410
done();
410411
}, 100);
411412
});
413+
414+
it('accept if type is invalidate', done => {
415+
resetWarned();
416+
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
417+
418+
uploader.unmount();
419+
uploader = mount(<Uploader {...props} accept="jpg,png" />);
420+
421+
const input = uploader.find('input').first();
422+
const files = [
423+
{
424+
name: 'unaccepted.webp',
425+
},
426+
];
427+
input.simulate('change', { target: { files } });
428+
const mockStart = jest.fn();
429+
handlers.onStart = mockStart;
430+
431+
expect(errSpy).toHaveBeenCalledWith(
432+
"Warning: Upload takes an invalidate 'accept' type 'jpg'.Skip for check.",
433+
);
434+
435+
setTimeout(() => {
436+
expect(mockStart.mock.calls.length).toBe(1);
437+
438+
errSpy.mockRestore();
439+
done();
440+
}, 100);
441+
});
412442
});
413443

414444
describe('accept', () => {
@@ -430,15 +460,25 @@ describe('uploader', () => {
430460
},
431461
};
432462

433-
function test(desc, value, files, expecptCallTimes) {
463+
function test(desc, value, files, expectCallTimes, errorMessage) {
434464
it(desc, done => {
465+
resetWarned();
466+
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
467+
435468
uploader = mount(<Uploader {...props} accept={value} />);
436469
const input = uploader.find('input').first();
437470
input.simulate('change', { target: { files } });
438471
const mockStart = jest.fn();
439472
handlers.onStart = mockStart;
473+
474+
if (errorMessage) {
475+
expect(errSpy).toHaveBeenCalledWith(errorMessage);
476+
}
477+
440478
setTimeout(() => {
441-
expect(mockStart.mock.calls.length).toBe(expecptCallTimes);
479+
expect(mockStart.mock.calls.length).toBe(expectCallTimes);
480+
481+
errSpy.mockRestore();
442482
done();
443483
}, 100);
444484
});
@@ -576,6 +616,23 @@ describe('uploader', () => {
576616
],
577617
2,
578618
);
619+
620+
test(
621+
'invalidate type should skip',
622+
'jpg',
623+
[
624+
{
625+
name: 'accepted.png',
626+
type: 'image/png',
627+
},
628+
{
629+
name: 'accepted.text',
630+
type: 'text/plain',
631+
},
632+
],
633+
2,
634+
"Warning: Upload takes an invalidate 'accept' type 'jpg'.Skip for check.",
635+
);
579636
});
580637

581638
describe('transform file before request', () => {

0 commit comments

Comments
 (0)