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

Commit

Permalink
feat(getgalleryinfo): added tests and handler mocks for getGalleryInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
KenEucker committed Mar 11, 2021
1 parent 2ad7f51 commit c6ee364
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
52 changes: 52 additions & 0 deletions __tests__/getGalleryInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const imgur = require('../lib/imgur.js');

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

describe('getGalleryInfo', () => {
describe('get gallery info response', () => {
test('should fail when id is not passed', () => {
const errMsg = 'Invalid gallery ID';
expect(imgur.getGalleryInfo()).rejects.toThrowError(errMsg);
});

test('gallery info is returned', async () => {
const resp = await imgur.getGalleryInfo('JK9ybyj');
expect(resp).toMatchInlineSnapshot(`
Object {
"description": "gallery-description",
"id": "JK9ybyj",
"title": "gallery-title",
}
`);
});
});

describe("delegates to _imgurRequest('gallery', ...)", () => {
const mockResult = {
data: [],
params: {
id: 'JK9ybyj',
},
};
const payload = 'JK9ybyj';
const _imgurRequestBackup = imgur._imgurRequest;

beforeEach(() => {
imgur._imgurRequest = jest
.fn()
.mockImplementation(() => Promise.resolve(mockResult));
});

afterEach(() => {
imgur._imgurRequest.mockClear();
imgur._imgurRequest = _imgurRequestBackup;
});

it('should delegate', () => {
const promise = imgur.getGalleryInfo('JK9ybyj');

expect(imgur._imgurRequest).toHaveBeenCalledWith('gallery', payload);
expect(promise).resolves.toMatchObject(mockResult);
});
});
});
12 changes: 12 additions & 0 deletions lib/mocks/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ const handlers = [
};
return res(ctx.json(response));
}),
rest.get('https://api.imgur.com/3/gallery/JK9ybyj', (_req, res, ctx) => {
const response = {
data: {
id: 'JK9ybyj',
title: 'gallery-title',
description: 'gallery-description',
},
success: true,
status: 200,
};
return res(ctx.json(response));
}),
];

module.exports = {
Expand Down

0 comments on commit c6ee364

Please sign in to comment.