From 42e71f2c98d66d2f562c36c0358db25724f27d09 Mon Sep 17 00:00:00 2001 From: Ken Eucker Date: Mon, 8 Mar 2021 23:02:58 -0800 Subject: [PATCH] feat(updateinfo): added the updateInfo method and included a new test file for this method The updateInfo method takes a required id and optional title and description parameters. 106 --- __tests__/updateInfo.js | 63 +++++++++++++++++++++++++++++++++++++++++ lib/imgur.js | 27 ++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 __tests__/updateInfo.js diff --git a/__tests__/updateInfo.js b/__tests__/updateInfo.js new file mode 100644 index 00000000..f5958aa1 --- /dev/null +++ b/__tests__/updateInfo.js @@ -0,0 +1,63 @@ +const imgur = require('../lib/imgur.js'); + +describe('updateInfo', () => { + describe('update image metadata', () => { + test('should fail when id is not passed', () => { + const errMsg = 'image id is required'; + expect(imgur.updateInfo()).rejects.toThrowError(errMsg); + }); + + test('should fail when query is passed a boolean', () => { + const errMsg = 'You did not pass a string as an id.'; + expect(imgur.updateInfo(true)).rejects.toThrowError(errMsg); + }); + + test('should fail when id passed is not a string', () => { + const errMsg = 'You did not pass a string as an id.'; + expect(imgur.updateInfo(1)).rejects.toThrowError(errMsg); + }); + }); + + describe("delegates to _imgurRequest('update', ...)", () => { + const mockResult = { + data: [], + params: { + id: 'V8svmob', + title: 'image title', + message: 'image description', + }, + }; + const payload = 'V8svmob'; + const _imgurRequestBackup = imgur._imgurRequest; + + beforeEach(() => { + imgur._imgurRequest = jest + .fn() + .mockImplementation(() => Promise.resolve(mockResult)); + }); + + afterEach(() => { + imgur._imgurRequest.mockClear(); + imgur._imgurRequest = _imgurRequestBackup; + }); + + it('should delegate', () => { + const params = { + title: 'image title', + description: 'image description', + }; + const promise = imgur.updateInfo( + 'V8svmob', + params.title, + params.description + ); + + expect(imgur._imgurRequest).toHaveBeenCalledWith( + 'update', + payload, + params + ); + expect(promise).resolves.toMatchObject(mockResult); + }); + }); +}); diff --git a/lib/imgur.js b/lib/imgur.js index 193d54cc..32ab4ea2 100644 --- a/lib/imgur.js +++ b/lib/imgur.js @@ -350,6 +350,33 @@ imgur.getAlbumInfo = async (id) => { return await imgur._imgurRequest('album', id); }; +/** + * Update image metadata + * @param {string} id - unique image id + * @param {string} title - the title field + * @param {string} description - the description field + * @returns {promise} + */ +imgur.updateInfo = async (id, title, description) => { + const extraFormParams = {}; + + if (!id) { + throw new Error('image id is required'); + } else if (typeof id !== 'string') { + throw new Error('You did not pass a string as an id.'); + } + + if (typeof title === 'string' && title.length) { + extraFormParams.title = title; + } + + if (typeof description === 'string' && description.length) { + extraFormParams.description = description; + } + + return await imgur._imgurRequest('update', id, extraFormParams); +}; + imgur.search = async (query, options) => { const checkQuery = imgur.checkQuery(query); let params;