From 83b586f000eff14d04a493478de54f47020c8d05 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Wed, 3 Oct 2018 23:32:04 +0200 Subject: [PATCH] refactor: tests for notify request service (#1039) * refactor: tests for notify request service * refactor: improves notify request service tests * refactor: uses beforeEach for jest.resetModules --- src/lib/notify/notify-request.js | 6 +- test/unit/api/notify-request.spec.js | 101 +++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 test/unit/api/notify-request.spec.js diff --git a/src/lib/notify/notify-request.js b/src/lib/notify/notify-request.js index e1256a68e..9cfc01d10 100644 --- a/src/lib/notify/notify-request.js +++ b/src/lib/notify/notify-request.js @@ -13,17 +13,13 @@ export function notifyRequest(options, content) { if (err || response.statusCode >= HTTP_STATUS.BAD_REQUEST) { const errorMessage = isNil(err) ? response.body : err.message; logger.logger.error({ errorMessage }, 'notify service has thrown an error: @{errorMessage}'); - reject(errorMessage); } else { logger.logger.info({ content }, 'A notification has been shipped: @{content}'); if (isNil(body) === false) { - const bodyResolved = isNil(body) === false ? body : null; - logger.logger.debug({ body }, ' body: @{body}'); - return resolve(bodyResolved); + resolve(body); } - reject(Error('body is missing')); } }); diff --git a/test/unit/api/notify-request.spec.js b/test/unit/api/notify-request.spec.js new file mode 100644 index 000000000..21e447063 --- /dev/null +++ b/test/unit/api/notify-request.spec.js @@ -0,0 +1,101 @@ +/** + * @prettier + */ + +import { HTTP_STATUS, API_ERROR } from '../../../src/lib/constants'; + +/** + * Mocks Logger Service + */ +const logger = { + logger: { + error: jest.fn(), + debug: jest.fn(), + info: jest.fn(), + }, +}; +jest.doMock('../../../src/lib/logger', () => logger); + +/** + * Test Data + */ +const options = { + url: 'http://slack-service', +}; +const content = 'Verdaccio@x.x.x successfully published'; + +describe('notifyRequest', () => { + beforeEach(() => { + jest.resetModules(); + }); + + test('when notification service throws error', async () => { + jest.doMock('request', () => (options, resolver) => { + const response = { + statusCode: HTTP_STATUS.BAD_REQUEST, + }; + const error = { + message: API_ERROR.BAD_DATA, + }; + resolver(error, response); + }); + + const notification = require('../../../src/lib/notify/notify-request'); + const args = [{ errorMessage: 'bad data' }, 'notify service has thrown an error: @{errorMessage}']; + + await expect(notification.notifyRequest(options, content)).rejects.toEqual(API_ERROR.BAD_DATA); + expect(logger.logger.error).toHaveBeenCalledWith(...args); + }); + + test('when notification service throws error with null error value', async () => { + jest.doMock('request', () => (options, resolver) => { + const response = { + statusCode: HTTP_STATUS.BAD_REQUEST, + body: API_ERROR.BAD_DATA, + }; + + resolver(null, response); + }); + + const notification = require('../../../src/lib/notify/notify-request'); + const args = [{ errorMessage: 'bad data' }, 'notify service has thrown an error: @{errorMessage}']; + + await expect(notification.notifyRequest(options, content)).rejects.toEqual(API_ERROR.BAD_DATA); + expect(logger.logger.error).toHaveBeenCalledWith(...args); + }); + + test('when notification is successfully delivered', async () => { + jest.doMock('request', () => (options, resolver) => { + const response = { + statusCode: HTTP_STATUS.OK, + body: 'Successfully delivered', + }; + + resolver(null, response, response.body); + }); + + const notification = require('../../../src/lib/notify/notify-request'); + const infoArgs = [{ content: 'Verdaccio@x.x.x successfully published' }, 'A notification has been shipped: @{content}']; + const debugArgs = [{ body: 'Successfully delivered' }, ' body: @{body}']; + + await expect(notification.notifyRequest(options, content)).resolves.toEqual('Successfully delivered'); + expect(logger.logger.info).toHaveBeenCalledWith(...infoArgs); + expect(logger.logger.debug).toHaveBeenCalledWith(...debugArgs); + }); + + test('when notification is successfully delivered but body is undefined/null', async () => { + jest.doMock('request', () => (options, resolver) => { + const response = { + statusCode: HTTP_STATUS.OK, + }; + + resolver(null, response); + }); + + const notification = require('../../../src/lib/notify/notify-request'); + const infoArgs = [{ content: 'Verdaccio@x.x.x successfully published' }, 'A notification has been shipped: @{content}']; + + await expect(notification.notifyRequest(options, content)).rejects.toThrowError('body is missing'); + expect(logger.logger.info).toHaveBeenCalledWith(...infoArgs); + }); +});