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

Commit

Permalink
feat(updateinfo): added the updateInfo method and included a new test…
Browse files Browse the repository at this point in the history
… file for this method

The updateInfo method takes a required id and optional title and description parameters.

106
  • Loading branch information
KenEucker committed Mar 9, 2021
1 parent 7238312 commit 42e71f2
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
63 changes: 63 additions & 0 deletions __tests__/updateInfo.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
27 changes: 27 additions & 0 deletions lib/imgur.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 42e71f2

Please sign in to comment.