Skip to content

Commit

Permalink
fix: supoort * and */* value (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
kerm1it authored Mar 24, 2021
1 parent e2b5cd2 commit 3995546
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/attr-accept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default (file: RcFile, acceptedFiles: string | string[]) => {

return acceptedFilesArray.some(type => {
const validType = type.trim();
// This is something like */*,* allow all files
if (/^\*(\/\*)?$/.test(type)) {
return true;
}
if (validType.charAt(0) === '.') {
return endsWith(fileName.toLowerCase(), validType.toLowerCase());
}
Expand Down
150 changes: 150 additions & 0 deletions tests/uploader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,156 @@ describe('uploader', () => {
});
});

describe('accept', () => {
if (typeof FormData === 'undefined') {
return;
}

let uploader;
const handlers = {};

const props = {
action: '/test',
data: { a: 1, b: 2 },
directory: true,
onStart(file) {
if (handlers.onStart) {
handlers.onStart(file);
}
},
};

function test(desc, value, files, expecptCallTimes) {
it(desc, done => {
uploader = mount(<Uploader {...props} accept={value} />);
const input = uploader.find('input').first();
input.simulate('change', { target: { files } });
const mockStart = jest.fn();
handlers.onStart = mockStart;
setTimeout(() => {
expect(mockStart.mock.calls.length).toBe(expecptCallTimes);
done();
}, 100);
});
}

test(
'default',
undefined,
[
{
name: 'accepted.webp',
},
{
name: 'accepted.png',
},
{
name: 'accepted.txt',
},
],
3,
);

test(
'support .ext',
'.png',
[
{
name: 'unaccepted.webp',
},
{
name: 'accepted.png',
},
],
1,
);

test(
'support .ext,ext',
'.png,.txt',
[
{
name: 'accepted.png',
},
{
name: 'unaccepted.jpg',
},
{
name: 'accepted.txt',
},
],
2,
);

test(
'support image/type',
'image/jpeg',
[
{
name: 'unaccepted.png',
type: 'image/png',
},
{
name: 'accepted.jpg',
type: 'image/jpeg',
},
],
1,
);

test(
'support image/*',
'image/*',
[
{
name: 'accepted.png',
type: 'image/png',
},
{
name: 'accepted.jpg',
type: 'image/jpeg',
},
{
name: 'unaccepted.text',
type: 'text/plain',
},
],
2,
);

test(
'support *',
'*',
[
{
name: 'accepted.png',
type: 'image/png',
},
{
name: 'accepted.text',
type: 'text/plain',
},
],
2,
);

test(
'support */*',
'*/*',
[
{
name: 'accepted.png',
type: 'image/png',
},
{
name: 'accepted.text',
type: 'text/plain',
},
],
2,
);
});

describe('transform file before request', () => {
let uploader;
beforeEach(() => {
Expand Down

1 comment on commit 3995546

@vercel
Copy link

@vercel vercel bot commented on 3995546 Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.