From 286dc15e40ceadf3921174d29cc0b27e2e5094e7 Mon Sep 17 00:00:00 2001 From: Sebass van Boxel Date: Wed, 4 Nov 2020 22:21:20 +0100 Subject: [PATCH] Added specs for when comment body is empty --- __tests__/comment.test.ts | 129 ++++++++++++++++++++++++-------------- 1 file changed, 83 insertions(+), 46 deletions(-) diff --git a/__tests__/comment.test.ts b/__tests__/comment.test.ts index f8ef90b..52f8acb 100644 --- a/__tests__/comment.test.ts +++ b/__tests__/comment.test.ts @@ -4,6 +4,13 @@ import { updateComment, deleteComment } from "../src/comment"; + +import * as core from '@actions/core'; + +jest.mock('@actions/core', () => ({ + warning: jest.fn() +})); + const repo = {}; it("findPreviousComment", async () => { const comment = { @@ -55,55 +62,85 @@ it("findPreviousComment", async () => { expect(await findPreviousComment(octokit, repo, 123, "LegacyComment")).toBe(headerFirstComment) expect(octokit.issues.listComments).toBeCalledWith({ issue_number: 123 }); }); -it("updateComment", async () => { - const octokit = { - issues: { - updateComment: jest.fn(() => Promise.resolve()) - } - }; - expect( - await updateComment(octokit, repo, 456, "hello there", "") - ).toBeUndefined(); - expect(octokit.issues.updateComment).toBeCalledWith({ - comment_id: 456, - body: "hello there\n" - }); - expect( - await updateComment(octokit, repo, 456, "hello there", "TypeA") - ).toBeUndefined(); - expect(octokit.issues.updateComment).toBeCalledWith({ - comment_id: 456, - body: "hello there\n" + +describe("updateComment", () => { + let octokit; + + beforeEach(() => { + octokit = { + issues: { + updateComment: jest.fn(() => Promise.resolve()) + } + }; + }) + + it("with comment body", async() => { + expect( + await updateComment(octokit, repo, 456, "hello there", "") + ).toBeUndefined(); + expect(octokit.issues.updateComment).toBeCalledWith({ + comment_id: 456, + body: "hello there\n" + }); + expect( + await updateComment(octokit, repo, 456, "hello there", "TypeA") + ).toBeUndefined(); + expect(octokit.issues.updateComment).toBeCalledWith({ + comment_id: 456, + body: "hello there\n" + }); + expect( + await updateComment(octokit, repo, 456, "hello there", "TypeA", "hello there\n") + ).toBeUndefined(); + expect(octokit.issues.updateComment).toBeCalledWith({ + comment_id: 456, + body: "hello there\n\nhello there" + }); }); - expect( - await updateComment(octokit, repo, 456, "hello there", "TypeA", "hello there\n") - ).toBeUndefined(); - expect(octokit.issues.updateComment).toBeCalledWith({ - comment_id: 456, - body: "hello there\n\nhello there" - }); + it("without comment body and previousbody", async() => { + expect( + await updateComment(octokit, repo, 456, "", "") + ).toBeUndefined(); + expect(octokit.issues.updateComment).not.toBeCalled(); + expect(core.warning).toBeCalledWith('Comment body cannot be blank'); + }) }); -it("createComment", async () => { - const octokit = { - issues: { - createComment: jest.fn(() => Promise.resolve()) - } - }; - expect( - await createComment(octokit, repo, 456, "hello there", "") - ).toBeUndefined(); - expect(octokit.issues.createComment).toBeCalledWith({ - issue_number: 456, - body: "hello there\n" - }); - expect( - await createComment(octokit, repo, 456, "hello there", "TypeA") - ).toBeUndefined(); - expect(octokit.issues.createComment).toBeCalledWith({ - issue_number: 456, - body: "hello there\n" - }); + +describe("createComment", () => { + let octokit; + + beforeEach(() => { + octokit = { + issues: { + createComment: jest.fn(() => Promise.resolve()) + } + }; + }) + + it("with comment body or previousBody", async () => { + expect( + await createComment(octokit, repo, 456, "hello there", "") + ).toBeUndefined(); + expect(octokit.issues.createComment).toBeCalledWith({ + issue_number: 456, + body: "hello there\n" + }); + expect( + await createComment(octokit, repo, 456, "hello there", "TypeA") + ).toBeUndefined(); + expect(octokit.issues.createComment).toBeCalledWith({ + issue_number: 456, + body: "hello there\n" + }); + }) + it("without comment body and previousBody", async () => { + expect( + await createComment(octokit, repo, 456, "", "") + ).toBeUndefined(); + expect(octokit.issues.createComment).not.toBeCalled(); + expect(core.warning).toBeCalledWith('Comment body cannot be blank'); + }) }); it("deleteComment", async () => {