Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

Added the favoriteImage method and tests #122

Merged
merged 3 commits into from
Mar 13, 2021
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
15 changes: 15 additions & 0 deletions __tests__/deleteImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const imgur = require('../lib/imgur.js');

describe('deleteImage()', () => {
describe('delete image response', () => {
test('should fail when id is not passed', () => {
const errMsg = 'Missing delete hash';
expect(imgur.deleteImage()).rejects.toThrowError(errMsg);
});

test('image is successfully deleted', async () => {
const resp = await imgur.deleteImage('JK9ybyj');
expect(resp).toMatchInlineSnapshot(`true`);
});
});
});
16 changes: 16 additions & 0 deletions __tests__/favoriteImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const imgur = require('../lib/imgur.js');

describe('favoriteImage()', () => {
describe('favorite image response', () => {
test('should fail with no input', () => {
const errMsg = 'Missing image ID';

expect(imgur.favoriteImage()).rejects.toThrowError(errMsg);
});

test('should return successful favorite image response', async () => {
const resp = await imgur.favoriteImage('lDrXtHj');
expect(resp).toMatchInlineSnapshot(`"favorited"`);
});
});
});
2 changes: 1 addition & 1 deletion __tests__/getGalleryInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const imgur = require('../lib/imgur.js');

beforeAll(() => imgur.setClientId('abc123'));

describe('getGalleryInfo', () => {
describe('getGalleryInfo()', () => {
describe('get gallery info response', () => {
test('should fail when id is not passed', () => {
const errMsg = 'Invalid gallery ID';
Expand Down
29 changes: 22 additions & 7 deletions lib/imgur.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ imgur._imgurRequest = async (operation, payload, extraFormParams) => {
break;
case 'search':
options.method = 'GET';
options.url += '/gallery/search/' + payload;
options.url += 'gallery/search/' + payload;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leading / doesn't break the request but it would break any tests we would want to create for this endpoint. I think this leading slash was left in by mistake, and after spending more time with this code I feel confident that is the case.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm curious, in what way do tests break? is it bc of the way the url is matched in the handler?

Copy link
Collaborator Author

@KenEucker KenEucker Mar 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaimallea correct. It would be easy to fix the test to suite, but then the URL would look weird:

https://api.imgur.com/3//gallery/search/lDrXtHj

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏾

break;
case 'favorite':
options.method = 'POST';
options.url += 'image/' + payload + '/favorite';
break;
default:
throw new Error('Invalid operation');
Expand All @@ -105,15 +109,13 @@ imgur._imgurRequest = async (operation, payload, extraFormParams) => {
};
}

if (operation === 'upload' || operation === 'update') {
if (typeof extraFormParams === 'object') {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of limiting this block of code by the type of operation, let's just add the form params if the extraFormParams parameter is an object. This includes the 'upload' and 'update' operations as well as anything else that would need to use extraFormParams in an imgurRequest.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

if (operation === 'upload') {
form.append('image', payload);
}

if (typeof extraFormParams === 'object') {
for (let param in extraFormParams) {
form.append(param, extraFormParams[param]);
}
for (let param in extraFormParams) {
form.append(param, extraFormParams[param]);
}

options.body = form;
Expand Down Expand Up @@ -337,7 +339,20 @@ imgur.deleteImage = async (deleteHash) => {
throw new Error('Missing delete hash');
}

return await imgur._imgurRequest('delete', 'deleteHash');
return await imgur._imgurRequest('delete', deleteHash);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had we had tests for this method, we would have caught this obvious error. Now, we do have tests!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow! tsk tsk @ myself 😞

};

/**
* Favorite image
* @param {string} id - the id of the image to favorite
* @returns {promise}
*/
imgur.favoriteImage = async (id) => {
if (!id) {
throw new Error('Missing image ID');
}

return await imgur._imgurRequest('favorite', id);
};

/**
Expand Down
21 changes: 21 additions & 0 deletions lib/mocks/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ const handlers = [
ctx.cookie('authorize_token', allow)
);
}),

rest.delete('https://api.imgur.com/3/image/JK9ybyj', (_req, res, ctx) => {
const response = {
data: true,
success: true,
status: 200,
};
return res(ctx.json(response));
}),

rest.post(
'https://api.imgur.com/3/image/lDrXtHj/favorite',
(_req, res, ctx) => {
const response = {
data: 'favorited',
success: true,
status: 200,
};
return res(ctx.json(response));
}
),
];

module.exports = {
Expand Down