Skip to content

Commit

Permalink
fix: fix when readEntries method throw error and add new test
Browse files Browse the repository at this point in the history
  • Loading branch information
huiliangShen committed Aug 7, 2024
1 parent 4101c75 commit 1f8cbae
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/traverseFileTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const traverseFileTree = async (files: InternalDataTransferItem[], isAccepted) =
const entries = [];

while (true) {
const results = await new Promise<InternalDataTransferItem[]>((resolve, reject) => {
dirReader.readEntries(resolve, reject);
const results = await new Promise<InternalDataTransferItem[]>((resolve) => {
dirReader.readEntries(resolve, () => resolve([]));
});

if (!results.length) {
Expand Down
61 changes: 57 additions & 4 deletions tests/uploader.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ const makeFileSystemEntryAsync = item => {
createReader: () => {
let first = true;
return {
async readEntries(handle) {
async readEntries(handle, error) {
await sleep(100);

if (!first) {
return handle([]);
}

if (item.error && first) {
return error && error(new Error('read file error'))
}

first = false;
return handle(item.children.map(makeFileSystemEntry));
return handle(item.children.map(makeFileSystemEntryAsync));
},
};
},
Expand Down Expand Up @@ -532,7 +536,56 @@ describe('uploader', () => {
setTimeout(() => {
expect(mockStart.mock.calls.length).toBe(2);
done();
}, 500);
}, 1000);
});

it('dragging and dropping files to upload through asynchronous file reading with some readEntries method throw error', (done) => {
const input = uploader.container.querySelector('input')!;

const files = {
name: 'foo',
children: [
{
name: 'bar',
error: true,
children: [
{
name: '1.png',
},
{
name: 'ffc',
children: [
{
name: '7.png',
},
{
name: '8.png',
},
],
}
],
},
{
name: 'rc',
children: [
{
name: '3.png',
},
{
name: '4.webp',
},
],
},
],
};
fireEvent.drop(input, { dataTransfer: { items: [makeDataTransferItemAsync(files)] } });
const mockStart = jest.fn();
handlers.onStart = mockStart;

setTimeout(() => {
expect(mockStart.mock.calls.length).toBe(1);
done();
}, 1000);
});

it('unaccepted type files to upload will not trigger onStart when select directory', done => {
Expand Down

0 comments on commit 1f8cbae

Please sign in to comment.