From 4c62bf2be97cef04834cff08e2af0d08984bb0e6 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Sun, 17 Sep 2023 23:55:26 +0900 Subject: [PATCH] chore: fix and add tests --- src/index.ts | 2 +- test/pin.test.ts | 2 +- test/upload.test.ts | 18 ++++++++++++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index eae0e1a..e3a03f0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 } }; } } diff --git a/test/pin.test.ts b/test/pin.test.ts index 9aa7b13..453952a 100644 --- a/test/pin.test.ts +++ b/test/pin.test.ts @@ -6,5 +6,5 @@ describe('pin()', () => { const receipt = await pin(json); expect(receipt.provider).toBe('4everland'); expect(receipt.cid).toBe('bafkreibatgmdqdxsair3j52zfhtntegshtypq2qbex3fgtorwx34kzippe'); - }, 30e3); + }); }); diff --git a/test/upload.test.ts b/test/upload.test.ts index f4226d2..602e6b2 100644 --- a/test/upload.test.ts +++ b/test/upload.test.ts @@ -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', () => { @@ -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', () => { @@ -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', () => { @@ -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'); + }); }); });