Skip to content

Commit

Permalink
chore: fix and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Sep 17, 2023
1 parent 95be785 commit 4c62bf2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ async function sendRequest(url: string, init: any) {
try {
return (await fetch(url, init)).result;
} catch (e: any) {
return { error: e.data?.error || { code: e.status, message: e.message } };
return { error: e.data?.error || { code: e.status, message: e.statusText } };
}
}
2 changes: 1 addition & 1 deletion test/pin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ describe('pin()', () => {
const receipt = await pin(json);
expect(receipt.provider).toBe('4everland');
expect(receipt.cid).toBe('bafkreibatgmdqdxsair3j52zfhtntegshtypq2qbex3fgtorwx34kzippe');
}, 30e3);
});
});
18 changes: 14 additions & 4 deletions test/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('upload()', () => {
const receipt = await upload(formData);
expect(receipt.provider).toBe('4everland');
expect(receipt.cid).toBe('bafkreidxvfyqu6l3tb3y5gi2nq5zqyincpev2rangnv7nmaocrk7q3o2fi');
}, 30e3);
});
});

describe('when the file is missing', () => {
Expand All @@ -21,7 +21,7 @@ describe('upload()', () => {
const receipt = await upload(formData);
expect(receipt.error.code).toBe(400);
expect(receipt.error.message).toBe('No file submitted');
}, 10e3);
});
});

describe('when the file is too big', () => {
Expand All @@ -31,7 +31,7 @@ describe('upload()', () => {
const receipt = await upload(formData);
expect(receipt.error.code).toBe(400);
expect(receipt.error.message).toBe('File too large');
}, 10e3);
});
});

describe('when the file is not an image', () => {
Expand All @@ -41,6 +41,16 @@ describe('upload()', () => {
const receipt = await upload(formData);
expect(receipt.error.code).toBe(415);
expect(receipt.error.message).toBe('Unsupported file type');
}, 10e3);
});
});

describe('on network error', () => {
it('returns an error following the same format as server error', async () => {
const formData = new FormData();
formData.append('file', createReadStream(path.join(__dirname, './fixtures/file.json')));
const receipt = await upload(formData, 'https://pineapple.fyi/not-existing');
expect(receipt.error.code).toBe(404);
expect(receipt.error.message).toBe('Not Found');
});
});
});

0 comments on commit 4c62bf2

Please sign in to comment.