Skip to content

Commit

Permalink
feat:add test and fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
coderz-w committed Jun 10, 2024
1 parent cc62278 commit c3cc93c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/traverseFileTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface InternalDataTransferItem extends DataTransferItem {
}

const traverseFileTree = (files: InternalDataTransferItem[], callback, isAccepted) => {
let restDirectory = 0;
let restFile = files.length;
const flattenFileList = [];
function loopFiles(item: InternalDataTransferItem, callback) {
const dirReader = item.createReader();
Expand All @@ -26,7 +26,7 @@ const traverseFileTree = (files: InternalDataTransferItem[], callback, isAccepte
const isFinished = !entryList.length;

if (isFinished) {
restDirectory--;
restFile = restFile - 1 + fileList.length;
callback(fileList);
} else {
sequence();
Expand All @@ -39,6 +39,7 @@ const traverseFileTree = (files: InternalDataTransferItem[], callback, isAccepte
// eslint-disable-next-line @typescript-eslint/naming-convention
const _traverseFileTree = (item: InternalDataTransferItem, path?: string) => {
if (!item) {
restFile = restFile - 1;
return;
}
// eslint-disable-next-line no-param-reassign
Expand All @@ -62,13 +63,13 @@ const traverseFileTree = (files: InternalDataTransferItem[], callback, isAccepte
});
}
flattenFileList.push(file);
if (restDirectory === 0) {
restFile = restFile - 1;
if (restFile === 0) {
callback(flattenFileList);
}
}
});
} else if (item.isDirectory) {
restDirectory++;
loopFiles(item, (entries: InternalDataTransferItem[]) => {
entries.forEach(entryItem => {
_traverseFileTree(entryItem, `${path}${item.name}/`);
Expand Down
52 changes: 51 additions & 1 deletion tests/uploader.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const makeFileSystemEntry = item => {
return {
readEntries(handle) {
if (!first) {
return [];
return handle([]);
}

first = false;
Expand Down Expand Up @@ -374,6 +374,56 @@ describe('uploader', () => {
uploader = render(<Upload {...props} />);
});

it('beforeUpload should run after all children files are parsed', done => {
const props = { action: '/test', directory: true, accept: '.png' };
const mockBeforeUpload = jest.fn();
const beforeUpload = (file, fileList) => {
console.log('beforeUpload', file, fileList);
mockBeforeUpload(file, fileList);
};
const Test = () => {
return <Upload {...props} beforeUpload={beforeUpload} />;
};

const { container } = render(<Test />);
const files = {
name: 'foo',
children: [
{
name: 'bar',
children: [
{
name: '1.png',
},
{
name: '2.png',
},
{
name: 'rc',
children: [
{
name: '3.png',
},
{
name: '4.png',
},
],
},
],
},
],
};
const input = container.querySelector('input')!;
fireEvent.drop(input, { dataTransfer: { items: [makeDataTransferItem(files)] } });
setTimeout(() => {
expect(mockBeforeUpload.mock.calls.length).toBe(4);
expect(mockBeforeUpload.mock.calls[0][1].length).toBe(4);
expect(mockBeforeUpload.mock.calls[1][1].length).toBe(4);
expect(mockBeforeUpload.mock.calls[2][1].length).toBe(4);
expect(mockBeforeUpload.mock.calls[3][1].length).toBe(4);
done();
}, 100);
});
it('unaccepted type files to upload will not trigger onStart', done => {
const input = uploader.container.querySelector('input')!;
const files = {
Expand Down

0 comments on commit c3cc93c

Please sign in to comment.