update test

This commit is contained in:
marocchino 2021-02-25 16:23:13 +09:00
parent 831680f812
commit d344d61a6d
No known key found for this signature in database
GPG key ID: AFF521DBDB122570

View file

@ -3,156 +3,185 @@ import {
createComment, createComment,
updateComment, updateComment,
deleteComment deleteComment
} from "../src/comment"; } from '../src/comment'
import * as core from '@actions/core'; import * as core from '@actions/core'
jest.mock('@actions/core', () => ({ jest.mock('@actions/core', () => ({
warning: jest.fn() warning: jest.fn()
})); }))
const repo = {}; const repo = {
it("findPreviousComment", async () => { owner: 'marocchino',
repo: 'sticky-pull-request-comment'
}
it('findPreviousComment', async () => {
const comment = { const comment = {
user: { user: {
login: "github-actions[bot]" login: 'github-actions[bot]'
}, },
body: "previous message\n<!-- Sticky Pull Request Comment -->" body: 'previous message\n<!-- Sticky Pull Request Comment -->'
}; }
const commentWithCustomHeader = { const commentWithCustomHeader = {
user: { user: {
login: "github-actions[bot]" login: 'github-actions[bot]'
}, },
body: "previous message\n<!-- Sticky Pull Request CommentTypeA -->" body: 'previous message\n<!-- Sticky Pull Request CommentTypeA -->'
}; }
const headerFirstComment = { const headerFirstComment = {
user: { user: {
login: "github-actions[bot]" login: 'github-actions[bot]'
}, },
body: "<!-- Sticky Pull Request CommentLegacyComment -->\nheader first message" body:
'<!-- Sticky Pull Request CommentLegacyComment -->\nheader first message'
} }
const otherComments = [ const otherComments = [
{ {
user: { user: {
login: "some-user" login: 'some-user'
}, },
body: "lgtm" body: 'lgtm'
}, },
{ {
user: { user: {
login: "github-actions[bot]" login: 'github-actions[bot]'
}, },
body: "previous message\n<!-- Sticky Pull Request CommentTypeB -->" body: 'previous message\n<!-- Sticky Pull Request CommentTypeB -->'
}, }
]; ]
const octokit = { const octokit: any = {
issues: { issues: {
listComments: jest.fn(() => listComments: jest.fn(() =>
Promise.resolve({ Promise.resolve({
data: [commentWithCustomHeader, comment, headerFirstComment, ...otherComments] data: [
commentWithCustomHeader,
comment,
headerFirstComment,
...otherComments
]
}) })
) )
} }
}; }
expect(await findPreviousComment(octokit, repo, 123, "")).toBe(comment); expect(await findPreviousComment(octokit, repo, 123, '')).toBe(comment)
expect(await findPreviousComment(octokit, repo, 123, "TypeA")).toBe( expect(await findPreviousComment(octokit, repo, 123, 'TypeA')).toBe(
commentWithCustomHeader commentWithCustomHeader
); )
expect(await findPreviousComment(octokit, repo, 123, "LegacyComment")).toBe(headerFirstComment) expect(await findPreviousComment(octokit, repo, 123, 'LegacyComment')).toBe(
expect(octokit.issues.listComments).toBeCalledWith({ issue_number: 123 }); headerFirstComment
}); )
expect(octokit.issues.listComments).toBeCalledWith({
owner: 'marocchino',
repo: 'sticky-pull-request-comment',
issue_number: 123
})
})
describe("updateComment", () => { describe('updateComment', () => {
let octokit; let octokit
beforeEach(() => { beforeEach(() => {
octokit = { octokit = {
issues: { issues: {
updateComment: jest.fn(() => Promise.resolve()) updateComment: jest.fn(() => Promise.resolve())
} }
}; }
}) })
it("with comment body", async() => { it('with comment body', async () => {
expect( expect(
await updateComment(octokit, repo, 456, "hello there", "") await updateComment(octokit, repo, 456, 'hello there', '')
).toBeUndefined(); ).toBeUndefined()
expect(octokit.issues.updateComment).toBeCalledWith({ expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456, comment_id: 456,
body: "hello there\n<!-- Sticky Pull Request Comment -->" owner: 'marocchino',
}); repo: 'sticky-pull-request-comment',
body: 'hello there\n<!-- Sticky Pull Request Comment -->'
})
expect( expect(
await updateComment(octokit, repo, 456, "hello there", "TypeA") await updateComment(octokit, repo, 456, 'hello there', 'TypeA')
).toBeUndefined(); ).toBeUndefined()
expect(octokit.issues.updateComment).toBeCalledWith({ expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456, comment_id: 456,
body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->" owner: 'marocchino',
}); repo: 'sticky-pull-request-comment',
body: 'hello there\n<!-- Sticky Pull Request CommentTypeA -->'
})
expect( expect(
await updateComment(octokit, repo, 456, "hello there", "TypeA", "hello there\n<!-- Sticky Pull Request CommentTypeA -->") await updateComment(
).toBeUndefined(); octokit,
repo,
456,
'hello there',
'TypeA',
'hello there\n<!-- Sticky Pull Request CommentTypeA -->'
)
).toBeUndefined()
expect(octokit.issues.updateComment).toBeCalledWith({ expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456, comment_id: 456,
body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->\nhello there" owner: 'marocchino',
}); repo: 'sticky-pull-request-comment',
}); body:
'hello there\n<!-- Sticky Pull Request CommentTypeA -->\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');
}) })
});
describe("createComment", () => { it('without comment body and previousbody', async () => {
let octokit; expect(await updateComment(octokit, repo, 456, '', '')).toBeUndefined()
expect(octokit.issues.updateComment).not.toBeCalled()
expect(core.warning).toBeCalledWith('Comment body cannot be blank')
})
})
describe('createComment', () => {
let octokit
beforeEach(() => { beforeEach(() => {
octokit = { octokit = {
issues: { issues: {
createComment: jest.fn(() => Promise.resolve()) createComment: jest.fn(() => Promise.resolve())
} }
}; }
}) })
it("with comment body or previousBody", async () => { it('with comment body or previousBody', async () => {
expect( expect(
await createComment(octokit, repo, 456, "hello there", "") await createComment(octokit, repo, 456, 'hello there', '')
).toBeUndefined(); ).toBeUndefined()
expect(octokit.issues.createComment).toBeCalledWith({ expect(octokit.issues.createComment).toBeCalledWith({
issue_number: 456, issue_number: 456,
body: "hello there\n<!-- Sticky Pull Request Comment -->" owner: 'marocchino',
}); repo: 'sticky-pull-request-comment',
body: 'hello there\n<!-- Sticky Pull Request Comment -->'
})
expect( expect(
await createComment(octokit, repo, 456, "hello there", "TypeA") await createComment(octokit, repo, 456, 'hello there', 'TypeA')
).toBeUndefined(); ).toBeUndefined()
expect(octokit.issues.createComment).toBeCalledWith({ expect(octokit.issues.createComment).toBeCalledWith({
issue_number: 456, issue_number: 456,
body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->" owner: 'marocchino',
}); repo: 'sticky-pull-request-comment',
body: 'hello there\n<!-- Sticky Pull Request CommentTypeA -->'
})
}) })
it("without comment body and previousBody", async () => { it('without comment body and previousBody', async () => {
expect( expect(await createComment(octokit, repo, 456, '', '')).toBeUndefined()
await createComment(octokit, repo, 456, "", "") expect(octokit.issues.createComment).not.toBeCalled()
).toBeUndefined(); expect(core.warning).toBeCalledWith('Comment body cannot be blank')
expect(octokit.issues.createComment).not.toBeCalled();
expect(core.warning).toBeCalledWith('Comment body cannot be blank');
}) })
}); })
it("deleteComment", async () => { it('deleteComment', async () => {
const octokit = { const octokit: any = {
issues: { issues: {
deleteComment: jest.fn(() => Promise.resolve()) deleteComment: jest.fn(() => Promise.resolve())
} }
}; }
expect( expect(await deleteComment(octokit, repo, 456)).toBeUndefined()
await deleteComment(octokit, repo, 456)
).toBeUndefined();
expect(octokit.issues.deleteComment).toBeCalledWith({ expect(octokit.issues.deleteComment).toBeCalledWith({
comment_id: 456 comment_id: 456,
}); owner: 'marocchino',
}); repo: 'sticky-pull-request-comment'
})
})