-
Notifications
You must be signed in to change notification settings - Fork 53
Added the favoriteImage method and tests #122
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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`); | ||
}); | ||
}); | ||
}); |
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"`); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
break; | ||
case 'favorite': | ||
options.method = 'POST'; | ||
options.url += 'image/' + payload + '/favorite'; | ||
break; | ||
default: | ||
throw new Error('Invalid operation'); | ||
|
@@ -105,15 +109,13 @@ imgur._imgurRequest = async (operation, payload, extraFormParams) => { | |
}; | ||
} | ||
|
||
if (operation === 'upload' || operation === 'update') { | ||
if (typeof extraFormParams === 'object') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}; | ||
|
||
/** | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏾