From 97af864599a1c3ef9cf08723a1c76fa578e8ff03 Mon Sep 17 00:00:00 2001 From: Victor Chan Date: Thu, 15 Aug 2019 14:04:16 +1000 Subject: [PATCH] issue #26 Test comment works as expected --- lib/actions/comment.js | 2 +- lib/actions/comment.spec.js | 70 +++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/lib/actions/comment.js b/lib/actions/comment.js index d630009..b76d612 100644 --- a/lib/actions/comment.js +++ b/lib/actions/comment.js @@ -17,7 +17,7 @@ function executeCommentAction({ args, config, credentials }) { } = args; if (!message) { - throw new Error('No comment to post onto pull request'); + return Promise.reject(new Error('No comment to post onto pull request')); } const client = new BitbucketClient({ diff --git a/lib/actions/comment.spec.js b/lib/actions/comment.spec.js index e69de29..14c09d4 100644 --- a/lib/actions/comment.spec.js +++ b/lib/actions/comment.spec.js @@ -0,0 +1,70 @@ +'use strict'; + +const chai = require('chai'); +const nock = require('nock'); + +const { executeCommentAction } = require('./comment'); + +const HTTP_CREATED = 201; + +describe('Action Comment', () => { + describe('executeCommentAction', () => { + let config, credentials, args, postCommentNock; + beforeEach(() => { + config = { + bitbucket: { + repoUser: 'my-user', + repoSlug: 'my-repo', + }, + }; + credentials = { + bitbucket: { + username: 'user1', + password: 'pass1', + }, + }; + args = { + pullRequestID: 10, + commentMessage: 'foo bar', + }; + postCommentNock = nock('https://api.bitbucket.org/2.0') + .post('/repositories/my-user/my-repo/pullrequests/10/comments') + .once() + .reply(HTTP_CREATED, {}); + }); + + afterEach(() => { + nock.cleanAll(); + }); + + it('returns error if there are no comments to post', done => { + delete args.commentMessage; + executeCommentAction({ + args, + config, + credentials, + }) + .then(() => done('expected error when there is no comment message to post')) + .catch(({ message }) => { + chai.expect(message).to.equal('No comment to post onto pull request'); + chai.expect(postCommentNock.isDone(), 'Request should not be called').to.be.false; + done(); + }); + }); + + it('returns success when posting comment with all args', done => { + executeCommentAction({ + args, + config, + credentials, + }) + .then(() => { + chai.expect(postCommentNock.isDone(), 'Request should have been called once').to.be.true; + done(); + }) + .catch(({ message }) => { + done(message); + }); + }); + }); +});