Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix tests #19

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snapshot-labs/pineapple",
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
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'
})
})
);
});
});
});