Skip to content

Commit

Permalink
chore: Pass invalidate type (react-component#320)
Browse files Browse the repository at this point in the history
* test: test driven

* fix: Ignore invalite type
  • Loading branch information
zombieJ authored May 17, 2021
1 parent 8747865 commit a983bfa
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/attr-accept.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warning from 'rc-util/lib/warning';
import type { RcFile } from './interface';

export default (file: RcFile, acceptedFiles: string | string[]) => {
Expand All @@ -15,6 +16,8 @@ export default (file: RcFile, acceptedFiles: string | string[]) => {
if (/^\*(\/\*)?$/.test(type)) {
return true;
}

// like .jpg, .png
if (validType.charAt(0) === '.') {
const lowerFileName = fileName.toLowerCase();
const lowerType = validType.toLowerCase();
Expand All @@ -26,11 +29,24 @@ export default (file: RcFile, acceptedFiles: string | string[]) => {

return affixList.some(affix => lowerFileName.endsWith(affix));
}

// This is something like a image/* mime type
if (/\/\*$/.test(validType)) {
// This is something like a image/* mime type
return baseMimeType === validType.replace(/\/.*$/, '');
}
return mimeType === validType;

// Full match
if (mimeType === validType) {
return true;
}

// Invalidate type should skip
if (/^\w+$/.test(validType)) {
warning(false, `Upload takes an invalidate 'accept' type '${validType}'.Skip for check.`);
return true;
}

return false;
});
}
return true;
Expand Down
61 changes: 59 additions & 2 deletions tests/uploader.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint no-console:0 */
import React from 'react';
import { format } from 'util';
import { resetWarned } from 'rc-util/lib/warning';
import { mount } from 'enzyme';
import sinon from 'sinon';
import Uploader from '../index';
Expand Down Expand Up @@ -409,6 +410,35 @@ describe('uploader', () => {
done();
}, 100);
});

it('accept if type is invalidate', done => {
resetWarned();
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

uploader.unmount();
uploader = mount(<Uploader {...props} accept="jpg,png" />);

const input = uploader.find('input').first();
const files = [
{
name: 'unaccepted.webp',
},
];
input.simulate('change', { target: { files } });
const mockStart = jest.fn();
handlers.onStart = mockStart;

expect(errSpy).toHaveBeenCalledWith(
"Warning: Upload takes an invalidate 'accept' type 'jpg'.Skip for check.",
);

setTimeout(() => {
expect(mockStart.mock.calls.length).toBe(1);

errSpy.mockRestore();
done();
}, 100);
});
});

describe('accept', () => {
Expand All @@ -430,15 +460,25 @@ describe('uploader', () => {
},
};

function test(desc, value, files, expecptCallTimes) {
function test(desc, value, files, expectCallTimes, errorMessage) {
it(desc, done => {
resetWarned();
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

uploader = mount(<Uploader {...props} accept={value} />);
const input = uploader.find('input').first();
input.simulate('change', { target: { files } });
const mockStart = jest.fn();
handlers.onStart = mockStart;

if (errorMessage) {
expect(errSpy).toHaveBeenCalledWith(errorMessage);
}

setTimeout(() => {
expect(mockStart.mock.calls.length).toBe(expecptCallTimes);
expect(mockStart.mock.calls.length).toBe(expectCallTimes);

errSpy.mockRestore();
done();
}, 100);
});
Expand Down Expand Up @@ -576,6 +616,23 @@ describe('uploader', () => {
],
2,
);

test(
'invalidate type should skip',
'jpg',
[
{
name: 'accepted.png',
type: 'image/png',
},
{
name: 'accepted.text',
type: 'text/plain',
},
],
2,
"Warning: Upload takes an invalidate 'accept' type 'jpg'.Skip for check.",
);
});

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

0 comments on commit a983bfa

Please sign in to comment.