From c6ee36432e7f325b11c67e36edd0405849cfa663 Mon Sep 17 00:00:00 2001 From: Ken Eucker Date: Thu, 11 Mar 2021 12:14:29 -0800 Subject: [PATCH] feat(getgalleryinfo): added tests and handler mocks for getGalleryInfo --- __tests__/getGalleryInfo.js | 52 +++++++++++++++++++++++++++++++++++++ lib/mocks/handlers.js | 12 +++++++++ 2 files changed, 64 insertions(+) create mode 100644 __tests__/getGalleryInfo.js diff --git a/__tests__/getGalleryInfo.js b/__tests__/getGalleryInfo.js new file mode 100644 index 00000000..5e409b5d --- /dev/null +++ b/__tests__/getGalleryInfo.js @@ -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); + }); + }); +}); diff --git a/lib/mocks/handlers.js b/lib/mocks/handlers.js index d86ecb64..0be3f996 100644 --- a/lib/mocks/handlers.js +++ b/lib/mocks/handlers.js @@ -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 = {