Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Sep 19, 2023
1 parent ea8498c commit edf4aa7
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions test/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,71 @@ describe('upload()', () => {
it('returns an error', async () => {
const formData = new FormData();
formData.append('test', 'test');
const receipt = await upload(formData);
expect(receipt.error.code).toBe(400);
expect(receipt.error.message).toBe('No file submitted');

expect.assertions(1);

await expect(upload(formData)).rejects.toEqual(
expect.objectContaining({
error: expect.objectContaining({
code: 400,
message: 'No file submitted'
})
})
);
});
});

describe('when the file is too big', () => {
it('returns an error', async () => {
const formData = new FormData();
formData.append('file', createReadStream(path.join(__dirname, './fixtures/too-heavy.jpg')));
const receipt = await upload(formData);
expect(receipt.error.code).toBe(400);
expect(receipt.error.message).toBe('File too large');

expect.assertions(1);

await expect(upload(formData)).rejects.toEqual(
expect.objectContaining({
error: expect.objectContaining({
code: 400,
message: 'File too large'
})
})
);
});
});

describe('when the file is not an image', () => {
it('returns an error', async () => {
const formData = new FormData();
formData.append('file', createReadStream(path.join(__dirname, './fixtures/file.json')));
const receipt = await upload(formData);
expect(receipt.error.code).toBe(415);
expect(receipt.error.message).toBe('Unsupported file type');

expect.assertions(1);

await expect(upload(formData)).rejects.toEqual(
expect.objectContaining({
error: expect.objectContaining({
code: 415,
message: 'Unsupported file type'
})
})
);
});
});

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');

expect.assertions(1);

await expect(upload(formData, 'https://pineapple.fyi/not-existing')).rejects.toEqual(
expect.objectContaining({
error: expect.objectContaining({
code: 404,
message: 'Not Found'
})
})
);
});
});
});

0 comments on commit edf4aa7

Please sign in to comment.